CodeBlanks Logo Icon CodeBlanks

LEFT vs RIGHT

Choose the right outer join for the task.

LEFT vs RIGHT

LEFT and RIGHT JOIN apply the same logic from opposite directions. Swapping table order converts one into the other.

SELECT c.name, o.id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;

You can get the same result with RIGHT JOIN too.

c.name, o.id is selected from customers c. orders o is joined on o.customer_id = c.id, keeping every row of the left table.

LEFT vs RIGHT — details

Most developers prefer LEFT JOIN because reading left-to-right feels natural. RIGHT JOIN is rarely used.

Mixing LEFT and RIGHT JOIN in one query makes it hard to read; putting the main table first and always using LEFT JOIN is a good habit.

Practice LEFT vs RIGHT

Keep customers on the left and LEFT JOIN with orders.

Starter code

query.sql
-- pick direction
SELECT c.name, o.id
FROM customers c

Sample solution

query.sql
SELECT c.name, o.id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;

Expected output

name    id
Ayşe    1
Fatma   NULL

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Pick the direction

Question
SELECT c.name, o.id
FROM customers c
____ orders o ON o.customer_id = c.id;
Answer
SELECT c.name, o.id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;

Explanation: LEFT JOIN is the right answer.

2 LEFT vs RIGHT: match the terms

Term Meaning
LEFT JOIN all left
RIGHT JOIN all right
NULL empty
ON condition

3 Which direction is preferred over RIGHT JOIN?

  • INNER JOIN
  • LEFT JOIN Correct answer
  • CROSS JOIN
  • UNION

Explanation: LEFT JOIN is preferred as it reads more naturally.

All lessons in Intermediate