> 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.
Best practices for writing SQL queries
11–20 of 158 posts
Re: Best practices for writing SQL queries
#12Does anybody else like putting from first? I find it makes the auto complete sooo much better and easier to read.
Re: Best practices for writing SQL queries
#13> 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.
Re: Best practices for writing SQL queries
#14Does anybody else like putting from first? I find it makes the auto complete sooo much better and easier to read.
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> 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.
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> 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.
Re: Best practices for writing SQL queries
#17> 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.
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>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.…
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
#19Re: Best practices for writing SQL queries
#20>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.
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.