0
0
SQLquery~20 mins

CTE vs subquery vs view decision in SQL - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CTE vs Subquery vs View Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When to use a CTE instead of a subquery?
Which situation best justifies using a Common Table Expression (CTE) instead of a subquery in SQL?
AWhen you want to reuse the same result multiple times within a query for better readability.
BWhen you need to permanently store the result for future queries.
CWhen you want to create an index on the result set.
DWhen you want to avoid using temporary tables for performance reasons.
Attempts:
2 left
💡 Hint
Think about readability and reusability inside a single query.
query_result
intermediate
2:00remaining
Output difference between subquery and view
Given a table Employees with columns id, name, and department, what is the output of the following queries?

Query 1 (using subquery):
SELECT name FROM Employees WHERE department = (SELECT department FROM Employees WHERE id = 3);

Query 2 (using view):
CREATE VIEW DeptView AS SELECT department FROM Employees WHERE id = 3;
SELECT name FROM Employees WHERE department = (SELECT department FROM DeptView);

Assuming employee with id=3 belongs to 'Sales' department, what will both queries return?
ABoth queries return names of all employees in the 'Sales' department.
BQuery 1 returns names in 'Sales', Query 2 returns an error because views cannot be used in WHERE.
CQuery 1 returns only the employee with id=3, Query 2 returns all employees in 'Sales'.
DBoth queries return an empty set because subqueries and views behave differently.
Attempts:
2 left
💡 Hint
Think about how subqueries and views work in filtering data.
📝 Syntax
advanced
2:00remaining
Identify the correct syntax for CTE usage
Which of the following SQL statements has correct syntax when defining a CTE?
SQL
WITH SalesCTE AS (SELECT * FROM Sales WHERE amount > 1000) SELECT * FROM SalesCTE;
AWITH SalesCTE (SELECT * FROM Sales WHERE amount > 1000) SELECT * FROM SalesCTE;
BWITH SalesCTE AS (SELECT * FROM Sales WHERE amount > 1000) SELECT * FROM SalesCTE;
CWITH SalesCTE AS SELECT * FROM Sales WHERE amount > 1000; SELECT * FROM SalesCTE;
DWITH SalesCTE AS (SELECT * FROM Sales WHERE amount > 1000); SELECT * FROM SalesCTE;
Attempts:
2 left
💡 Hint
Check the placement of AS and parentheses in CTE syntax.
optimization
advanced
2:00remaining
Performance impact of views vs CTEs
Which statement about performance is true when comparing views and CTEs in SQL?
AViews and CTEs both create temporary tables that slow down queries.
BCTEs are always faster than views because they are temporary and inline.
CViews always improve query performance because they store precomputed results.
DNeither views nor CTEs store data physically; performance depends on query complexity and indexing.
Attempts:
2 left
💡 Hint
Think about how views and CTEs are processed by the database engine.
🔧 Debug
expert
2:00remaining
Why does this query with a view fail?
Consider this SQL code:
CREATE VIEW RecentOrders AS SELECT * FROM Orders WHERE order_date > '2024-01-01';
SELECT customer_id, COUNT(*) FROM RecentOrders GROUP BY customer_id HAVING COUNT(*) > 5;

Why might this query fail in some SQL systems?
ABecause the view does not include a GROUP BY clause, causing aggregation errors.
BBecause views cannot be used in GROUP BY clauses.
CBecause some SQL systems require explicit schema qualification when querying views.
DBecause the view is not materialized and the query optimizer cannot handle aggregation on views.
Attempts:
2 left
💡 Hint
Consider database-specific requirements for referencing views.