Bird
Raised Fist0
MySQLquery~20 mins

EXCEPT equivalent in MySQL - Practice Problems & Coding Challenges

Choose your learning style10 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
What is the output of this EXCEPT equivalent query?
Given two tables Employees and Managers with a single column name, what does the following query return?

SELECT name FROM Employees WHERE name NOT IN (SELECT name FROM Managers);
MySQL
CREATE TABLE Employees (name VARCHAR(20));
INSERT INTO Employees VALUES ('Alice'), ('Bob'), ('Charlie');
CREATE TABLE Managers (name VARCHAR(20));
INSERT INTO Managers VALUES ('Bob');
A[{"name": "Bob"}]
B[{"name": "Alice"}, {"name": "Charlie"}]
C[{"name": "Alice"}, {"name": "Bob"}, {"name": "Charlie"}]
D[]
Attempts:
2 left
💡 Hint
Think about which employees are not managers.
❓ query_result
intermediate
2:00remaining
Which query returns rows in TableA but not in TableB?
You want to find all rows in TableA that do not appear in TableB. Which of these queries correctly does that in MySQL?
ASELECT * FROM TableA WHERE NOT EXISTS (SELECT 1 FROM TableB WHERE TableB.id = TableA.id);
BSELECT * FROM TableA EXCEPT SELECT * FROM TableB;
CSELECT * FROM TableA WHERE id NOT IN (SELECT id FROM TableB);
DSELECT * FROM TableB WHERE id NOT IN (SELECT id FROM TableA);
Attempts:
2 left
💡 Hint
MySQL does not support EXCEPT. Use a subquery with NOT EXISTS or NOT IN.
📝 Syntax
advanced
2:00remaining
Which query will cause a syntax error in MySQL?
Identify the query that will cause a syntax error in MySQL when trying to find rows in Table1 not in Table2.
ASELECT * FROM Table1 LEFT JOIN Table2 ON Table1.id = Table2.id WHERE Table2.id IS NULL;
BSELECT * FROM Table1 WHERE NOT EXISTS (SELECT 1 FROM Table2 WHERE Table2.id = Table1.id);
CSELECT * FROM Table1 WHERE id NOT IN (SELECT id FROM Table2);
DSELECT * FROM Table1 EXCEPT SELECT * FROM Table2;
Attempts:
2 left
💡 Hint
MySQL does not support EXCEPT keyword.
❓ optimization
advanced
2:00remaining
Which query is the most efficient EXCEPT equivalent in MySQL for large tables?
You want to find rows in Orders that are not in ShippedOrders. Which query is generally the most efficient in MySQL for large datasets?
ASELECT * FROM Orders WHERE id NOT IN (SELECT id FROM ShippedOrders);
BSELECT * FROM Orders WHERE NOT EXISTS (SELECT 1 FROM ShippedOrders WHERE ShippedOrders.id = Orders.id);
CSELECT Orders.* FROM Orders LEFT JOIN ShippedOrders ON Orders.id = ShippedOrders.id WHERE ShippedOrders.id IS NULL;
DSELECT * FROM Orders EXCEPT SELECT * FROM ShippedOrders;
Attempts:
2 left
💡 Hint
Consider how joins and subqueries perform on large tables.
🧠 Conceptual
expert
3:00remaining
Why does NOT IN sometimes produce unexpected results compared to NOT EXISTS in MySQL?
Consider these two queries to find rows in Products not in Discontinued:

1) SELECT * FROM Products WHERE id NOT IN (SELECT id FROM Discontinued);
2) SELECT * FROM Products WHERE NOT EXISTS (SELECT 1 FROM Discontinued WHERE Discontinued.id = Products.id);

Why might query 1 return no rows even if some products are not discontinued?
ABecause NOT IN returns no rows if the subquery returns any NULL values.
BBecause NOT EXISTS is slower and less reliable than NOT IN.
CBecause NOT IN only works with numeric columns.
DBecause NOT EXISTS requires indexes to work correctly.
Attempts:
2 left
💡 Hint
Think about how NULL values affect NOT IN.

Practice

(1/5)
1. What does the SQL EXCEPT operation do?
easy
A. Returns rows from the first query that are not in the second query
B. Returns rows common to both queries
C. Returns all rows from both queries combined
D. Deletes rows from the first table

Solution

  1. Step 1: Understand the purpose of EXCEPT

    The EXCEPT operation returns rows that appear in the first query but do not appear in the second query.
  2. Step 2: Compare with other set operations

    Unlike INTERSECT which returns common rows, EXCEPT excludes rows found in the second set.
  3. Final Answer:

    Returns rows from the first query that are not in the second query -> Option A
  4. Quick Check:

    EXCEPT = unique rows in first set [OK]
Hint: EXCEPT means "first minus second" rows [OK]
Common Mistakes:
  • Confusing EXCEPT with INTERSECT
  • Thinking EXCEPT returns all rows combined
  • Assuming EXCEPT deletes data
