What if you could instantly know if something exists without searching everything?
Why Subqueries with EXISTS in MySQL? - Purpose & Use Cases
Imagine you have a big list of customers and a separate list of orders. You want to find which customers have made at least one order. Doing this by checking each customer manually against every order is like flipping through two huge phone books page by page.
Manually comparing each customer to every order is slow and tiring. It's easy to make mistakes, miss some matches, or waste a lot of time repeating the same checks over and over.
Using Subqueries with EXISTS lets you quickly ask: 'Does this customer have any orders?' without scanning everything fully. It's like having a smart assistant who instantly says yes or no, saving you time and effort.
SELECT customer_id FROM customers;
-- Then for each customer, check orders manuallySELECT customer_id FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.customer_id);This lets you efficiently find related data by asking simple yes/no questions inside your queries, making your database work smarter.
A store wants to send a thank-you email only to customers who have placed orders. Using EXISTS, they can quickly find those customers without scanning all orders repeatedly.
Manual checks are slow and error-prone.
EXISTS lets you ask if related data exists efficiently.
This makes queries faster and easier to write.
Practice
EXISTS keyword do in a MySQL query?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 CQuick Check:
EXISTS means row presence check = B [OK]
- Confusing EXISTS with COUNT
- Thinking EXISTS deletes rows
- Mixing EXISTS with JOIN
EXISTS in a WHERE clause?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 DQuick Check:
EXISTS needs parentheses around subquery = C [OK]
- Omitting parentheses after EXISTS
- Using '=' or 'IN' with EXISTS
- Writing subquery without parentheses
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);
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 BQuick Check:
EXISTS filters customers with orders = D [OK]
- Thinking it returns all customers
- Confusing orders with customers
- Assuming syntax error in subquery
SELECT * FROM employees WHERE EXISTS SELECT * FROM departments WHERE departments.id = employees.dept_id;
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 AQuick Check:
EXISTS needs parentheses = A [OK]
- Omitting parentheses after EXISTS
- Thinking SELECT * is invalid inside EXISTS
- Misunderstanding WHERE clause usage
products(id, name) and orders(id, product_id), which query correctly uses EXISTS to achieve this?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 AQuick Check:
NOT EXISTS filters products without orders = A [OK]
- Using EXISTS instead of NOT EXISTS
- Comparing wrong columns in subquery
- Confusing product_id with order id
Ready to prove it?
Pass the test and this pattern moves unknown
