CTE Introduction Flashcards

(20 cards)

1
Q

What does CTE stand for in SQL?

A

Common Table Expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Common Table Expression?

A

A named temporary result set used within a SQL statement.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which SQL keyword is used to create a CTE?

A

WITH

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In which types of SQL statements can you use a CTE?

A

SELECT, INSERT, UPDATE, DELETE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Does a CTE persist after the query is run?

A

No, it is temporary and discarded after the query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How is a CTE referenced in the main query?

A

Like a regular table, using its assigned name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is one benefit of using a CTE over creating a physical table?

A

You avoid the need to create and drop temporary tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the syntax structure after the WITH keyword?

A

CTE name, followed by a SELECT statement inside parentheses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can you use more than one CTE in a query?

A

Yes, by separating them with commas after a single WITH keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are multiple CTEs referenced in the main query?

A

By their individual names as if they were tables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the example with schools calculate?

A

Average number of students per district.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

In the banking bonus example, what does the first CTE calculate?

A

Average bonus for each position.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When joining a table with a CTE, which clause is typically used?

A

JOIN

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is an advantage of using CTEs for readability?

A

They organize complex queries into logical building blocks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Can a CTE reference another CTE?

A

Yes, in the case of nested CTEs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a nested CTE?

A

A CTE that uses another CTE within its definition.

17
Q

In the bonus example with multiple CTEs, what two averages are compared?

A

Average bonus per position and per region.

18
Q

What SQL aggregate functions were demonstrated with CTEs in examples?

A

AVG, MIN, MAX

19
Q

In the outlet bonus evaluation, which CTE calculates the average bonus per outlet?

A

avg_per_outlet

20
Q

What should you be careful about when nesting CTEs?

A

Ensure earlier CTEs are defined before referencing them.