Bird
0
0

Given the tables:

medium📝 query result Q13 of 15
PostgreSQL - Joins in PostgreSQL
Given the tables:
employees(id, name, dept_id)
departments(id, dept_name)
What will this query return?
SELECT e.name, d.dept_name FROM employees e INNER JOIN departments d ON e.dept_id = d.id;
AAll employees, even if they have no matching department.
BAn error because of alias usage.
CAll departments with employee names, even if no employees exist.
DAll employees with their department names, only if dept_id matches a department id.
Step-by-Step Solution
Solution:
  1. Step 1: Understand INNER JOIN effect on employees and departments

    The INNER JOIN returns rows where employees.dept_id matches departments.id, so only employees with a valid department appear.
  2. Step 2: Analyze query output

    The query selects employee names and their department names for matching rows only. Employees without a matching department are excluded.
  3. Final Answer:

    All employees with their department names, only if dept_id matches a department id. -> Option D
  4. Quick Check:

    INNER JOIN filters unmatched rows [OK]
Quick Trick: INNER JOIN shows only matching rows from both tables [OK]
Common Mistakes:
  • Assuming unmatched employees appear
  • Confusing aliases causing errors
  • Thinking departments without employees appear

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes