I am writing an SQL statement which needs to:
- Look for a job with a certain number
- Match that job to another table
- Show the results
This is what I currently have.
select * from PreviousJobs pj, Jobs j where jobId = '273121'
AND where (pj.sOtherRef = j.sOtherRef) = True
Hmmmmm
There’s no nesting needed; it is a simple JOIN that’s required, and that would be best written with the explicit join notation:
You don’t need the second WHERE in your statement (that is a syntax error; you should have supplied us with the error message(s) that your DBMS gave you). You don’t need to compare the comparison with TRUE.
The comma-separated list of table names in the FROM clause was necessary in SQL-86 and SQL-89, but has not been necessary since SQL-92 support was added into the DBMS. You should know about it so that if you get to read old SQL, you know what it means. But you should plan to use only the new JOIN notation unless there’s overwhelming (and ill-advised) pressure from workplace standards to use the old notation.
Depending on the DBMS you use, you might find the AS in the table aliases is not permitted (Oracle), even though the standard says it is OK. That sort of difference is why it is a good idea to include information about your DBMS in the question.