Junior
Detailed answer
JOINs & Anti-joins
SQL & Databases
How do you find employees who have no orders (LEFT JOIN / NOT EXISTS)?
Short answer: LEFT JOIN Orders and filter WHERE Orders.OrderId IS NULL, or use NOT EXISTS. NOT EXISTS often optimizes better and handles NULLs more safely than NOT IN.
Sample solution
T-SQL
-- LEFT JOIN anti-join
SELECT e.EmpId, e.Name
FROM Employees e
LEFT JOIN Orders o ON o.EmpId = e.EmpId
WHERE o.OrderId IS NULL;
-- NOT EXISTS (preferred in many plans)
SELECT e.EmpId, e.Name
FROM Employees e
WHERE NOT EXISTS (
SELECT 1 FROM Orders o WHERE o.EmpId = e.EmpId
);
Avoid NOT IN (subquery) if the subquery can return NULL — it can yield empty results.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png