Common

SELECT

Qualified column names

Use references to identify table's columns

E.g., SELECT c.name FROM customers c;

Otherwise, it might not able to map all columns from a table.

When a query involves multiple tables, it can be difficult to identify which column belongs to which table, especially if the tables have columns with the same names.

References should be qualified if select has more than one referenced table/view.

Learn more: references.qualification

-- Anti-pattern
SELECT a, b
FROM foo
LEFT JOIN vee ON vee.a = foo.a;

-- ✅ Best practice
SELECT foo.a, vee.b
FROM foo
LEFT JOIN vee ON vee.a = foo.a;
   

Table aliases should be unique

Learn more: aliasing.unique.table

-- Anti-pattern
SELECT
    t.a,
    t.b
FROM foo AS t, bar AS t;

-- ✅ Best practice
-- Make all tables have a unique alias.
SELECT
    f.a,
    b.b
FROM foo AS f, bar AS b

Last updated