SQL

SQL

Made by DeepSource

Use LEFT JOIN instead of RIGHT JOIN SQL-L055

Style
Minor

A RIGHT JOIN returns all rows from the right table and matching rows from the left table, while a LEFT JOIN returns all rows from the left table and matched rows from the right table.

Depending on the order and structure of your tables, using a RIGHT JOIN may be less efficient or clear than using a LEFT JOIN. We suggest switching to a LEFT JOIN if it detects that your query can be simplified or improved by doing so.

Bad practice

In this example, the two SUM functions don't have the same capitalisation.

SELECT
    foo.col1,
    bar.col2
FROM foo
RIGHT JOIN bar
    ON foo.bar_id = bar.id

Recommended

Refactor and use LEFT JOIN instead.

SELECT
    foo.col1,
    bar.col2
FROM bar
LEFT JOIN foo
    ON foo.bar_id = bar.id