0
0
SQLquery~20 mins

Subquery in WHERE clause in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subquery Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find employees with salary above average
Given a table Employees with columns id, name, and salary, what is the output of this query?
SELECT name FROM Employees WHERE salary > (SELECT AVG(salary) FROM Employees);
SQL
SELECT name FROM Employees WHERE salary > (SELECT AVG(salary) FROM Employees);
AAll employee names regardless of salary
BList of employee names whose salary is less than the average salary
CList of employee names whose salary is greater than the average salary
DSyntax error due to subquery in WHERE clause
Attempts:
2 left
💡 Hint
Think about what the subquery returns and how it is used in the WHERE clause.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in subquery usage
Which option contains a syntax error when using a subquery in the WHERE clause to find products priced higher than the average price?
SELECT product_name FROM Products WHERE price > (SELECT AVG(price) FROM Products);
SQL
SELECT product_name FROM Products WHERE price > (SELECT AVG(price) FROM Products);
ASELECT product_name FROM Products WHERE price > (SELECT AVG(price) FROM Products);
BSELECT product_name FROM Products WHERE price > SELECT AVG(price) FROM Products;
C;)stcudorP MORF )ecirp(GVA TCELES( > ecirp EREHW stcudorP MORF eman_tcudorp TCELES
DELECT product_name FROM Products WHERE price > (SELECT AVG(price) FROM Products);
Attempts:
2 left
💡 Hint
Check if the subquery is properly enclosed in parentheses.
optimization
advanced
3:00remaining
Optimize query with subquery in WHERE clause
Consider this query to find customers who placed orders with total amount greater than the average order amount:
SELECT customer_id FROM Orders WHERE total_amount > (SELECT AVG(total_amount) FROM Orders);

Which option improves performance by avoiding repeated subquery execution?
SQL
SELECT customer_id FROM Orders WHERE total_amount > (SELECT AVG(total_amount) FROM Orders);
AAdd an index on total_amount column to speed up the subquery
BRewrite as:<br>SELECT customer_id FROM Orders WHERE total_amount &gt; AVG(total_amount);
CUse a correlated subquery:<br>SELECT customer_id FROM Orders WHERE total_amount &gt; (SELECT AVG(total_amount) FROM Orders WHERE Orders.customer_id = customer_id);
DUse a CTE to calculate average once:<br>WITH avg_amount AS (SELECT AVG(total_amount) AS avg_val FROM Orders)<br>SELECT customer_id FROM Orders, avg_amount WHERE total_amount > avg_val;
Attempts:
2 left
💡 Hint
Calculate the average once and reuse it.
🔧 Debug
advanced
2:30remaining
Debug why query returns no rows
Given tables Orders(order_id, customer_id, amount) and Customers(customer_id, name), this query returns no rows:
SELECT name FROM Customers WHERE customer_id IN (SELECT customer_id FROM Orders WHERE amount > 1000);

What is the most likely reason?
SQL
SELECT name FROM Customers WHERE customer_id IN (SELECT customer_id FROM Orders WHERE amount > 1000);
ANo orders have amount greater than 1000, so subquery returns empty set
Bcustomer_id is not a valid column in Orders table
CThe subquery should use EXISTS instead of IN
DThe main query should join Customers and Orders instead of using subquery
Attempts:
2 left
💡 Hint
Check the data in Orders table for amounts over 1000.
🧠 Conceptual
expert
3:00remaining
Understanding subquery behavior in WHERE clause
What happens if a subquery in the WHERE clause returns NULL when used with a comparison operator like > ?
Example:
SELECT * FROM Products WHERE price > (SELECT MAX(discount) FROM Discounts WHERE product_id = Products.id);

Assuming some products have no matching discount rows, what is the effect on those products in the result?
SQL
SELECT * FROM Products WHERE price > (SELECT MAX(discount) FROM Discounts WHERE product_id = Products.id);
AProducts with no matching discount rows are excluded because comparison with NULL yields false
BProducts with no matching discount rows are included because NULL is treated as zero
CQuery raises an error due to NULL in subquery result
DProducts with no matching discount rows are included because NULL is ignored
Attempts:
2 left
💡 Hint
Remember how SQL treats NULL in comparisons.