Data-Driven Decision Making in SQL
Irene Ortner
Data Scientist at Applied Statistics
Step 1: The inner query
SELECT DISTINCT customer_id
FROM renting
WHERE rating <= 3
| customer_id |
|-------------|
| 28          |
| 41          |
| 86          |
| 120         |
SELECT name
FROM customers
WHERE customer_id IN (28, 41, 86, 120);
Step 2: The outer query
SELECT name
FROM customers
WHERE customer_id IN
    (SELECT DISTINCT customer_id
    FROM renting
    WHERE rating <= 3);
| name            |
|-----------------|
| Sidney Généreux |
| Zara Mitchell   |
Step 1: The inner query
SELECT MIN(date_account_start)
FROM customers
WHERE country = 'Austria';
| min        |
|------------|
| 2017-11-22 |
Step 2: The outer query
SELECT country, MIN(date_account_start)
FROM customers
GROUP BY country
HAVING MIN(date_account_start) <
    (SELECT MIN(date_account_start)
    FROM customers
    WHERE country = 'Austria');
| country       | min        |
|---------------|------------|
| Spain         | 2017-02-14 |
| Great Britain | 2017-03-31 |
SELECT name
FROM actors
WHERE actor_id IN
    (SELECT actor_id 
     FROM actsin
     WHERE movie_id =
        (SELECT movie_id 
         FROM movies
         WHERE title='Ray'));
| name             |
|------------------|
| Jamie Foxx       |
| Kerry Washington |
| Regina King      |
Data-Driven Decision Making in SQL