TECHNOLOGIESTieInn Technologies

LEFT, RIGHT and FULL joins

SQL · 15 questions

Short notes

Key idea

A LEFT JOIN keeps every row of the left table, matched or not. Where nothing matches, the right table's columns come back as NULL. That makes it the tool for "who has none?" questions.

Syntax

SELECT c.name, o.order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id;
  • RIGHT JOIN is the mirror image; most people just swap the tables.
  • FULL OUTER JOIN keeps unmatched rows from both sides.

Example

Customers who never ordered:

SELECT c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;

Traps

  • A WHERE on the right table undoes the left join. WHERE o.status = 'paid' drops the NULL rows. Put that condition in the ON clause instead.
  • COUNT(*) counts unmatched rows as 1. Use COUNT(o.order_id) to get 0 for customers with no orders.
  • Use COALESCE(SUM(o.amount), 0) to show 0 instead of NULL.

Want the full lesson with step-by-step examples, a project and a certificate? SQL course, ₹120 →

Questions

Open in Code Lab5 free questions
5 questions

    LEFT, RIGHT and FULL joins

  1. Question 1easy

    customers LEFT JOIN orders: which customers appear in the result?

  2. LEFT, RIGHT and FULL joins

  3. Question 2medium

    40 customers placed 80 orders between them, and 9 customers never ordered. How many rows does this return?

    SELECT COUNT(*)
    FROM customers c
    LEFT JOIN orders o ON o.customer_id = c.customer_id;
  4. LEFT, RIGHT and FULL joins

  5. Question 3hard

    Customer 11 never placed an order. What does this return?

    SELECT COUNT(*) - COUNT(o.order_id)
    FROM customers c
    LEFT JOIN orders o ON o.customer_id = c.customer_id
    WHERE c.customer_id = 11;
  6. LEFT, RIGHT and FULL joins

  7. Question 4hard

    You want every customer with their delivered orders, keeping customers who have none. Which is right?

  8. LEFT, RIGHT and FULL joins

  9. Question 5easy

    In customers c LEFT JOIN orders o, customer 1 has no orders. What is o.amount on customer 1's row?

10 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.