EXISTS checks if a subquery returns any rows. It returns TRUE if the subquery has at least one row, otherwise FALSE.
Click to reveal answer
intermediate
How does a subquery with EXISTS differ from a normal JOIN?
EXISTS only checks for the presence of rows in the subquery and returns TRUE or FALSE, while JOIN combines rows from two tables based on a condition.
Click to reveal answer
beginner
Write a simple example of a query using EXISTS to find customers who have placed orders.
SELECT * FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id);
Click to reveal answer
intermediate
Why might you use EXISTS instead of IN in a subquery?
EXISTS can be more efficient because it stops searching as soon as it finds a matching row, while IN may scan all rows. EXISTS also handles NULLs better.
Click to reveal answer
beginner
Can EXISTS subqueries return data? Explain.
No, EXISTS subqueries do not return data themselves. They only return TRUE or FALSE to indicate if the subquery found any rows.
Click to reveal answer
What does the EXISTS keyword return in a SQL query?
AAll rows from the subquery
BTRUE if subquery returns at least one row
CThe count of rows in the subquery
DNULL if subquery returns no rows
✗ Incorrect
EXISTS returns TRUE if the subquery returns any rows, otherwise FALSE.
Which of the following is a valid use of EXISTS?
ASELECT * FROM table1 WHERE EXISTS (SELECT * FROM table2 WHERE condition);
BSELECT EXISTS FROM table1;
CEXISTS SELECT * FROM table1;
DSELECT * FROM table1 WHERE EXISTS = 1;
✗ Incorrect
EXISTS is used with a subquery inside WHERE clause to check for row existence.
Why might EXISTS be preferred over IN in some cases?
AIN ignores NULL values
BIN is faster than EXISTS
CEXISTS returns all matching rows
DEXISTS stops searching after finding one match
✗ Incorrect
EXISTS can be more efficient because it stops after finding the first matching row.
What does the subquery inside EXISTS typically return?
ANULL values only
BA specific column to display
CAny column or 1, since only existence matters
DThe total count of rows
✗ Incorrect
The subquery inside EXISTS usually returns 1 or any column because only the presence of rows matters.
If a subquery inside EXISTS returns no rows, what is the result?
AFALSE
BTRUE
CAn error
DNULL
✗ Incorrect
EXISTS returns FALSE if the subquery returns no rows.
Explain how the EXISTS keyword works in SQL and give a simple example.
Think about checking if something exists rather than retrieving data.
You got /3 concepts.
Describe a scenario where using EXISTS is better than using a JOIN or IN clause.
Consider when you only need to know if related data exists.
You got /4 concepts.
Practice
(1/5)
1. What does the EXISTS keyword do in a MySQL query?
easy
A. Counts the total number of rows in a table
B. Deletes rows from a table based on a condition
C. Checks if a subquery returns any rows and returns TRUE or FALSE
D. Joins two tables together
Solution
Step 1: Understand the purpose of EXISTS
EXISTS checks if the subquery returns at least one row.
Step 2: Compare with other options
Counting rows, deleting rows, and joining tables are different operations.
Final Answer:
Checks if a subquery returns any rows and returns TRUE or FALSE -> Option C
Quick Check:
EXISTS means row presence check = B [OK]
Hint: EXISTS means "is there at least one row?" [OK]
Common Mistakes:
Confusing EXISTS with COUNT
Thinking EXISTS deletes rows
Mixing EXISTS with JOIN
2. Which of the following is the correct syntax to use EXISTS in a WHERE clause?
easy
A. SELECT * FROM table WHERE EXISTS = (SELECT column FROM table2);
B. SELECT * FROM table WHERE EXISTS SELECT column FROM table2;
C. SELECT * FROM table WHERE EXISTS IN (SELECT column FROM table2);
D. SELECT * FROM table WHERE EXISTS (SELECT column FROM table2);
Solution
Step 1: Recall EXISTS syntax
EXISTS must be followed by a subquery in parentheses.
Step 2: Check each option
SELECT * FROM table WHERE EXISTS (SELECT column FROM table2); uses correct syntax with parentheses; others have syntax errors.
Final Answer:
SELECT * FROM table WHERE EXISTS (SELECT column FROM table2); -> Option D
Quick Check:
EXISTS needs parentheses around subquery = C [OK]
Hint: EXISTS always uses parentheses for the subquery [OK]
Common Mistakes:
Omitting parentheses after EXISTS
Using '=' or 'IN' with EXISTS
Writing subquery without parentheses
3. Given tables customers(id, name) and orders(id, customer_id), what does this query return?
SELECT name FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.id);
medium
A. An error because of invalid subquery
B. All customers who have placed at least one order
C. All orders with matching customer names
D. All customers including those without orders
Solution
Step 1: Understand the EXISTS subquery
The subquery checks if there is at least one order with customer_id matching customers.id.
Step 2: Interpret the WHERE clause effect
Only customers with matching orders exist, so only those customers are returned.
Final Answer:
All customers who have placed at least one order -> Option B
Quick Check:
EXISTS filters customers with orders = D [OK]
Hint: EXISTS filters rows with matching related data [OK]
Common Mistakes:
Thinking it returns all customers
Confusing orders with customers
Assuming syntax error in subquery
4. Identify the error in this query:
SELECT * FROM employees WHERE EXISTS SELECT * FROM departments WHERE departments.id = employees.dept_id;
medium
A. Missing parentheses around the subquery after EXISTS
B. Using SELECT * inside EXISTS is not allowed
C. The WHERE clause inside subquery is invalid
D. EXISTS cannot be used with multiple tables
Solution
Step 1: Check EXISTS syntax
EXISTS requires the subquery to be enclosed in parentheses.
Step 2: Verify other parts
SELECT * is allowed inside EXISTS; WHERE clause is valid; EXISTS works with multiple tables.
Final Answer:
Missing parentheses around the subquery after EXISTS -> Option A
Quick Check:
EXISTS needs parentheses = A [OK]
Hint: Always put subquery in parentheses after EXISTS [OK]
Common Mistakes:
Omitting parentheses after EXISTS
Thinking SELECT * is invalid inside EXISTS
Misunderstanding WHERE clause usage
5. You want to find all products that have never been ordered. Given tables products(id, name) and orders(id, product_id), which query correctly uses EXISTS to achieve this?
hard
A. SELECT name FROM products WHERE NOT EXISTS (SELECT 1 FROM orders WHERE orders.product_id = products.id);
B. SELECT name FROM products WHERE EXISTS (SELECT 1 FROM orders WHERE orders.product_id = products.id);
C. SELECT name FROM products WHERE EXISTS (SELECT 1 FROM orders WHERE orders.id = products.id);
D. SELECT name FROM products WHERE NOT EXISTS (SELECT 1 FROM orders WHERE orders.id = products.id);
Solution
Step 1: Understand the goal
We want products with no matching orders, so use NOT EXISTS with matching product_id.
Step 2: Analyze each option
SELECT name FROM products WHERE NOT EXISTS (SELECT 1 FROM orders WHERE orders.product_id = products.id); correctly uses NOT EXISTS with orders.product_id = products.id. SELECT name FROM products WHERE EXISTS (SELECT 1 FROM orders WHERE orders.product_id = products.id); finds products with orders. Options C and D compare wrong columns.
Final Answer:
SELECT name FROM products WHERE NOT EXISTS (SELECT 1 FROM orders WHERE orders.product_id = products.id); -> Option A
Quick Check:
NOT EXISTS filters products without orders = A [OK]
Hint: Use NOT EXISTS to find items with no related rows [OK]
Common Mistakes:
Using EXISTS instead of NOT EXISTS
Comparing wrong columns in subquery
Confusing product_id with order id
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.