Subqueries Flashcards

1
Q

What type of query is used inside another query as if it were a table?

A

Subquery

Last Revised: 4/2/21, 3.3

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

True or False: A subquery is always enclosed within parentheses, and it is usually executed prior to its outer query.

A

True

Last Revised: 4/2/21, 3.5

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

Put parentheses around your query, give it an alias, and you’ve got a subquery that you can put into the ____________ clause.

A

FROM

Last Revised: 4/2/21, 3.6

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

Inline views are another name for ________ ?

A

Subqueries in the FROM clause of the outer query

Last Revised: 4/2/21, 3.6

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

What is a candidate key?

A

(Last Revised: 4/2/21, 3.7)

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

What operator returns TRUE if the subquery yields any results and FALSE otherwise?

A

The EXISTS operator

Last Revised: 4/2/21, 3.8

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

What follows the EXISTS operator?

A

A subquery

Last Revised: 4/2/21, 3.8

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

Because the EXISTS operator returns TRUE or FASLE, it can be combined with other Boolean logic such as _____ .

A

NOT

Last Revised: 4/2/21, 3.8

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

What clause is the EXISTS operator commonly used in?

A

The WHERE clause

Last Revised: 4/2/21, 3.8

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

What is another way to think about latest encounters?

A

Think of it as “there are no later encounters.” In SQL, that translates to WHERE NOT EXISTS followed by a subquery that returns later encounters

(Last Revised: 4/2/21, 3.8)

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

What is the extent to which a portion of a query can be referenced elsewhere?

A

Scope

Last Revised: 4/2/21, 3.10

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

True or False: An outer query can only reference the results of a subquery, not the data or logic used to create the results.

A

True
• That means that the outer query can only reference the columns returned by the SELECT clause of the subquery.

(Last Revised: 4/2/21)

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

What clause does the subquery use to pass columns to the outer query?

A

The SELECT clause

Last Revised: 4/2/21, 3.10

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

Can a subquery reference the columns retuned by the FROM clause of the outer query?

A

Yes
• When a subquery is processed, SQL attempts to resolve references within the subquery; it that fails, then SQL attempts to resolve the reference with columns retuned by the FROM clause in the outer query.

(Last Revised: 4/2/21, 3.10)

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

What is an Uncorrelated Subquery a result of?

A

If a subquery doesn’t reference columns from the outer query, it is known as an uncorrelated subquery.

(Last Revised: 4/2/21)

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

What is a Correlated Subquery a result of?

A

If a subquery references columns from the outer query, it is known as a correlated subquery. Correlated subqueries are executed one for each row processed by the outer query, so they should generally be avoided for performance reasons. Queries that include correlated subqueries can often be rewritten to not use correlated subqueries.

(Last Revised: 4/2/21, 3.11)

17
Q

Are subqueries that appear in the FROM clause always correlated or uncorrelated?

A

They are always uncorrelated because the FROM clause hasn’t finished executing by the time the subquery is processed
• Subqueries elsewhere in the outer query may be correlated or uncorrelated.

(Last Revised: 4/2/21, 3.11)

18
Q

What operator should you use if you want your subquery to return only one column of data?

A

The IN operator
Write a query that has only one column in the SELECT clause. Then wrap it in parenthesis and it’s ready to be used as a subquery with the IN operator.

(Last Revised: 4/2/21, 3.13)

19
Q

What type of operator and in what clause should you use if you want your subquery to return a single value?

A

A comparison operator in the WHERE clause
Write a query that has only one column in the SELECT clause and returns only one row. Then wrap it in parentheses and it’s ready to be used as a subquery with a comparison operator.

(Last Revised: 4/2/21, 3.15)

20
Q

What do common table expressions (CTE) do?

A

They allow you to execute a subquery and save the result set for later reference by you main SELECT statement.

(Last Revised: 4/2/21, 3.16)

21
Q

What is a recursive CTE?

A

It is when a CTE references other CTEs.

Last Revised: 4/2/21, 3.16

22
Q

Where is the WITH clause positioned in your query?

A

Immediately before the main SELECT statement.

Last Revised: 4/2/21, 3.16

23
Q

Where are CTEs defined?

A

In the WITH clause.

Last Revised: 4/2/21, 3.16

24
Q

Can CTEs be referenced directly and be given aliases?

A

Yes, in the FROM clause.

Last Revised: 4/2/21, 3.17

25
Q

What two clauses can you use a subquery in?

A
  1. The FROM clause
  2. The WHERE clause

(Last Revised: 4/2/21, 3. )