
Aggregating - Assignments
1. Number of actors by gender
Complete the following query to obtain the number of actors by gender.
- Retrieve data from the
actorstable. - Group by the
gendercolumn withGROUP BY. - Select the
gendercolumn, and count the number of rows with theCOUNT()function.
| gender | count |
|---|---|
| M | 1464 |
| F | 443 |
2. Average number of films
Complete the following query to also calculate the average number of films for actors of a certain gender.
- Reuse your code from the previous assignment.
- Retrieve data from the
actorstable. - Group by the
gendercolumn withGROUP BY. - Select/calculate columns:
- Gender:
gender - Count the number of rows with the
COUNT()function. - Calculate the average number of films with
AVG().- Name this column
film_count_avgwithAS.
- Name this column
- Gender:
| gender | count | film_count_avg |
|---|---|---|
| M | 1464 | 1.0484972677595628 |
| F | 443 | 1.0248306997742664 |
3. Largest number of films
Complete the following query to also calculate the largest number of films for an actor of a certain gender.
- Reuse your code from the previous assignment.
- Retrieve data from the
actorstable. - Group by the
gendercolumn withGROUP BY. - Select/calculate columns:
- Gender:
gender - Count the number of rows with the
COUNT()function. - Calculate the average number of films with
AVG().- Name this column
film_count_avgwithAS.
- Name this column
- Find the largest number of films with
MAX().- Name this column
film_count_maxwithAS.
- Name this column
- Gender:
SELECT
gender,
COUNT(*),
AVG(film_count) AS film_count_avg,
MAX(film_count) AS film_count_max
FROM
actors
GROUP BY
gender;
| gender | count | film_count_avg | film_count_max |
|---|---|---|---|
| M | 1464 | 1.0484972677595628 | 9 |
| F | 443 | 1.0248306997742664 | 3 |
4. (Extra) Length of names
Complete the following query to also calculate the average length (number of characters) of first names of actors per gender.
- Reuse your code from the previous assignment.
- Calculate the average length of first names, column
first_name.- Use the
AVG()andLENGTH()functions. - Name the new column
first_name_avg.
- Use the
- Which gender has the longest first name on average?
SELECT
gender,
COUNT(*),
AVG(film_count) AS film_count_avg,
MAX(film_count) AS film_count_max,
AVG(LENGTH(first_name)) AS first_name_avg
FROM
actors
GROUP BY
gender;
| gender | count | film_count_avg | film_count_max | first_name_avg |
|---|---|---|---|---|
| M | 1464 | 1.0484972677595628 | 9 | 6.4699453551912568 |
| F | 443 | 1.0248306997742664 | 3 | 6.8442437923250564 |
5. (Extra) Number of actors with names of certain length
Complete the following query to:
- Group by the length of first names of actors.
- Select/calculate as columns:
- Length (number of characters) of first names of actors.
- The number of rows (number of actors with a first name of a certain length).
- Sort by the length (number of characters) of first names of actors.
SELECT
LENGTH(first_name) as first_name_length,
COUNT(*)
FROM
actors
GROUP BY
LENGTH(first_name)
ORDER BY
first_name_length;
| first_name_length | count |
|---|---|
| 2 | 12 |
| 3 | 146 |
| 4 | 296 |
| 5 | 365 |
| ... | ... |