3.6 Sub Queries Flashcards

1
Q

What can be said about this subquery

A

The outer SELECT statement uses a subquery to determine which languages are used by a larger percentage of a country’s population than Aruba’s official language.

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

Which query executes first?

A
  1. The subquery 2 executes first to find the official language Percentage for ABW, which is 5.3.
  2. The outer query 1 executes using the value 5.3 returned by the subquery 2.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What would this subquery return?

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

What would this subquery return?

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

What does this query return?

A
Dutch, Balochi, Kongo

switching step 1 & 2 would be ideal.

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

Using the Exist operator

What does this query return?

A
Sam Snead
  1. Lisa Ellison and Maria Rodriguez both have a ‘Spouse’ in the Family table, but Sam Snead does not.
  2. NOT EXISTS returns TRUE when no rows are selected in the subquery, so only Sam Snead is returned.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Using the Exist operator

What does this query return?

A
Lisa Ellison and Maria Rodriguez
  1. The correlated subquery returns one row for Lisa Ellison and one row for Maria Rodriguez.
  2. No row exists in Family that has Sam Snead’s Id and relationship ‘Spouse’.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a subquery?

A

A subquery, also known as a nested query or inner query, is a query embedded within another SQL query

Typically used in a SELECT statement’s WHERE clause to provide data to the outer query, influencing the outcome of the selection. The subquery is enclosed in parentheses.

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

What is the purpose of a subquery in SQL?

A

To provide data to the outer query, influencing the outcome of the selection

Typically used in the WHERE clause of a SELECT statement.

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

A subquery is enclosed in _______.

A

(parentheses)

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

What does a correlated subquery do?

A

It references a column from the outer query within the subquery’s WHERE clause

Rows selected depend on the current row being processed in the outer query.

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

A correlated subquery selects rows based on _______.

A

the current row being processed in the outer query.

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

What does the EXISTS operator do in SQL?

A

Returns TRUE if the subquery fetches at least one row, FALSE otherwise

NOT EXISTS returns TRUE if no rows are selected, FALSE otherwise.

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

How can subqueries be transformed for performance optimization?

A

By flattening subqueries into joins

Most subqueries following IN or EXISTS can be flattened into joins, enhancing efficiency.

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

What is a key concept of flattening subqueries?

A

It can significantly improve query performance in data-intensive applications

Example: INNER JOIN City ON CountryCode WHERE Population > 1000000.

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

Subqueries are used $.

A

Subqueries can be used in various clauses, including WHERE.

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

What role do subqueries play in SQL queries?

A

They serve as powerful tools for organizing complex SQL queries

Understanding the use of subqueries and correlated subqueries enhances data extraction strategies.

18
Q

This is an example of what type of query

Selecting languages with a higher usage percentage than Aruba’s official language

A

This is an example of a subquery

Example
19
Q

This is an example of what type of query

  1. Selecting cities with a population larger THAN
  2. the country’s average city population
A

This is an example of a correlated subquery

Example Code
20
Q

What is an Alias?

A

A temporary name given to a table or column for the duration of a query.

21
Q

Why use Aliases? 3

A
  1. simplifies complex queries
  2. avoids ambiguity
  3. improves clarity
22
Q

How do you create an Alias using the AS Keyword?

A

Use the AS keyword to define an alias

SELECT Name AS N FROM City AS C

23
Q

Can you create an Alias without the AS Keyword?

A
  • The AS keyword is optional and may be omitted.
24
Q

In the query what does E represent?

SELECT Name 
FROM Employee E WHERE NOT EXISTS 
    (SELECT * FROM Family 
		WHERE Id = E.Id AND Relationship = 'Spouse');
A

E is an alias for the Employee table.

25
What is the importance of the alias `Employee.Id` in the subquery? ``` SELECT Name FROM Employee E WHERE EXISTS (SELECT * FROM Family WHERE ID = E.Id AND Relationship = 'Spouse'); ```
It allows referencing the Id of the current employee without repeating Employee.Id
26
An **alias** gives tables or columns a temporary name for _______.
clarity.
27
Using the `AS` keyword is strictly `___` when creating an alias.
Optional
28
What does the alias N refer to in the example? `SELECT Name AS N FROM Employee AS E`
`N` refers to the Name column.
29
What does the alias C refer to in the example? `SELECT Name AS N FROM City AS C`
`C` refers to the City table.
30
What is the term for replacing a subquery with an equivalent join?
Flattening a query
31
When are joins usually preferred in SQL?
When performance is a concern
32
Which types of **subqueries can** usually be flattened?
Most subqueries that **follow**: 1. `IN` 2. `EXISTS`, or 3. return a single value
33
Which types of subqueries typically cannot be flattened?
Most subqueries that follow `NOT EXISTS` or contain a `GROUP BY` clause cannot be flattened
34
List the first step in flattening a query.
Retain the outer query `SELECT` , `FROM`, `GROUP BY`, `HAVING`, and `ORDER BY` clauses
35
What should be added to *each subquery table* _during_ the flattening process?
`INNER JOIN` clauses ## Footnote Second step in first past
36
During flattening, where should **comparisons** between *sub*query and *outer* query columns be moved?
To `ON` clauses ## Footnote Third step in the first pass
37
What should be included in the `WHERE` clause _after_ flattening a query?
**Remaining expressions** in the *sub*query and *outer* query `WHERE` clauses ## Footnote Fourth step in the first pass
38
What is the final step if necessary during the flattening process?
Remove duplicate rows with `SELECT DISTINCT` ## Footnote Fifth step in the first pass
39
What is the first **pass step** at flattening a query?
Retain the outer query `SELECT`, `FROM`, `GROUP BY`, `HAVING`, and `ORDER BY` clauses.
40
What should be done after the first pass?
1. **Test** the flattened query and **adjust** to achieve the correct result. 2. **Verify** that the original and flattened queries are equivalent against a variety of data.