📖 Scenario: You work at a small online store. You have two tables: customers and orders. You want to find which customers have placed at least one order.
🎯 Goal: Build a SQL query using a subquery with EXISTS to list all customers who have made orders.
📋 What You'll Learn
Create a customers table with columns customer_id and customer_name.
Create an orders table with columns order_id and customer_id.
Insert the exact customers and orders data as specified.
Write a SQL query using EXISTS to find customers with orders.
💡 Why This Matters
🌍 Real World
Stores and businesses often need to find customers who have made purchases to send promotions or analyze sales.
💼 Career
Knowing how to use EXISTS subqueries is important for database querying tasks in data analysis, backend development, and reporting.
Progress0 / 4 steps
1
Create the customers table and insert data
Write SQL statements to create a table called customers with columns customer_id (integer) and customer_name (text). Then insert these exact rows: (1, 'Alice'), (2, 'Bob'), (3, 'Charlie').
PostgreSQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Create the orders table and insert data
Write SQL statements to create a table called orders with columns order_id (integer) and customer_id (integer). Then insert these exact rows: (101, 1), (102, 1), (103, 3).
PostgreSQL
Hint
Use CREATE TABLE and INSERT INTO like before, matching the exact columns and values.
3
Write the query using EXISTS to find customers with orders
Write a SQL query that selects customer_id and customer_name from customers where there exists at least one row in orders with the same customer_id. Use a subquery with EXISTS.
PostgreSQL
Hint
Use WHERE EXISTS (SELECT 1 FROM orders WHERE orders.customer_id = customers.customer_id) to check if the customer has any orders.
4
Complete the query with ordering by customer_name
Add an ORDER BY clause to the query to sort the results by customer_name in ascending order.
PostgreSQL
Hint
Use ORDER BY customer_name ASC after the WHERE EXISTS clause.
Practice
(1/5)
1. What does the EXISTS keyword do in a SQL query?
easy
A. Checks if a subquery returns any rows and returns true or false
B. Counts the total number of rows in a table
C. Joins two tables based on a condition
D. Deletes rows from a table
Solution
Step 1: Understand the purpose of EXISTS
The EXISTS keyword 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 to EXISTS.
Final Answer:
Checks if a subquery returns any rows and returns true or false -> Option A
Quick Check:
EXISTS = true if subquery has rows [OK]
Hint: EXISTS means: 'Is there at least one row?' [OK]
Common Mistakes:
Confusing EXISTS with COUNT
Thinking EXISTS joins tables
Assuming EXISTS deletes data
2. Which of the following is the correct syntax to use EXISTS in a WHERE clause?
easy
A. WHERE EXISTS IN (SELECT * FROM orders WHERE customer_id = customers.id)
B. WHERE EXISTS SELECT * FROM orders WHERE customer_id = customers.id
C. WHERE EXISTS (SELECT * FROM orders WHERE customer_id = customers.id)
D. WHERE EXISTS = (SELECT * FROM orders WHERE customer_id = customers.id)
Solution
Step 1: Check correct EXISTS syntax
The correct syntax requires EXISTS followed 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 C
Quick Check:
EXISTS needs parentheses around subquery [OK]
Hint: EXISTS always followed by parentheses with a subquery [OK]
Common Mistakes:
Omitting parentheses after EXISTS
Using '=' or 'IN' with EXISTS
Writing subquery outside 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. All customers including those without orders
B. All customers who have placed at least one order
C. All orders with customer names
D. An error because of invalid subquery
Solution
Step 1: Understand the EXISTS subquery
The subquery checks if there is at least one order with customer_id matching the customer's id.
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 B
Quick Check:
EXISTS filters customers with orders [OK]
Hint: EXISTS filters rows having matching related rows [OK]
Common Mistakes:
Thinking it returns all customers
Confusing with JOIN returning orders
Assuming syntax error in subquery
4. Identify the error in this query:
SELECT id FROM customers WHERE EXISTS SELECT * FROM orders WHERE orders.customer_id = customers.id;
medium
A. Incorrect table alias usage
B. Using SELECT * instead of SELECT 1 in EXISTS
C. Missing WHERE clause in subquery
D. Missing parentheses around the subquery after EXISTS
Solution
Step 1: Check EXISTS syntax
The EXISTS keyword 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 D
Quick Check:
EXISTS requires parentheses around subquery [OK]
Hint: Always put parentheses after EXISTS [OK]
Common Mistakes:
Omitting parentheses after EXISTS
Thinking SELECT * is invalid in EXISTS
Confusing alias usage
5. You want to find all customers who have placed orders only in the last 30 days. Which query correctly uses 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');
hard
A. Select customers with orders in last 30 days using correct date filter
B. Select customers with orders older than 30 days
C. Select customers with any orders regardless of date
D. Select customers if any order in last 30 days ignoring customer link
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 A
Quick Check:
EXISTS with date filter and customer match [OK]
Hint: Add date condition inside EXISTS subquery with customer match [OK]
Common Mistakes:
Using wrong date comparison operator
Forgetting to link orders to customers
Filtering orders without customer condition
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.