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

Fill in the blank: 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 only 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

Selecting cities with a population larger than 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?

A

To simplify complex queries, avoid ambiguity, and improve 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
  • SELECT Name AS N FROM City AS C creates the alias N for the Name column and alias C for the City table.
  • 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
Q

What is the importance of the alias in the subquery?

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

It allows referencing the Id of the current employee without repeating Employee.Id.

26
Q

An alias gives tables or columns a temporary name for _______.

A

clarity.

27
Q

Using the AS keyword is strictly \_\_\_ when creating an alias.

A

Optional

28
Q

What does the alias N refer to in the example?

SELECT Name AS N FROM Employee AS E

A

N refers to the Name column.

29
Q

What does the alias C refer to in the example?

SELECT Name AS N FROM City AS C

A

C refers to the City table.

30
Q

What is the term for replacing a subquery with an equivalent join?

A

Flattening a query

31
Q

When are joins usually preferred in SQL?

A

When performance is a concern

32
Q

Which types of subqueries can usually be flattened?

A

Most subqueries that follow IN or EXISTS, or return a single value

33
Q

Which types of subqueries typically cannot be flattened?

A

Most subqueries that follow NOT EXISTS or contain a GROUP BY clause

34
Q

List the first step in flattening a query.

A

Retain the outer query SELECT FROM, GROUP BY, HAVING, and ORDER BY clauses

35
Q

What should be added for each subquery table during the flattening process?

A

INNER JOIN clauses

Second step in first past

36
Q

During flattening, where should comparisons between subquery and outer query columns be moved?

A

To ON clauses

Third step in the first pass

37
Q

What should be included in the WHERE clause after flattening a query?

A

Remaining expressions in the subquery and outer query WHERE clauses

Fourth step in the first pass

38
Q

What is the final step if necessary during the flattening process?

A

Remove duplicate rows with SELECT DISTINCT

Fifth step in the first pass

39
Q

What is the first pass step at flattening a query?

A

Retain the outer query SELECT, FROM, GROUP BY, HAVING, and ORDER BY clauses.

40
Q

What should be done after the first pass?

A
  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.