SQL

SQL

Made by DeepSource

Table aliasing in FROM and JOIN operators SQL-L031

Style
Minor

Avoid table aliases in FROM clauses and JOIN conditions, as it’s harder to understand what the table called “c” is compared to “customers”.

Exception

This rule is controversial and for many larger databases avoiding alias is neither realistic nor desirable. In this case this rule should be disabled.

Bad practice

SELECT
    COUNT(o.customer_id) as order_amount,
    c.name
FROM orders as o
JOIN customers as c on o.id = c.user_id

Recommended

SELECT
    COUNT(orders.customer_id) as order_amount,
    customers.name
FROM orders
JOIN customers on orders.id = customers.user_id;

-- Self-join will not raise issue

SELECT
    table1.a,
    table_alias.b,
FROM
    table1
    LEFT JOIN table1 AS table_alias ON
        table1.foreign_key = table_alias.foreign_key