2. Which of the following is the correct MySQL syntax to simulate EXCEPT using NOT EXISTS?
easy
A. SELECT * FROM table1 WHERE NOT EXISTS (SELECT * FROM table2 WHERE table2.id = table1.id);
B. SELECT * FROM table1 EXCEPT SELECT * FROM table2;
C. SELECT * FROM table1 JOIN table2 ON table1.id = table2.id WHERE table2.id IS NULL;
D. SELECT * FROM table1 WHERE id IN (SELECT id FROM table2);

Solution

  1. Step 1: Identify how NOT EXISTS works

    NOT EXISTS checks if a row in table1 has no matching row in table2 based on the condition.
  2. Step 2: Match syntax to EXCEPT behavior

    The query selects rows from table1 where no matching id exists in table2, simulating EXCEPT.
  3. Final Answer:

    SELECT * FROM table1 WHERE NOT EXISTS (SELECT * FROM table2 WHERE table2.id = table1.id); -> Option A
  4. Quick Check:

    NOT EXISTS filters out matching rows [OK]
Hint: Use NOT EXISTS with matching condition for EXCEPT [OK]
Common Mistakes:
  • Using EXCEPT directly in MySQL (unsupported)
  • Confusing JOIN with NOT EXISTS logic
  • Using IN instead of NOT EXISTS
3. Given tables:
table1(id): 1, 2, 3, 4
table2(id): 3, 4, 5
What is the result of this query?
SELECT id FROM table1 WHERE NOT EXISTS (SELECT 1 FROM table2 WHERE table2.id = table1.id);
medium
A. [3, 4]
B. [1, 2]
C. [5]
D. [1, 2, 3, 4, 5]

Solution

  1. Step 1: Identify ids in table1 not in table2

    table1 has 1,2,3,4; table2 has 3,4,5. IDs 1 and 2 are missing in table2.
  2. Step 2: Understand NOT EXISTS filtering

    The query returns ids from table1 where no matching id exists in table2, so 1 and 2 are returned.
  3. Final Answer:

    [1, 2] -> Option B
  4. Quick Check:

    table1 minus table2 = [1, 2] [OK]
Hint: Find ids in first table missing in second [OK]
Common Mistakes:
  • Including ids present in both tables
  • Confusing NOT EXISTS with EXISTS
  • Returning ids from table2 instead
4. Identify the error in this MySQL query intended to simulate EXCEPT:
SELECT * FROM table1 LEFT JOIN table2 ON table1.id = table2.id WHERE table2.id = NULL;
medium
A. LEFT JOIN should be INNER JOIN
B. Missing ON condition in JOIN
C. Using '=' to compare with NULL instead of IS NULL
D. No error, query is correct

Solution

  1. Step 1: Understand NULL comparison in SQL

    In SQL, NULL cannot be compared using '='; use IS NULL instead.
  2. Step 2: Identify the mistake in WHERE clause

    The condition table2.id = NULL is always false; it should be table2.id IS NULL to find unmatched rows.
  3. Final Answer:

    Using '=' to compare with NULL instead of IS NULL -> Option C
  4. Quick Check:

    NULL comparison requires IS NULL [OK]
Hint: Use IS NULL, not = NULL, to check for NULL values [OK]
Common Mistakes:
  • Using '=' instead of IS NULL
  • Confusing JOIN types
  • Omitting ON clause in JOIN
5. You want to find customers who placed orders last month but did NOT place orders this month. Which query correctly simulates EXCEPT in MySQL?
hard
A. SELECT customer_id FROM orders LEFT JOIN orders o2 ON orders.customer_id = o2.customer_id WHERE o2.order_date BETWEEN '2024-06-01' AND '2024-06-30';
B. SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-06-01' AND '2024-06-30' EXCEPT SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-05-01' AND '2024-05-31';
C. SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-05-01' AND '2024-06-30';
D. SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-05-01' AND '2024-05-31' AND NOT EXISTS (SELECT 1 FROM orders o2 WHERE o2.customer_id = orders.customer_id AND o2.order_date BETWEEN '2024-06-01' AND '2024-06-30');

Solution

  1. Step 1: Identify the time frames and logic

    We want customers who ordered in May but not in June.
  2. Step 2: Use NOT EXISTS to exclude June customers

    The query selects May customers and excludes those with June orders using NOT EXISTS subquery.
  3. Step 3: Check other options

    SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-06-01' AND '2024-06-30' EXCEPT SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-05-01' AND '2024-05-31'; uses EXCEPT which MySQL does not support; C returns all customers in both months; D returns June customers only.
  4. Final Answer:

    SELECT customer_id FROM orders WHERE order_date BETWEEN '2024-05-01' AND '2024-05-31' AND NOT EXISTS (SELECT 1 FROM orders o2 WHERE o2.customer_id = orders.customer_id AND o2.order_date BETWEEN '2024-06-01' AND '2024-06-30'); -> Option D
  5. Quick Check:

    NOT EXISTS excludes June orders [OK]
Hint: Use NOT EXISTS with date ranges to exclude recent orders [OK]
Common Mistakes:
  • Using EXCEPT directly in MySQL
  • Selecting all orders without filtering
  • Confusing LEFT JOIN logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard