1.Every department, with its people
IntermediateUsing a RIGHT JOIN from employees to departments, return every dept_name with the name of each employee in it. Departments with no employees must still appear. Sort by department, then name.
Show hint
In a RIGHT JOIN, every row of the right (second) table is kept.
Show solution
SELECT d.dept_name, e.name
FROM employees e
RIGHT JOIN departments d ON e.dept_id = d.dept_id
ORDER BY d.dept_name, e.name;dept_name | name
------------+------
Engineering | Meera
Engineering | Ravi
Management | Asha
Research | NULL
Sales | JohnResearch has no employees, so its name is NULL. Priya has no department, so a RIGHT JOIN drops her. Most people write this query as departments LEFT JOIN employees, which gives the same result and reads more naturally.