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.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details