scuba,
As others have said, the ansi syntax is much more powerful. Be careful
not to cripple the server by doing lots of left joins to 'get at'
information. If coursefacilitytypes always has a matching record in
Courses, then you could do your query like this:
SELECT FT.FACILITY_TYPE, CASE WHEN CF.CURSE_ID IS NULL THEN 0 ELSE 1
END AS SELECTED,
C.CURSE_NAME, C.CURSE_NUMBER
FRM FACILITY_TYPES FT
LEFT JIN CURSE_FACILITY_TYPES CF
JIN CURSES C
N CF.CURSE_ID = C.CURSE_ID and
cf.course_id=1
N FT.FACILITY_TYPE_ID = CF.FACILITY_TYPE_ID
I'm pretty certain the the coursid=1 could have stayed at the bottom or
it can go where I put it. you unlock the power of doing a left
join to a 'quantity', where the quantity is some joined set of tables
with exact criteria, then you will be writing all kinds of queries
that have stumped everyone for years.
At my job, no one knew how to select employees and their spouses, but
be sure to include employees with no spouse.
people left join (people as familymembers join personalinfo on
familymembers.A=personalinfo.B and
personalinfo.relationship='spouse')
on people.A=familymembers.B
The A, B are just joining keys. If you don't put the 'spouse' stuff in
an inner join, you end up getting all the kids as well, their
relationship will be NULL, the spouse will have relationship 'spouse'.
This can really screw up the sums and counts in any report you are
making. You need to pinpoint the spouse and only the spouse's
what ansi syntax lets you do with ease.
At my job, my boss gave everyone SQL for Smarties by Celko (kickbacks
appreciated, but not necessary), and I'd recommend it. I've written
queries for all kinds of things in 20 minutes while coworkers are
stumped and then spend hours and days writing sprocs and fighting with
other parts of my company to get the sprocs loaded into customers
systems, etc.
I use Sql Server 2000 and long ago stopped typing 'inner' and 'outer'.
Saves a wee bit of time.
scubakiz wrote:
Thank you all very much for your help. I think I see how this syntax
can offer more flexibility. I already found a solution to a long
standing problem that's been a challenge using the old syntax. For
example, if I wanted to extend the UTER join and get supporting
information from a thrid table if a join existed, I'd have to resort to
creative sub-queries. This does not work:
SELECT FT.FACILITY_TYPE, CASE WHEN CF.CURSE_ID IS NULL THEN 0 ELSE 1
END AS SELECTED, C.CURSE_NAME, C.CURSE_NUMBER
FRM FACILITY_TYPES FT, CURSE_FACILITY_TYPES CF, CURSES C
WHERE FT.FACILITY_TYPE_ID *= CF.FACILITY_TYPE_ID
AND CF.CURSE_ID = C.CURSE_ID
AND C.CURSE_ID = 1
In SQL 2005, it returns only a single row for course 1 (I added a
record for course 1). I seem to remember in SQL 2000, I would just get
an error about an inner join not being allowed inside an outer join.
To get this to work, I'd have to write UGLY SQL like this:
SELECT FT.FACILITY_TYPE, CASE WHEN CF.CURSE_ID IS NULL THEN 0 ELSE 1
END AS SELECTED,
(SELECT CURSE_NAME FRM CURSES WHERE CURSE_ID = CF.CURSE_ID)
AS CURSE_NAME,
(SELECT CURSE_NUMBER FRM CURSES WHERE CURSE_ID = CF.CURSE_ID)
AS CURSE_NUMBER
FRM FACILITY_TYPES FT, CURSE_FACILITY_TYPES CF
WHERE FT.FACILITY_TYPE_ID *= CF.FACILITY_TYPE_ID
AND CF.CURSE_ID = 1
Not pleasant or effecient!
However, using the examples you all provided (thank you again), I can
easily extend them to create:
SELECT FT.FACILITY_TYPE, CASE WHEN CF.CURSE_ID IS NULL THEN 0 ELSE 1
END AS SELECTED,
C.CURSE_NAME, C.CURSE_NUMBER
FRM FACILITY_TYPES FT
LEFT UTER JIN CURSE_FACILITY_TYPES CF
N (FT.FACILITY_TYPE_ID = CF.FACILITY_TYPE_ID AND
CF.CURSE_ID = 1)
LEFT UTER JIN CURSES C
N (CF.CURSE_ID = C.CURSE_ID)
Which gives me NULLs when no match exists, but the course name and
number when there is a match. This is great!