What if you could instantly know if something exists without searching everything?
Why Subqueries with EXISTS in PostgreSQL? - Purpose & Use Cases
Imagine you have a big list of customers and a separate list of orders. You want to find all customers who have made at least one order. Doing this by checking each customer against every order manually 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, like missing some matches or checking the same pairs over and over. This wastes time and can give wrong answers.
Using Subqueries with EXISTS lets the database quickly check if at least one matching order exists for each customer. It stops searching as soon as it finds one match, making the process fast and reliable.
SELECT customer_id FROM customers WHERE customer_id IN (SELECT customer_id FROM orders);
SELECT customer_id FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.customer_id);This lets you efficiently find related data without scanning everything, making queries faster and easier to write.
A store wants to send a thank-you email only to customers who have placed orders. Using EXISTS, they quickly find those customers without checking every order manually.
Manual checks are slow and error-prone.
EXISTS stops searching once a match is found, saving time.
It helps find related data efficiently and clearly.
Practice
EXISTS keyword do in a SQL query?Solution
Step 1: Understand the purpose of EXISTS
TheEXISTSkeyword is used to check if a subquery returns any rows. It returns true if there is at least one row, otherwise false.Step 2: Compare with other options
Counting rows, joining tables, or deleting rows are different SQL operations unrelated toEXISTS.Final Answer:
Checks if a subquery returns any rows and returns true or false -> Option AQuick Check:
EXISTS = true if subquery has rows [OK]
- Confusing EXISTS with COUNT
- Thinking EXISTS joins tables
- Assuming EXISTS deletes data
EXISTS in a WHERE clause?Solution
Step 1: Check correct EXISTS syntax
The correct syntax requiresEXISTSfollowed by a subquery enclosed in parentheses.Step 2: Validate each option
WHERE EXISTS (SELECT * FROM orders WHERE customer_id = customers.id) uses parentheses correctly. The other options have syntax errors or incorrect keywords.Final Answer:
WHERE EXISTS (SELECT * FROM orders WHERE customer_id = customers.id) -> Option CQuick Check:
EXISTS needs parentheses around subquery [OK]
- Omitting parentheses after EXISTS
- Using '=' or 'IN' with EXISTS
- Writing subquery outside 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 withcustomer_idmatching the customer'sid.Step 2: Determine the main query result
The main query selects customer names only if the subquery returns rows, meaning the customer has orders.Final Answer:
All customers who have placed at least one order -> Option BQuick Check:
EXISTS filters customers with orders [OK]
- Thinking it returns all customers
- Confusing with JOIN returning orders
- Assuming syntax error in subquery
SELECT id FROM customers WHERE EXISTS SELECT * FROM orders WHERE orders.customer_id = customers.id;
Solution
Step 1: Check EXISTS syntax
TheEXISTSkeyword must be followed by a subquery enclosed in parentheses.Step 2: Identify the error in the query
The query misses parentheses around the subquery, causing a syntax error.Final Answer:
Missing parentheses around the subquery after EXISTS -> Option DQuick Check:
EXISTS requires parentheses around subquery [OK]
- Omitting parentheses after EXISTS
- Thinking SELECT * is invalid in EXISTS
- Confusing alias usage
EXISTS to achieve this?A) SELECT name FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.id AND order_date >= CURRENT_DATE - INTERVAL '30 days'); B) SELECT name FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.id AND order_date <= CURRENT_DATE - INTERVAL '30 days'); C) SELECT name FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.id); D) SELECT name FROM customers WHERE EXISTS (SELECT 1 FROM orders WHERE order_date >= CURRENT_DATE - INTERVAL '30 days');
Solution
Step 1: Understand the date filter requirement
We want customers with orders placed in the last 30 days, so the order date must be >= current date minus 30 days.Step 2: Analyze each query option
Select customers with orders in last 30 days using correct date filter correctly filters orders by customer and date. Select customers with orders older than 30 days filters orders older than 30 days. Select customers with any orders regardless of date ignores date. Select customers if any order in last 30 days ignoring customer link ignores customer link.Final Answer:
Select customers with orders in last 30 days using correct date filter -> Option AQuick Check:
EXISTS with date filter and customer match [OK]
- Using wrong date comparison operator
- Forgetting to link orders to customers
- Filtering orders without customer condition
Ready to prove it?
Pass the test and this pattern moves unknown
