Correlated Subqueries Flashcards

1
Q

What is a correlated subquery?

A

It’s a subquery that uses information from the outer query

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

T/F: A correlated subquery is run on every row of the outer query

A

True

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

When would you want to use a correlated subquery?

A

You’d want to use one when the subquery depends on the value of each row in the outer query.

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

T/F: You have to alias the outer query’s table so it can be referenced in the inner subquery

A

True, e.g.
~~~
SELECT first_name, salary, department FROM employees outerr
WHERE salary > (SELECT ROUND(AVG(salary), 2)
FROM employees
WHERE department = outerr.department)
~~~

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

Use implicit join syntax to join employees and regions on region_id

A
SELECT *
FROM employees, regions
WHERE employees.region_id = regions.region_id
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What type of JOIN is an implicit JOIN?

A

It performs an INNER JOIN

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

Why are implicit joins bad practice?

A

Because they are less explicit in their syntax, which can make it difficult to read/track what is happening in a more complex query

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

How would you rewrite this to use explicit syntax?
~~~
SELECT first_name, region
FROM employees, regions
WHERE employees.region_id = regions.region_id
~~~

A
SELECT first_name, region
FROM employees
INNER JOIN regions
ON employees.region_id = regions.region_id
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the function of an INNER JOIN?

A

It joins two tables on a single column, and returns the rows where the values in that column are equal in a single table

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

What is the order of operations in this query?:
~~~
SELECT first_name, email, division, country
FROM employees e
INNER JOIN departments d
ON e.department = d.department
INNER JOIN regions r
ON e.region_id = r.region_id
WHERE email IS NOT NULL
~~~

A
  1. FROM clause is executed from top to bottom, i.e. the first INNER JOIN is performed, then the next INNER JOIN
  2. WHERE clause is executed, filtering on email column
  3. SELECT statement returns specified columns
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Why does this fail?
~~~
SELECT department FROM employees e
INNER JOIN departments d
ON employees.department = departments.department
~~~

A

Because department exists in both tables and is ambiguously called in the SELECT statement. You need to add a table prefix to specify which department column you want to return, e.g.:
~~~
SELECT e.department FROM employees e
INNER JOIN departments d
ON employees.department = departments.department
~~~

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

What is the function of a LEFT JOIN?

A

It returns ALL the values from the left table, and only the matching values from the right table. Any non-matching rows will have NULL in the right table columns.

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

What is the function of a RIGHT JOIN?

A

It returns ALL the values from the right table, and only the matching values from the left table. Any non-matching rows will have NULL in the left table columns.

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

What is LEFT/RIGHT JOIN also known as?

A

LEFT/RIGHT OUTER JOIN

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

What does FULL OUTER JOIN return?

A

It returns all match and non-match rows in a single table

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

In words using JOIN syntax, how would you find all of the values that DON’T exist in the right joining column but exist in the left joining column?

A

Do a LEFT JOIN on the column, then only return rows where the right column is NULL

17
Q

Why does this error?
~~~
SELECT DISTINCT department, region_id
FROM employees
UNION ALL
SELECT department, division
FROM departments
~~~

A

Because the type of region_id and division are different, preventing them from being concatenated

18
Q

What is happening in this query?
~~~
SELECT DISTINCT department
FROM employees
UNION
SELECT department
FROM departments
~~~

A

It is concatenating the top and bottom query values together and removing duplicates

19
Q

What is the difference between UNION and UNION ALL?

A

Both will concatenate one query result after the other, but UNION will remove duplicate values while UNION ALL will not

20
Q

T/F: you can only UNION one column at a time

A

False. Multiple columns can be UNION’d as long as the paired columns are the same type

21
Q

What does this query return?
~~~
SELECT DISTINCT department
FROM employees
EXCEPT
SELECT department
FROM departments
~~~

A

It returns the values from the employees table which DO NOT exist in the departments table

22
Q

What is the equivalent of EXCEPT in Oracle?

A

MINUS

23
Q

What type of JOIN does this return?
~~~
SELECT *
FROM departments, employees
~~~

A

It returns a CROSS JOIN, or Cartesian Product, of both of the tables. This means that every row from departments will be joined with every row of employees, resulting in and row length of len(departments) * len(employees)

24
Q

What’s a more explicit way of writing this query?
~~~
SELECT *
FROM departments, employees
~~~

A
SELECT *
FROM departments CROSS JOIN employees
25
Q

T/F: You need to specify a common column to join on with CROSS JOIN

A

False, CROSS JOIN doesn’t join on any column, instead returning the Cartesian Product of both tables