5 ms·
Could you write their example SELECT members.firstname || ' ' || members.lastname AS "Full Name" FROM borrowings INNER JOIN members ON memb
by Animats 10y ago
Could you write their example
SELECT members.firstname || ' ' || members.lastname
AS "Full Name"
FROM borrowings
INNER JOIN members
ON members.memberid=borrowings.memberid
INNER JOIN books
ON books.bookid=borrowings.bookid
WHERE borrowings.bookid IN (SELECT bookid
FROM books
WHERE stock>(SELECT avg(stock)
FROM books))
GROUP BY members.firstname, members.lastname;
more simply as
SELECT @avgstock = avg(stock) from books;
SELECT members.firstname || ' ' || members.lastname
AS "Full Name"
FROM borrowings, members, books
WHERE members.memberid = borrowings.memberid
AND books.bookid = borrowings.bookid
AND borrowings.bookid IN
(SELECT bookid
FROM books
WHERE stock > @avgstock))
GROUP BY members.firstname, members.lastname;
Are those explicit INNER JOIN clauses necessary? By now, most SQL
query optimizers can figure out on their own how to join tables.
- mattmanser 10y agoMy impression was that few people use the comma syntax these days, it's old fashioned, harder to read, harder to change, harder for the query plan optimizer, etc. I know this is an appeal to authority, but the consensus is pretty clear if you search SO and programmer stackexchange: http://stackoverflow.com/questions/1018822/inner-join-on-vs-where-clause http://stackoverflow.com/questions/1018822/inner-join-on-vs-... http://programmers.stackexchange.com/questions/78225/using-join-keyword-or-not http://programmers.stackexchange.com/questions/78225/using-j... You're also introducing a lot more concepts by adding variables, and that code wouldn't work on SQL Server, for example, as you need to declare the variable first. Then again the `||` doesn't work on SQL Server either.
- hobs 10y agoAs an active DBA I would tell you that most of the time I would expect the comma join syntax to come from someone with grey hair, its an older format that once was standard. I would also say that if you are suggesting style, you might ommit that INNER from the JOIN (if you are targeting sql server its redundant.)
- bigger_cheese 10y agoI tend to mix and match the two. The comma join looks way more natural to me. One thing I do habitually which I don't see other people do much is use aliases. I learnt SQL through SAS which is where the excessive aliases came from and it kind of stuck with me.
- hobs 10y agoIf you write any decent amount of SQL and you dont alias, I will find you, and I will ... teach you a better way to code. Most people don't have to do stuff in highly normalized systems, so when you write code that can have dozens of joins and potentially more predicates, you would go insane NOT using aliasing.
- lucio 10y agoThe "comma join" disconnects the join from the condition. It is hard to find the actual join condition between all the "where" conditions. By using JOIN ... ON .., you explicitly link tables with their join condition and also separate join conditions from other filter conditions on the "where".
- DanielBMarkham 10y agoThe "comma join" disconnects the join from the condition. Yes, because it's more important to first know what's being joined, then traverse that list looking for how it's being joined. You get the full name in the conditionals, so it's not like it's a mystery what they attach to. But I have grey hair, so there you go.... :) Comma syntax makes much more sense to me. But now I know that this is not The Way Things Are Done. I also learned from the comments that comma-first isn't recommended. So now my code will be more legible. It's all good.
- lucio 10y agoI'm 45. I'm so old that I'm proficient using (+) to code outer joins in oracle :) Comma-join was the standard for me, and I had a hate-phase towards LEFT/RIGHT JOIN. But I must admin that once I've really tried the "new" join-on syntax it has great advantages that outweighs the pain of change.
- taberiand 10y agoThere are dozens of different ways you could write the given query, e.g., Select members.firstname + ' ' + members.lastname as "Full Name" From members Join borrowings on members.memberid = borrowings.memberid Join ( Select bookid from books where stock > (Select avg(stock) from books) ) books on borrowings.bookid = books.bookid Using comma syntax precludes many of these other forms, and therefore I would not recommend teaching it to beginners (or in fact ever using it), as it limits your options.
- dragonwriter 10y ago> Are those explicit INNER JOIN clauses necessary? Explicit INNER JOIN is clearer than comma joins, which combine join conditions with filter conditions. > By now, most SQL query optimizers can figure out on their own how to join tables. Clarity, rather than efficiency, is the reason to prefer explicit INNER JOINS over comma joins. Though in this case, the conditions for the explicit INNER JOINS could be simplified by using USING (and, also, eliminating the unnecessary subquery): SELECT members.firstname || ' ' || members.lastname AS "Full Name" FROM borrowings INNER JOIN members USING memberid INNER JOIN books USING bookid WHERE books.stock > (SELECT avg(stock) FROM books) GROUP BY members.firstname, members.lastname;
- mkesper 10y agoThanks for explaining a use of using!
- rtkwe 10y agoThere's so many ways to accomplish the same thing in $SQLEngine that a lot of it comes down to style and clarity/rereadability. I'd prefer keeping the join conditions as USING or ON in most cases because it keeps them close to the statements related to it. Where I work I have to read through a lot of older code that hides them in the where clause and uses 3 letter abbreviations almost exclusively for table aliases which can be pretty tedious to sift through. Again it's mainly down to personal choices though (or in some cases institutional guidelines).
- collyw 10y agoI was taught the second style of joins in the WHERE clause, but moved to the first style as I saw it was recommended. Having used both, the INNER JOIN style makes more sense to me, as you are describing the JOINS when you are adding the tables and keeps it separate from the WHERE clause which is really a filtering step. The second style combines the two and makes and makes it less clear what you are trying to achieve in my opinion.