0
0
SQLquery~20 mins

Why CTEs are needed in SQL - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
Why use Common Table Expressions (CTEs)?

Which of the following best explains why CTEs are useful in SQL queries?

ACTEs store data permanently in the database for faster access.
BCTEs allow breaking complex queries into simpler parts for better readability and reuse.
CCTEs replace the need for any JOIN operations in SQL queries.
DCTEs automatically speed up query execution by creating indexes on the fly.
Attempts:
2 left
💡 Hint

Think about how CTEs help organize and structure queries.

query_result
intermediate
2:30remaining
Output of a query using a CTE

Given the following tables:

Employees: (id, name, department_id)
Departments: (id, name)

What is the output of this query?

WITH DeptCount AS (
  SELECT department_id, COUNT(*) AS emp_count
  FROM Employees
  GROUP BY department_id
)
SELECT d.name, dc.emp_count
FROM Departments d
JOIN DeptCount dc ON d.id = dc.department_id
ORDER BY dc.emp_count DESC;
SQL
Employees:
1, 'Alice', 10
2, 'Bob', 20
3, 'Charlie', 10
4, 'Diana', 30
5, 'Eve', 20

Departments:
10, 'HR'
20, 'Sales'
30, 'IT'
A[('IT', 1), ('HR', 2), ('Sales', 2)]
B[('HR', 2), ('Sales', 2), ('IT', 1)]
C[('Sales', 2), ('HR', 2), ('IT', 1)]
D[('HR', 3), ('Sales', 2), ('IT', 1)]
Attempts:
2 left
💡 Hint

Count employees per department, then join with department names and order by count descending.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this CTE query

Which option contains the syntax error in the CTE usage?

WITH RecentOrders AS (
  SELECT order_id, customer_id, order_date
  FROM Orders
  WHERE order_date > '2024-01-01'
)
SELECT * FROM RecentOrders
WHERE customer_id = 123;
ANo syntax error; the query is valid.
BMissing comma after CTE definition before SELECT statement.
CCTE must be followed by a semicolon before the main SELECT.
DIncorrect use of WHERE clause inside the CTE.
Attempts:
2 left
💡 Hint

Check if the CTE and main query are properly connected.

optimization
advanced
2:00remaining
Why might using a CTE hurt performance?

Which reason explains why CTEs can sometimes reduce query performance?

ACTEs are always materialized, causing repeated computation if referenced multiple times.
BCTEs automatically create indexes that slow down queries.
CCTEs force the database to scan all tables even if not needed.
DCTEs prevent the use of any WHERE clauses in the main query.
Attempts:
2 left
💡 Hint

Think about how databases handle CTEs internally.

🔧 Debug
expert
3:00remaining
Why does this recursive CTE cause an infinite loop?

Consider this recursive CTE to generate numbers:

WITH RECURSIVE Numbers AS (
  SELECT 1 AS num
  UNION ALL
  SELECT num + 1 FROM Numbers
  WHERE num < 5
)
SELECT * FROM Numbers;

Why might this query run indefinitely in some databases?

AThe UNION ALL should be UNION to avoid duplicates.
BRecursive CTEs are not supported in standard SQL.
CThe initial SELECT does not specify a column alias.
DThe recursive part lacks a proper termination condition causing infinite recursion.
Attempts:
2 left
💡 Hint

Check the condition that stops recursion.