Skip to main content
InterviewHubPRO
Section Overview
Q40

Question 40 of 50

Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)

80%
SQL & Relational Databases (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)

SQL Joins Deep Dive: Anti-Joins (`NOT EXISTS` vs `NOT IN`).

senior LevelCode ExampleDatabases & SQL

In Simple Words:

  • Inner Join: Returns rows with matching keys in both tables.
  • Left Outer Join: Returns all rows from left table, with NULLs for non-matching right rows.
  • Anti-Join: Finds records in Table A that have no matching record in Table B.
  • Performance Rule: Prefer NOT EXISTS over NOT IN! If the subquery in NOT IN contains even a single NULL value, NOT IN returns zero rows due to SQL three-valued logic (TRUE, FALSE, UNKNOWN), while NOT EXISTS handles NULLs safely and allows the optimizer to use an Anti-Hash Join.
sql
-- Optimal Anti-Join pattern using NOT EXISTS
SELECT u.id, u.email
FROM users u
WHERE NOT EXISTS (
  SELECT 1 FROM orders o WHERE o.user_id = u.id
);