Bird
0
0

Find the problem in this query:

medium📝 Debug Q7 of 15
PostgreSQL - Joins in PostgreSQL
Find the problem in this query:

SELECT d.name, e.name FROM departments d RIGHT JOIN employees e ON d.id = e.dept_id ORDER BY d.name;

Given that some employees have no matching department.
ARIGHT JOIN should be LEFT JOIN to keep all departments
BOrdering by d.name will put NULLs first, causing confusion
CON condition is missing
DEmployees table should be on left side
Step-by-Step Solution
Solution:
  1. Step 1: Understand RIGHT JOIN result

    RIGHT JOIN keeps all employees. Some employees have no matching department, so d.name is NULL for them.
  2. Step 2: Effect of ORDER BY d.name

    Ordering by d.name will place NULL values first by default, which may confuse output order.
  3. Final Answer:

    Ordering by d.name with NULLs can cause unexpected order. -> Option B
  4. Quick Check:

    ORDER BY column with NULLs can affect result order [OK]
Quick Trick: ORDER BY columns with NULLs may reorder rows unexpectedly [OK]
Common Mistakes:
  • Assuming RIGHT JOIN is wrong here
  • Missing ON condition (it is present)
  • Thinking table order must change

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes