0
0
SQLquery~20 mins

Why query patterns matter in SQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Query Pattern Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a JOIN with filtering
Given two tables, Employees and Departments, what is the output of the following query?
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';
A[{"name": "Bob", "department_name": "Marketing"}]
B[{"name": "Alice", "department_name": "Sales"}, {"name": "Carol", "department_name": "Sales"}]
C[{"name": "Alice", "department_name": "Sales"}, {"name": "Bob", "department_name": "Marketing"}, {"name": "Carol", "department_name": "Sales"}]
D[]
Attempts:
2 left
💡 Hint
Focus on the JOIN condition and the WHERE clause filtering by location.
📝 Syntax
intermediate
2: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;
ASELECT name, age FROM Users WHERE age > 20 ORDER BY;
BSELECT name, age FROM Users WHERE age > 20 ORDER BY age DESC;
CSELECT name, age FROM Users WHERE age > 20 ORDER BY 1;
DSELECT name, age FROM Users WHERE age > 20 ORDER BY age;
Attempts:
2 left
💡 Hint
ORDER BY must specify a column or expression to sort by.
optimization
advanced
2: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?
ASELECT customer_id FROM Orders GROUP BY customer_id HAVING COUNT(*) > 0;
BSELECT DISTINCT customer_id, (SELECT COUNT(*) FROM Orders o2 WHERE o2.customer_id = o1.customer_id) FROM Orders o1;
CSELECT customer_id, COUNT(*) FROM Orders GROUP BY customer_id;
DSELECT customer_id, COUNT(order_id) FROM Orders WHERE order_id IS NOT NULL GROUP BY customer_id;
Attempts:
2 left
💡 Hint
Consider how many times the Orders table is scanned in each query.
🔧 Debug
advanced
2:00remaining
Why does this query return no rows?
Given tables Products and Sales, why does this query return no rows?
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';
ABecause the WHERE clause filters out rows where s.sale_date is NULL, removing unmatched products.
BBecause LEFT JOIN requires an ON condition that matches all rows, which is missing.
CBecause Products table is empty.
DBecause sale_date column does not exist in Sales table.
Attempts:
2 left
💡 Hint
Think about how WHERE affects rows with NULLs from LEFT JOIN.
🧠 Conceptual
expert
2: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?
ABecause databases ignore query patterns and optimize all queries to the same plan automatically.
BBecause all queries are executed the same way internally, so patterns do not affect performance.
CBecause query patterns only affect the order of results, not the speed of execution.
DBecause query patterns determine how the database engine uses indexes, joins, and scans, affecting speed and resource use.
Attempts:
2 left
💡 Hint
Think about how databases process queries and use indexes.