0
0
SQLquery~20 mins

CTE as readable subquery replacement in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CTE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of CTE vs Subquery for Average Salary
Given the table employees with columns id, department, and salary, what is the output of the following query using a CTE that calculates the average salary per department and then selects departments with average salary above 5000?
SQL
WITH dept_avg AS (
  SELECT department, AVG(salary) AS avg_salary
  FROM employees
  GROUP BY department
)
SELECT department
FROM dept_avg
WHERE avg_salary > 5000
ORDER BY department;
AList of departments where the average salary is greater than 5000, sorted alphabetically
BList of all employees with salary above 5000, sorted by department
CList of departments with total salary above 5000, sorted alphabetically
DSyntax error due to missing GROUP BY clause
Attempts:
2 left
💡 Hint
Think about what the CTE calculates first and what the main query filters.
📝 Syntax
intermediate
2:00remaining
Identify the Syntax Error in CTE Usage
Which option contains a syntax error in the use of a CTE?
SQL
WITH dept_count AS (
  SELECT department, COUNT(*) AS emp_count
  FROM employees
  GROUP BY department
)
SELECT * FROM dept_count WHERE emp_count > 10;
AWITH dept_count AS (SELECT department, COUNT(*) AS emp_count FROM employees GROUP BY department) SELECT * FROM dept_count WHERE emp_count > '10';
BWITH dept_count AS (SELECT department, COUNT(*) AS emp_count FROM employees GROUP BY department) SELECT * FROM dept_count WHERE emp_count > 10;
CWITH dept_count AS SELECT department, COUNT(*) AS emp_count FROM employees GROUP BY department SELECT * FROM dept_count WHERE emp_count > 10;
D;01 > tnuoc_pme EREHW tnuoc_tped MORF * TCELES )tnemtraped YB PUORG seeyolpme MORF tnuoc_pme SA )*(TNUOC ,tnemtraped TCELES( SA tnuoc_tped HTIW
Attempts:
2 left
💡 Hint
Check the parentheses around the CTE definition.
optimization
advanced
2:00remaining
Optimizing Query with CTE vs Subquery
Consider a query that calculates total sales per region and then filters regions with sales above 10000. Which option shows the best use of a CTE to improve readability and avoid repeating the aggregation?
AWITH region_sales AS SELECT region, SUM(sales) AS total_sales FROM sales_data GROUP BY region SELECT region FROM region_sales WHERE total_sales > 10000;
BSELECT region FROM (SELECT region, SUM(sales) AS total_sales FROM sales_data GROUP BY region) WHERE total_sales > 10000;
CSELECT region FROM sales_data GROUP BY region HAVING SUM(sales) > 10000;
DWITH region_sales AS (SELECT region, SUM(sales) AS total_sales FROM sales_data GROUP BY region) SELECT region FROM region_sales WHERE total_sales > 10000;
Attempts:
2 left
💡 Hint
Look for correct CTE syntax and clear separation of steps.
🔧 Debug
advanced
2:00remaining
Debugging Incorrect CTE Result
A developer wrote this query to find departments with more than 5 employees, but it returns no rows. What is the likely cause?
SQL
WITH dept_emps AS (
  SELECT department, COUNT(employee_id) AS emp_count
  FROM employees
)
SELECT department FROM dept_emps WHERE emp_count > 5;
AMissing GROUP BY clause in the CTE causing emp_count to be a total count, not per department
BIncorrect alias name emp_count used in WHERE clause
CThe WHERE clause should be HAVING clause
DCTE is missing a closing parenthesis
Attempts:
2 left
💡 Hint
Check if aggregation is grouped properly.
🧠 Conceptual
expert
2:00remaining
Why Use CTE Instead of Subquery?
Which of the following is the best explanation for why using a CTE can be better than a subquery in complex SQL queries?
ACTEs always run faster than subqueries because they are cached automatically by the database engine.
BCTEs improve query readability by naming intermediate results and can be referenced multiple times, avoiding repetition.
CCTEs allow modifying data directly inside the query, unlike subqueries which are read-only.
DCTEs eliminate the need for indexes on tables used in the query.
Attempts:
2 left
💡 Hint
Think about how CTEs help organize complex queries.