Rule for initialisms within FROM clauses.
See original GitHub issueThis is prompted by the excellent work of @piotrgredowski on #473 for #469.
The fishtown sql guide says:
Avoid table aliases in join conditions (especially initialisms) – it’s harder to understand what the table called “c” is compared to “customers”.
Which we have implemented a rule for which corrects:
SELECT u.id, c.first_name, c.last_name, COUNT(o.user_id)
FROM users as u
JOIN customers as c on u.id = c.user_id
JOIN orders as o on u.id = o.user_id
to
SELECT u.id, customers.first_name, customers.last_name, COUNT(orders.user_id)
FROM users as u
JOIN customers on u.id = customers.user_id
JOIN orders on u.id = orders.user_id
This is definitely in line with the letter of the law. What I’m wondering is whether initialisms in a FROM clause are also part of the same antipattern 🤔 . Should there be an additional rule to avoid initialisms in the FROM
clause such that the full corrected query is:
SELECT users.id, customers.first_name, customers.last_name, COUNT(orders.user_id)
FROM users
JOIN customers on users.id = customers.user_id
JOIN orders on users.id = orders.user_id
I’m going to ask the fishtown guys what they think, but I’m curious for what other people think.
[I also think that @NiallRees will be amused by this suggestion because he’s seen a load of my production sql and knows that this is one of my bad habits]
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:9 (8 by maintainers)
I’m pretty confident that the answer here is aliases shouldn’t be used for the same reason as you don’t want it in join clauses.
From what I’ve read I’m meant to add it to the pull request now but I’ll add to both to make sure! Thanks!