Subqueries Flashcards

1
Q

Common table expression (CTE) allows you to execute a subquery and save the result set for later reference by your main SELECT statement. What are 3 benefits?

A

neater code, can be referenced multiple times, can reference other CTEs or itself.

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

A CTE that references itself is known as

A

recursive

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

Which clause in a subquery needs to be modified to make a column available for reference in the outer query?

A

The SELECT clause. Only the columns returned by a subquery’s SELECT clause can be referenced by the outer query.

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

Which are typically more efficient: correlated or uncorrelated subqueries.

A

Uncorrelated - they only need to be run once for each time the main SELECT statement is run.

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

If you want to reference a CTE in a SELECT statement, in which clause do you need to add it?

A

FROM clause

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

What is a subquery?

A

A query that is used inside another query (the outer query) as if it were a table. Nested!

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

A subquery that you’ve put in the FROM clause could also be called

A

an inline view

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

What is a candidate key?

A

A set of columns that isn’t the primary key but behaves like the primary key.

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

What is a correlated subquery?

A

a subquery that references columns from the outer query. it executes once for every row in the outer query. Inefficient!

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

The EXISTS operator is followed by a subquery. It returns TRUE if the subquery yields any results, FALSE otherwise.

A

It can be combined with other Boolean logic such as NOT. This is commonly used in the WHERE clause.

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

For the EXISTS operator to work the subquery must reference…

A

columns in the outer query

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

The outer query can only reference what part of the subquery?

A

The results of the subquery (the columns named in the SELECT clause).

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

How can you test a subquery to see if it is correlated or uncorrelated?

A

Try to run it by itself. If it runs it’s uncorrelated.

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

What is the concept ‘scope’ as it pertains to SQL.

A

The extent to which a portion of a query can be referenced elsewhere.

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