Data-Driven Decision Making in SQL
Bart Baesens
Professor Data Science and Analytics
The first few rows of the table renting_extended
:
| renting_id | country | genre | rating |
|------------|----------|--------|--------|
| 2 | Belgium | Drama | 10 |
| 32 | Belgium | Drama | 10 |
| 203 | Austria | Drama | 6 |
| 292 | Austria | Comedy | 8 |
| 363 | Belgium | Drama | 7 |
| .......... | ........ | ...... | ...... |
SELECT country,
genre,
COUNT(*)
FROM renting_extended
GROUP BY ROLLUP (country, genre);
SELECT country,
genre,
COUNT(*)
FROM renting_extended
GROUP BY ROLLUP (country, genre);
| country | genre | count |
|---------|--------|-------|
| null | null | 22 |
| Austria | Comedy | 2 |
| Belgium | Drama | 15 |
| Austria | Drama | 4 |
| Belgium | Comedy | 1 |
| Belgium | null | 16 |
| Austria | null | 6 |
SELECT country,
genre,
COUNT(*)
FROM renting_extended
GROUP BY ROLLUP (genre, country);
| country | genre | count |
|---------|--------|-------|
| null | null | 22 |
| Austria | Comedy | 2 |
| Belgium | Drama | 15 |
| Austria | Drama | 4 |
| Belgium | Comedy | 1 |
| null | Comedy | 3 |
| null | Drama | 19 |
ROLLUP (country, genre)
SELECT country,
genre,
COUNT(*) AS n_rentals,
COUNT(rating) AS n_ratings
FROM renting_extended
GROUP BY ROLLUP (genre, country);
| country | genre | n_rentals | n_ratings |
|----------|--------|-----------|-----------|
| null | null | 22 | 9 |
| Belgium | Drama | 15 | 6 |
| Austria | Comedy | 2 | 1 |
| Belgium | Comedy | 1 | 0 |
| Austria | Drama | 4 | 2 |
| null | Comedy | 3 | 1 |
| null | Drama | 19 | 8 |
Data-Driven Decision Making in SQL