Challenge - 5 Problems
Query Pattern Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a JOIN with filtering
Given two tables, Employees and Departments, what is the output of the following query?
Assume Employees:
1, Alice, 10
2, Bob, 20
3, Carol, 10
Departments:
10, Sales, New York
20, Marketing, Boston
SELECT e.name, d.department_name FROM Employees e JOIN Departments d ON e.department_id = d.id WHERE d.location = 'New York';
Assume Employees:
1, Alice, 10
2, Bob, 20
3, Carol, 10
Departments:
10, Sales, New York
20, Marketing, Boston
SQL
SELECT e.name, d.department_name FROM Employees e JOIN Departments d ON e.department_id = d.id WHERE d.location = 'New York';
Attempts:
2 left
💡 Hint
Focus on the JOIN condition and the WHERE clause filtering by location.
✗ Incorrect
The query joins Employees with Departments on department_id and filters departments located in New York. Only employees in Sales (id 10) are included, which are Alice and Carol.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in the query
Which option contains a syntax error in the SQL query?
SELECT name, age FROM Users WHERE age > 20 ORDER BY;
SQL
SELECT name, age FROM Users WHERE age > 20 ORDER BY;
Attempts:
2 left
💡 Hint
ORDER BY must specify a column or expression to sort by.
✗ Incorrect
Option A ends ORDER BY without specifying any column, causing a syntax error.
❓ optimization
advanced2:00remaining
Choose the most efficient query pattern for counting
You want to count how many orders each customer has placed. Which query pattern is the most efficient?
Attempts:
2 left
💡 Hint
Consider how many times the Orders table is scanned in each query.
✗ Incorrect
Option C uses GROUP BY with COUNT(*) scanning the table once. Option C uses a correlated subquery causing multiple scans. Option C only filters groups but does not count. Option C is similar to A but unnecessarily filters order_id IS NOT NULL which is redundant if order_id is always present.
🔧 Debug
advanced2:00remaining
Why does this query return no rows?
Given tables Products and Sales, why does this query return no rows?
Assume some products have no sales.
SELECT p.product_name FROM Products p LEFT JOIN Sales s ON p.id = s.product_id WHERE s.sale_date > '2023-01-01';
Assume some products have no sales.
SQL
SELECT p.product_name FROM Products p LEFT JOIN Sales s ON p.id = s.product_id WHERE s.sale_date > '2023-01-01';
Attempts:
2 left
💡 Hint
Think about how WHERE affects rows with NULLs from LEFT JOIN.
✗ Incorrect
The WHERE clause filters on s.sale_date > '2023-01-01'. For products without sales, s.sale_date is NULL, so the condition fails and those rows are excluded, negating the LEFT JOIN effect.
🧠 Conceptual
expert2:00remaining
Why do query patterns affect database performance?
Which explanation best describes why different SQL query patterns can lead to very different performance even if they return the same results?
Attempts:
2 left
💡 Hint
Think about how databases process queries and use indexes.
✗ Incorrect
Different query patterns can cause the database to choose different execution plans, use or ignore indexes, perform joins differently, and scan more or fewer rows, impacting performance.