views:

231

answers:

5

Is this equivalent to a LEFT JOIN?

SELECT DISTINCT a.name, b.name 
  FROM tableA a, 
       (SELECT DISTINCT name FROM tableB) as b

It seems as though there is no link between the two tables.

Is there an easier / more efficient way to write this?

+5  A: 

Not, it is equivalent to a cross or cartesian join (really bad) with a distinct applied afterwards. It is pretty hard to know what you really want with the query as it stands.

Otávio Décio
+2  A: 

Isn't this the same as

SELECT DISTINCT a.name, b.name
    FROM tableA a, tableB b

although I would be questioning the purpose for this query.

Doug Stalter
+2  A: 

It's ANSI-89 syntax for a cross join, producing a cartesian product (that's bad). Re-written using ANSI-92 JOIN syntax:

If on SQL Server/Oracle/Postgres, use:

    SELECT DISTINCT
           a.name,
           b.name
      FROM TABLEA a
CROSS JOIN (SELECT b.name 
              FROM TABLEB b) AS b

MySQL supports using:

SELECT DISTINCT
       a.name,
       b.name
  FROM TABLEA a
  JOIN (SELECT b.name 
          FROM TABLEB b) AS b

We'd need to know if there is any column(s) to tie records between the two tables to one another in order to update the query to use either an INNER JOIN or OUTER JOIN.

OMG Ponies
You'd need "CROSS JOIN", no? JOIN expects an ON clause in SQL Server. `Msg 102, Level 15, State 1, Line 5 Incorrect syntax near 'b'.`
gbn
@gbn: The OP hasn't specified which database is being used. The syntax is valid on MySQL.
OMG Ponies
Fair enough, but it's not portable...
gbn
OK, it's clarified: for SQL Server 2008...
gbn
+2  A: 

I hate the stigma people apply to cartesian joins. They're wonderful when used properly. I have a payroll application and we have to apply all the different taxing authorities to each employee. So, I have one table of employees and one table of taxing authorities.

Anyway.. I just wanted to defend the wonderful cartesian join. (:

</soapbox>

Rob
I have no objection to cross joins when they are appropriate. However when someone uses this outdated implicit join style, the maintainer cannot tell if a cross join was meant or if the query was just bad. Implicit cross joins which then have a distinct are usually the ones done by mistake and it is is a very bad mistake from a performance and data (you may be generating the wrong answer even with the distinct) perspective.
HLGEM
I agree, CROSS JOINs are useful. But when you meant to create a CROSS JOIN, use the appropriate CROSS JOIN syntax. Join the 90's and keep looking forward.
Aaron Bertrand
I agree with that.. you should use the appropriate syntax. It's just that i always heard database people spout off about how evil cross joins are and then forget to put a where clause in and return a million rows in.
Rob
A: 

Since there is no joining field you have a cross join. The distinct limits the total number of records to remove duplicates, but probably still is not giving you the answer you want.

Do this. Check the number of records you are getting. Then write a left join joining on company name (or company id which is what you really should have as a join field as company names change frequently). I'll bet you get a different number of records returned. I did this will two tables I had handy and this is what I got: Table a had 467 records Table b had 4413 records The cross join had 2060871 The cross join with the distinct had 826804 The left join had 4712 The inner join had 893

So you can see adding the distinct to the cross join loweres the number of records returned but doesn't guarantee you will get the result you would have had with the correct join. Given that you said the tables were company and company address, it would be very unlikely that a cross join was what you wanted.

HLGEM