views:

41

answers:

2

Why is the ( ) mandatory in the SQL statement

select * from gifts INNER JOIN sentgifts using (giftID); 

? The ( ) usually is for specifying grouping of something. But in this case, are we supposed to be able to use 2 or more field names...? in the example above, it can be all clear that it is 1 field, is it just that the parser is not made to bypass the ( ) when it is all clear? (such as in the language Ruby).

+1  A: 

You can in fact specify a list of columns, check here.

And it's mandatory because syntax exceptions blow the parser and are bad for performance.

That said one could argue that USING itself is already just syntactic sugar, it's just a shortcut of specifying a join with the normal ON syntax in case the column names are the same for the joined tables.

Since you can join ON multiple columns USING is just doing the same thing.

The following statements are therefore equivalent:

SELECT A.c1, A.c2, A.c3 FROM TABLE_A A
JOIN TABLE_B B 
ON A.c1 = B.c1 
ON A.c2 = B.c2 
ON A.c3 = B.c3

and

SELECT A.c1, A.c2, A.c3 FROM TABLE_A A
JOIN TABLE_B B 
USING (c1,c2,c3)
ntziolis
+1  A: 

Yes you can specify multiple columns inside of the using clause like below.

using(col_1,col_2,col_3)
Mike Keller