Live data from Hacker News

Best practices for writing SQL queries

metabase.com

11–20 of 158 posts

Re: Best practices for writing SQL queries

#11
post #4

> Avoid SELECT title, last_name, first_name FROM books LEFT JOIN authors ON books.author_id = authors.id > Prefer SELECT b.title, a.last_name, a.first_name FROM books AS b LEFT JOIN authors AS a ON b.author_id = a.id Couldn't disagree more. One letter abbreviations hurt readability IMO.

While I semi disagree, I think the author's primary point was that you should always scope your column names.

Re: Best practices for writing SQL queries

#13
post #4

> Avoid SELECT title, last_name, first_name FROM books LEFT JOIN authors ON books.author_id = authors.id > Prefer SELECT b.title, a.last_name, a.first_name FROM books AS b LEFT JOIN authors AS a ON b.author_id = a.id Couldn't disagree more. One letter abbreviations hurt readability IMO.

I prefer having some meaningful alias because trying to remember what a,b,c,d, etc gets annoying.

Re: Best practices for writing SQL queries

#14

Does anybody else like putting from first? I find it makes the auto complete sooo much better and easier to read.

Xquery and Linq both use FLWOR like syntax which puts the "FROM" first and helps auto complete, wish SQL had ordered things this way:

https://en.wikipedia.org/wiki/FLWOR

SELECT first_name FROM person WHERE first_name LIKE 'john'

becomes:

FROM person WHERE first_name LIKE 'john' SELECT first_name

SQL reads more English like while from first is more Yoda speak but the auto-complete is worth more to me.

Re: Best practices for writing SQL queries

#15
post #4

> Avoid SELECT title, last_name, first_name FROM books LEFT JOIN authors ON books.author_id = authors.id > Prefer SELECT b.title, a.last_name, a.first_name FROM books AS b LEFT JOIN authors AS a ON b.author_id = a.id Couldn't disagree more. One letter abbreviations hurt readability IMO.

While single letter aliases can be bad outside of small examples like that, even worse being:

    SELECT t1.thing
         , t2.stuff
         , t3.stuffagain
         , t4.more 
      FROM SomeTable            t1 
      JOIN TableThatLinksToSelf t2 ON 
      JOIN TableThatLinksToSelf t3 ON 
      JOIN AnotherTable         t4 ON 
that is not the point that is being made here. The point is that explicitly naming tables is beneficial to understanding and reducing issues later. Short alias is preferable to not specifying column sources at all.

Re: Best practices for writing SQL queries

#16
post #4

> Avoid SELECT title, last_name, first_name FROM books LEFT JOIN authors ON books.author_id = authors.id > Prefer SELECT b.title, a.last_name, a.first_name FROM books AS b LEFT JOIN authors AS a ON b.author_id = a.id Couldn't disagree more. One letter abbreviations hurt readability IMO.

[deleted]

Re: Best practices for writing SQL queries

#17
post #10
post #4

> Avoid SELECT title, last_name, first_name FROM books LEFT JOIN authors ON books.author_id = authors.id > Prefer SELECT b.title, a.last_name, a.first_name FROM books AS b LEFT JOIN authors AS a ON b.author_id = a.id Couldn't disagree more. One letter abbreviations hurt readability IMO.

It seems to be a matter of personal preference, but I've never liked single-character aliases myself, and never understood why so many seem to.

Lazy typing: t is shorter than tableWithTheDataIWantIn

I prefer descriptive table and other object names, and abbreviate them in aliases within queries (though usually not to single letters).

Re: Best practices for writing SQL queries

#18
post #3

>LIKE compares characters, and can be paired with wildcard operators like %, whereas the = >operator compares strings and numbers for exact matches. The = can take advantage of indexed columns. Unless this specific to certain databases, LIKE can take advantage of indexes too, without wildcards LIKE should be nearly identical in performance to = both seeking the index. >Using wildcards for searching can be expensive.…

LIKE 'abc%' will use indexes but LIKE '%abc' will not.

At least for the latest versions of every database. If you go back to a version from 10+ years ago there's no guarantees.

Re: Best practices for writing SQL queries

#20
post #18
post #3

>LIKE compares characters, and can be paired with wildcard operators like %, whereas the = >operator compares strings and numbers for exact matches. The = can take advantage of indexed columns. Unless this specific to certain databases, LIKE can take advantage of indexes too, without wildcards LIKE should be nearly identical in performance to = both seeking the index. >Using wildcards for searching can be expensive.…

LIKE 'abc%' will use indexes but LIKE '%abc' will not. At least for the latest versions of every database. If you go back to a version from 10+ years ago there's no guarantees.

Pedantically if your database supports index scans it can use the index on the column to scan for '%abc' rather than the whole table which can be much faster while not as a fast as a seek.

It can only do a seek if there are character before the wildcard: 'ab%c', 'abc%' and 'abc' getting progressively faster due to less index entries transversed.

Post reply on HN