wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

T-SQL and Common Table Expression

Total questions: 30

Worksheet time: 5mins

Name
Class
Date
1.

What is the primary purpose of a Common Table Expression (CTE) in T-SQL?

a)

To permanently store temporary data

b)

 To simplify complex subqueries and make queries more readable

c)

To define a new database schema

d)

 To create a clustered index on a table

2.

A CTE is defined using which T-SQL keyword?

a)

CREATE VIEW

b)

WITH

c)

DECLARE

d)

CREATE FUNCTION

3.

Which of the following statements about a non-recursive CTE is TRUE?

a)

It can be referenced multiple times within the same query.

b)

 It is a permanent object stored in the database.

c)

It can be used to perform DML operations (e.g., INSERT, UPDATE, DELETE).

d)

It can only reference a single table.

4.

 CTE's scope is limited to the:

a)

Entire database instance

b)

Entire stored procedure or function

c)

immediate SELECT, INSERT, UPDATE, or DELETE statement that follows it

d)

Current user session

5.

What is the main advantage of a CTE over a derived table (subquery in the FROM clause)?

a)

 Better performance

b)

The ability to be referenced by itself (recursion)

c)

The ability to be referenced by other CTEs in the same WITH clause

d)

All of the above

6.

Consider the following query:
SQL
WITH EmployeeCTE (EmployeeID, EmployeeName) AS (
    SELECT EmployeeID, FirstName + ' ' + LastName
    FROM Employees
)
SELECT * FROM EmployeeCTE;

What is EmployeeCTE?

a)

A temporary table

b)

A view

c)

A CTE name

d)

A table alias

7.

Which type of CTE allows a query to refer to itself, making it suitable for traversing hierarchical data?

a)

Simple CTE

b)

Non-recursive CTE

c)

Recursive CTE

d)

Common CTE

8.

In a recursive CTE, the first SELECT statement is known as the:

a)

Anchor member

b)

Recursive member

c)

 Union member

d)

Base member

9.

What operator is used to combine the anchor member and the recursive member in a recursive CTE?

a)

UNION ALL

b)

UNION

c)

INTERSECT

d)

EXCEPT

10.

A recursive CTE must have a terminating condition to prevent an infinite loop. This condition is typically handled by a WHERE clause in the:

a)

Anchor member

b)

Recursive member

c)

SELECT statement outside the CTE

d)

Both the anchor and recursive members

11.

Which of the following is a valid use case for a CTE?

a)

Calculating running totals

b)

Finding a company's organizational hierarchy

c)

Simplifying a complex query with multiple joins

d)

All of the above

12.

True or False: A CTE is physically materialized and stored on disk.

a)

True

b)

False

13.

True or False: You can define multiple CTEs within a single WITH clause, separated by a comma.

a)

True

b)

False

14.

To reference a CTE defined earlier in the same WITH clause, you must:

a)

se the JOIN keyword

b)

Simply refer to it by its name

c)

Use a subquery

d)

It is not possible to reference a CTE from another CTE in the same WITH clause

15.

What is the maximum number of recursive iterations for a CTE, by default?

a)

100

b)

500

c)

1000

d)

Unlimited

16.

How can you override the default maximum recursion limit?

a)

OPTION (MAXRECURSION n)

b)

SET MAXRECURSION = n

c)

WITH (MAXRECURSION n)

d)

LIMIT RECURSION n

17.
  1. Which of these is a non-recursive use of a CTE?

a)

Traversing a part-of-a-whole relationship

b)

Finding the top 10 most expensive products

c)

Finding a manager's direct reports and their reports

d)

Building a bill of materials (BOM)

18.

What is a key limitation of using a CTE?

a)

It cannot be used with DML statements.

b)

It cannot be nested.

c)

 It cannot contain an ORDER BY clause (except in the TOP or OFFSET context).

d)

It can only be used with SELECT statements.

19.

In a recursive CTE, the anchor and recursive members must have the same number of columns and compatible data types. What is this concept known as?

a)

Column alignment

b)

Data type consistency

c)

Structure conformity

d)

Union-compatible

20.

What is the main difference between a CTE and a subquery in terms of readability?

a)

CTEs are always more complex.

b)

Subqueries are easier to read and debug.

c)

CTEs allow for the breakdown of a complex query into logical, readable steps.

d)

There is no significant difference.

21.

A CTE can be referenced by the final SELECT statement multiple times.

a)

True

b)

False

22.

Which of the following is NOT a good use case for a recursive CTE?

a)

Finding the path from a child node to a root node in a tree structure.

b)

Generating a series of dates.

c)

Calculating a single aggregate value from a table.

d)

Traversing a family tree hierarchy.

23.

Which clause is required in a non-recursive CTE definition?

a)

WHERE

b)

GROUP BY

c)

WITH

d)

HAVING

24.

A CTE's execution plan is typically the same as the equivalent query without a CTE.

a)

True

b)

False

25.

What happens if a recursive CTE reaches the MAXRECURSION limit without terminating?

a)

The query continues until all rows are returned.

b)

The query terminates with a warning message.

c)

The query fails and returns an error.

d)

The query returns a partial result.

26.

Which of these is a correct definition of a simple (non-recursive) CTE?

a)

WITH MyCTE AS (SELECT * FROM Sales)

b)

CREATE CTE MyCTE AS (SELECT * FROM Sales)

c)

BEGIN MyCTE AS (SELECT * FROM Sales)

d)

DEFINE MyCTE AS (SELECT * FROM Sales)

27.

A CTE can be used in a CREATE VIEW statement.

a)

True

b)

False

28.

The columns in a CTE can be explicitly named.

a)

True

b)

False

29.

What is the name of the operation that combines the anchor and recursive parts in a recursive CTE?

a)

UNION

b)

JOIN

c)

UNION ALL

d)

CROSS JOIN

30.

In a recursive CTE, what part of the query is responsible for iterating and adding new rows to the result set?

a)

The anchor member

b)

he base query

c)

The recursive member

d)

The outer SELECT statement