2

Let's assume we have these three Tables:

Sailors (sid, name, rating), Reserve (sid, bid, day) and Boat(bid,name,color)

I want to JOIN the Table Sailors with the Table Reserve. Are the following statements equivalent? Is someone more effective?

SELECT S.sid, S.name, R.bid  
FROM Sailors as S,
     Reserves as R   
WHERE S.sid = R.sid 

SELECT S.sid, S.name, R.bid   
FROM Sailors as S
INNER JOIN Reserves  as R  ON S.sid = R.sid

Thanks in advance!

jarlh
  • 42,561
  • 8
  • 45
  • 63
MJ13
  • 195
  • 6
  • 4
    Tip of today: Always use modern, explicit `JOIN` syntax. Easier to write (without errors), easier to read (and maintain), and easier to convert to outer join if needed. – jarlh May 02 '19 at 09:27
  • 2
    (BTW, those two queries will return the same result.) – jarlh May 02 '19 at 09:27
  • @jarlh Thanks for the edit. But how did you change the syntax? How can I color the keywords? – MJ13 May 02 '19 at 09:30
  • 1
    https://stackoverflow.com/editing-help – jarlh May 02 '19 at 09:34
  • 1
    In particular the formatting used here is the "code block", which you can apply to selected text in the editor with the `{}` button, or manually by prefixing each line of a code block by four spaces. – Aaron May 02 '19 at 09:37
  • I've been working with a very old version of a db (can't remember which one) and it didn't accept the implicit join syntax. If it works, it should return an equal result, performance could (rather unlikely) be a little better in the second one. – Woozar May 02 '19 at 09:37
  • You could refer here [bad-habits-to-kick-using-old-style-joins](http://web.archive.org/web/20170811132736/https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins) – Pham X. Bach May 02 '19 at 09:44

1 Answers1

1

Exactly the same, although the newer explicit join style is regarded as the better way to do it. Its easier to read, debug and maintain.

DECLARE @Sailors TABLE (sid INT, name VARCHAR(10), rating INT)
DECLARE @Reserve TABLE (sid INT, bid INT, day VARCHAR(10))

INSERT INTO @Sailors VALUES
(1,'name', 10),
(2,'name1', 5),
(3,'name2', 2),
(4,'name3', 3)

INSERT INTO @Reserve VALUES
(1,1,'Monday'),
(2,2,'Monday'),
(3,3,'Monday')

statements:

SELECT
s.sid,s.name,r.bid
FROM @Sailors AS s, @Reserve AS r
WHERE s.sid = r.sid

returns:

sid name    bid
1   name    1
2   name1   2
3   name2   3

and

SELECT
s.sid,s.name,r.bid
FROM @Reserve r
INNER JOIN @Sailors s ON r.sid = s.sid

returns

sid name    bid
1   name    1
2   name1   2
3   name2   3
dbajtr
  • 2,024
  • 2
  • 14
  • 22