0
0
SQLquery~20 mins

EXCEPT (MINUS) for differences in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
EXCEPT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find rows in TableA not in TableB using EXCEPT

Given two tables TableA and TableB with a single column id, what is the result of this query?

SELECT id FROM TableA
EXCEPT
SELECT id FROM TableB;

Assume:

TableA: {1, 2, 3, 4}
TableB: {3, 4, 5}
SQL
SELECT id FROM TableA
EXCEPT
SELECT id FROM TableB;
A{1, 2}
B{3, 4}
C{5}
D{1, 2, 3, 4, 5}
Attempts:
2 left
💡 Hint

EXCEPT returns rows from the first query that are not in the second.

query_result
intermediate
2:00remaining
Difference between EXCEPT and NOT IN

Consider the following two queries on tables Employees and Managers with column employee_id:

Query 1:
SELECT employee_id FROM Employees
EXCEPT
SELECT employee_id FROM Managers;
Query 2:
SELECT employee_id FROM Employees
WHERE employee_id NOT IN (SELECT employee_id FROM Managers);

Which statement is true about their results?

ABoth queries return the same result set.
BQuery 1 returns duplicates, Query 2 does not.
CQuery 2 returns NULL values if present, Query 1 does not.
DQuery 1 is faster but returns different results than Query 2.
Attempts:
2 left
💡 Hint

Think about how EXCEPT and NOT IN handle duplicates and NULLs.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in EXCEPT usage

Which of the following SQL queries will cause a syntax error?

ASELECT id FROM Table1 EXCEPT SELECT id FROM Table2;
BSELECT id, name FROM Table1 EXCEPT SELECT id, name FROM Table2;
CSELECT id FROM Table1 EXCEPT ALL SELECT id FROM Table2;
DSELECT id FROM Table1 EXCEPT SELECT name FROM Table2;
Attempts:
2 left
💡 Hint

Check if the columns in both SELECT statements match in number and type.

optimization
advanced
2:00remaining
Optimizing difference queries with EXCEPT

You want to find rows in Orders that are not in ArchivedOrders. Which query is generally more efficient?

ASELECT * FROM Orders EXCEPT SELECT * FROM ArchivedOrders;
BSELECT * FROM Orders WHERE order_id NOT IN (SELECT order_id FROM ArchivedOrders);
CSELECT * FROM Orders LEFT JOIN ArchivedOrders ON Orders.order_id = ArchivedOrders.order_id WHERE ArchivedOrders.order_id IS NULL;
DSELECT * FROM Orders WHERE EXISTS (SELECT 1 FROM ArchivedOrders WHERE ArchivedOrders.order_id = Orders.order_id);
Attempts:
2 left
💡 Hint

Consider how joins and indexes affect performance.

🧠 Conceptual
expert
2:00remaining
Behavior of EXCEPT with duplicates and NULLs

Given two tables T1 and T2 with a single column val:

T1: {1, 2, 2, NULL}
T2: {2, NULL}

What is the result of the query:

SELECT val FROM T1
EXCEPT
SELECT val FROM T2;
SQL
SELECT val FROM T1
EXCEPT
SELECT val FROM T2;
A{} (empty set)
B{1}
C{1, 2}
D{1, NULL}
Attempts:
2 left
💡 Hint

Remember how EXCEPT treats duplicates and NULL values.