How do you write a SQL query to find departments that have no employees using a LEFT JOIN?
💡 Model Answer
To find departments without employees, perform a LEFT JOIN from the department table to the employee table and filter rows where the employee key is NULL. Example:
SELECT d.deptno, d.dname
FROM dept d
LEFT JOIN emp e ON d.deptno = e.deptno
WHERE e.empno IS NULL;
The LEFT JOIN keeps all departments, attaching employee rows when they exist. When a department has no matching employee, the joined columns are NULL, so the WHERE clause selects those rows. This query runs in O(n + m) time, where n is the number of departments and m the number of employees, assuming appropriate indexes on deptno. It is a standard pattern for identifying orphaned or missing related records.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers on a discreet on-screen overlay.
Get Assisting AI — Starts at ₹500