SQL JOINs PRACTICE EXERCISES TechCareer Lab - techcareerlab.in (Demonstration resource) Use these two tables, from the tutorial "SQL JOINs Explained with Practical Examples". employees id | name | department_id ---+-------+-------------- 1 | Asha | 10 2 | Ravi | 20 3 | Meera | NULL 4 | Karan | 10 departments id | name ---+------------ 10 | Engineering 20 | Finance 30 | Marketing Setup (PostgreSQL / SQLite): CREATE TABLE departments (id INTEGER PRIMARY KEY, name VARCHAR(50) NOT NULL); CREATE TABLE employees ( id INTEGER PRIMARY KEY, name VARCHAR(50) NOT NULL, department_id INTEGER REFERENCES departments(id) ); INSERT INTO departments VALUES (10, 'Engineering'), (20, 'Finance'), (30, 'Marketing'); INSERT INTO employees VALUES (1, 'Asha', 10), (2, 'Ravi', 20), (3, 'Meera', NULL), (4, 'Karan', 10); EXERCISES --------- 1. List each employee with their department name, only for employees who have a department. 2. List all employees with their department name, showing NULL where there is none. 3. List all departments with the names of their employees, including departments with no employees. 4. Find employees who do not belong to any department. 5. Find departments that have no employees. 6. Count the number of employees in each department, including departments with zero employees. 7. List all employees, showing the department name only if the department is Engineering. 8. List the names of departments that have more than one employee. 9. List every employee and department combination (every employee paired with every department). 10. Return all employees and all departments, matched where possible (if your database supports it). ANSWERS ------- 1. SELECT e.name, d.name AS department FROM employees e INNER JOIN departments d ON e.department_id = d.id; -- Asha/Engineering, Ravi/Finance, Karan/Engineering 2. SELECT e.name, d.name AS department FROM employees e LEFT JOIN departments d ON e.department_id = d.id; -- adds Meera/NULL 3. SELECT d.name AS department, e.name AS employee FROM departments d LEFT JOIN employees e ON e.department_id = d.id; -- includes Marketing/NULL 4. SELECT name FROM employees WHERE department_id IS NULL; -- Meera 5. SELECT d.name FROM departments d LEFT JOIN employees e ON e.department_id = d.id WHERE e.id IS NULL; -- Marketing 6. SELECT d.name, COUNT(e.id) AS employee_count FROM departments d LEFT JOIN employees e ON e.department_id = d.id GROUP BY d.name; -- Engineering 2, Finance 1, Marketing 0 -- Note: COUNT(e.id) counts only matched rows; COUNT(*) would wrongly give Marketing 1. 7. SELECT e.name, d.name AS department FROM employees e LEFT JOIN departments d ON e.department_id = d.id AND d.name = 'Engineering'; -- all four employees; department filled only for Asha and Karan 8. SELECT d.name FROM departments d JOIN employees e ON e.department_id = d.id GROUP BY d.name HAVING COUNT(*) > 1; -- Engineering 9. SELECT e.name, d.name AS department FROM employees e CROSS JOIN departments d; -- 4 x 3 = 12 rows 10. SELECT e.name, d.name AS department FROM employees e FULL OUTER JOIN departments d ON e.department_id = d.id; -- Not supported in MySQL or older SQLite versions (before 3.39.0).