0
0
PostgreSQLquery~20 mins

INTERSECT and EXCEPT in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Set Operations Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this INTERSECT query?
Given two tables Employees_A and Employees_B with the following data:

Employees_A
id | name
1 | Alice
2 | Bob
3 | Carol

Employees_B
id | name
2 | Bob
3 | Carol
4 | Dave

What will be the result of this query?
SELECT id, name FROM Employees_A
INTERSECT
SELECT id, name FROM Employees_B;
A
id | name
2  | Bob
3  | Carol
B
id | name
4  | Dave
C
id | name
1  | Alice
D
id | name
1  | Alice
2  | Bob
3  | Carol
4  | Dave
Attempts:
2 left
💡 Hint
INTERSECT returns only rows that appear in both queries.
query_result
intermediate
2:00remaining
What rows does this EXCEPT query return?
Given two tables Products_2023 and Products_2024 with data:

Products_2023
id | product
1 | Chair
2 | Table
3 | Lamp

Products_2024
id | product
2 | Table
3 | Lamp
4 | Sofa

What is the output of this query?
SELECT id, product FROM Products_2023
EXCEPT
SELECT id, product FROM Products_2024;
A
id | product
2  | Table
3  | Lamp
B
id | product
4  | Sofa
C
id | product
1  | Chair
D
id | product
1  | Chair
4  | Sofa
Attempts:
2 left
💡 Hint
EXCEPT returns rows in the first query that are not in the second.
🧠 Conceptual
advanced
2:00remaining
Which statement about INTERSECT and EXCEPT is true?
Consider the following statements about INTERSECT and EXCEPT in SQL:

A) INTERSECT returns all rows from the first query plus rows from the second.
B) EXCEPT returns rows that appear in both queries.
C) INTERSECT returns only rows common to both queries.
D) EXCEPT returns all rows from the second query that are not in the first.

Which statement is correct?
AEXCEPT returns rows that appear in both queries.
BINTERSECT returns only rows common to both queries.
CINTERSECT returns all rows from the first query plus rows from the second.
DEXCEPT returns all rows from the second query that are not in the first.
Attempts:
2 left
💡 Hint
Think about what INTERSECT and EXCEPT do with sets of rows.
📝 Syntax
advanced
2:00remaining
Which query will cause a syntax error?
Which of the following SQL queries using INTERSECT or EXCEPT will cause a syntax error in PostgreSQL?
ASELECT id, name FROM table1 EXCEPT SELECT id FROM table2;
BSELECT * FROM table1 EXCEPT SELECT * FROM table2;
CSELECT id FROM table1 INTERSECT SELECT id FROM table2;
DSELECT id FROM table1 INTERSECT SELECT id FROM table2 WHERE id > 10;
Attempts:
2 left
💡 Hint
Check if the number and order of columns match in both queries.
optimization
expert
3:00remaining
How to optimize this EXCEPT query for large tables?
You have two large tables, Orders_2023 and Orders_2024, both with millions of rows and the same schema.

You want to find orders in 2023 that are not in 2024 using:
SELECT * FROM Orders_2023 EXCEPT SELECT * FROM Orders_2024;


Which optimization will most improve performance?
AAdd indexes on all columns used in the SELECT * for both tables.
BRewrite the query using LEFT JOIN and IS NULL to find missing rows.
CUse EXCEPT ALL instead of EXCEPT to avoid duplicate elimination.
DIncrease the work_mem setting in PostgreSQL to allow more memory for sorting.
Attempts:
2 left
💡 Hint
Think about how EXCEPT works internally with sorting and memory.