Bird
0
0

Given the categories table:

medium📝 query result Q5 of 15
SQL - Advanced Joins
Given the categories table:
id | name       | parent_id
---+------------+----------
1  | Electronics| NULL
2  | Computers  | 1
3  | Laptops    | 2
4  | Phones     | 1
What will the following query return?
SELECT c.name AS Category, p.name AS Parent
FROM categories c LEFT JOIN categories p ON c.parent_id = p.id;
AA list of categories with their parent category names, NULL if no parent exists.
BOnly categories that have a parent category, excluding top-level categories.
CAll categories with their own names repeated as parent names.
DAn error because self join requires INNER JOIN, not LEFT JOIN.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the join type

    LEFT JOIN returns all rows from c and matching rows from p, or NULL if no match.
  2. Step 2: Join condition

    Joining on c.parent_id = p.id links each category to its parent.
  3. Step 3: Result interpretation

    Top-level categories have parent_id NULL, so p.name will be NULL for them.
  4. Final Answer:

    A list of categories with their parent category names, NULL if no parent exists. is correct.
  5. Quick Check:

    LEFT JOIN preserves all categories including those without parents [OK]
Quick Trick: LEFT JOIN keeps all categories, parents NULL if none [OK]
Common Mistakes:
MISTAKES
  • Assuming INNER JOIN to include all categories
  • Expecting parent names for top-level categories
  • Confusing join keys causing incorrect matches

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SQL Quizzes