Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using Subquery with EXISTS Operator
📖 Scenario: You work for a bookstore database. The database has two tables: Books and Orders. You want to find all books that have been ordered at least once.
🎯 Goal: Build a SQL query using the EXISTS operator with a subquery to list all books that have orders.
📋 What You'll Learn
Create a Books table with columns BookID (integer) and Title (text).
Create an Orders table with columns OrderID (integer) and BookID (integer).
Write a query using EXISTS with a subquery to select all books that have at least one order.
Use exact table and column names as specified.
💡 Why This Matters
🌍 Real World
Bookstores and many businesses use databases to track products and orders. Checking if a product has been ordered is a common task.
💼 Career
Understanding subqueries and EXISTS is important for database querying roles, data analysis, and backend development.
Progress0 / 4 steps
1
Create the Books table with sample data
Create a table called Books with columns BookID (integer) and Title (text). Insert these exact rows: (1, 'Learn SQL'), (2, 'Database Basics'), (3, 'Advanced SQL').
SQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Create the Orders table with sample data
Create a table called Orders with columns OrderID (integer) and BookID (integer). Insert these exact rows: (101, 1), (102, 3).
SQL
Hint
Use CREATE TABLE and INSERT INTO like in Step 1.
3
Write the query using EXISTS subquery
Write a SQL query to select BookID and Title from Books where there exists at least one row in Orders with the same BookID. Use the EXISTS operator with a subquery.
SQL
Hint
Use WHERE EXISTS (SELECT 1 FROM Orders WHERE Orders.BookID = Books.BookID) to check for matching orders.
4
Complete the query with ordering
Add an ORDER BY Title clause at the end of the query to sort the results alphabetically by book title.
SQL
Hint
Use ORDER BY Title at the end of the query to sort results.
Practice
(1/5)
1. What does the EXISTS operator do in an SQL query?
easy
A. Checks if a subquery returns any rows and returns TRUE or FALSE.
B. Counts the 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 operator checks if the subquery returns at least one row.
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 rows found [OK]
Hint: EXISTS means "is there at least one row?" [OK]
Common Mistakes:
Thinking EXISTS counts rows instead of checking existence
Confusing EXISTS with JOIN operations
Assuming EXISTS deletes or modifies data
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 LIKE (SELECT column FROM table2);
Solution
Step 1: Review correct EXISTS syntax
EXISTS is used as EXISTS (subquery) without any operator like =, IN, or LIKE.
Step 2: Identify incorrect options
Options B, C, and D misuse operators (=, IN, LIKE) with EXISTS, causing syntax errors.
Final Answer:
SELECT * FROM table WHERE EXISTS (SELECT column FROM table2); -> Option A
Quick Check:
EXISTS uses only parentheses for subquery [OK]
Hint: EXISTS always followed by (subquery) without operators [OK]
Common Mistakes:
Adding = or IN after EXISTS
Using LIKE with EXISTS
Forgetting parentheses around subquery
3. Given tables Customers(id, name) and Orders(id, customer_id), what does this query return?
SELECT name FROM Customers c WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.customer_id = c.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 incorrect subquery.
Solution
Step 1: Understand the EXISTS subquery condition
The subquery checks if there is at least one order with customer_id matching the customer's id.
Step 2: Determine which customers are returned
Only customers with matching orders exist, so only those customers' names are returned.
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 with matching related data [OK]
Common Mistakes:
Thinking it returns all customers regardless of orders
Confusing with JOIN that returns all orders
Assuming syntax error due to subquery
4. Identify the error in this query:
SELECT * FROM Employees e WHERE EXISTS SELECT * FROM Salaries s WHERE s.emp_id = e.id;
medium
A. EXISTS cannot be used in WHERE clause.
B. Using SELECT * inside EXISTS is not allowed.
C. Missing parentheses around the subquery after EXISTS.
D. The alias 'e' is not defined.
Solution
Step 1: Check EXISTS syntax
EXISTS requires the subquery to be enclosed in parentheses.
Step 2: Identify the missing parentheses
The query lacks parentheses around the subquery after EXISTS, causing a syntax error.
Final Answer:
Missing parentheses around the subquery after EXISTS. -> Option C
Quick Check:
EXISTS (subquery) needs parentheses [OK]
Hint: Always put parentheses around subquery after EXISTS [OK]
Common Mistakes:
Omitting parentheses around subquery
Thinking SELECT * is invalid inside EXISTS
Misunderstanding alias 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 find these products?
hard
A. SELECT name FROM Products p WHERE NOT IN (SELECT product_id FROM Orders);
B. SELECT name FROM Products p WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.product_id = p.id);
C. SELECT name FROM Products p WHERE EXISTS NOT (SELECT 1 FROM Orders o WHERE o.product_id = p.id);
D. SELECT name FROM Products p WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.product_id = p.id);
Solution
Step 1: Understand the goal
We want products with no matching orders, so the subquery should check for orders and we want those where no such orders exist.
Step 2: Use NOT EXISTS correctly
NOT EXISTS with a subquery checking orders for the product returns products never ordered.
Step 3: Evaluate other options
SELECT name FROM Products p WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.product_id = p.id); returns products with orders, C has invalid syntax, D uses NOT IN which can cause issues with NULLs.
Final Answer:
SELECT name FROM Products p WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.product_id = p.id); -> Option D
Quick Check:
NOT EXISTS finds missing related rows [OK]
Hint: Use NOT EXISTS to find items with no related rows [OK]
Common Mistakes:
Using EXISTS instead of NOT EXISTS for missing data