Baby Names
Published:
In this project I have worked with data provided by the United States Social Security Administration, which lists first names along with the number and sex of babies they were given to in each year. For processing speed purposes, we've limited the dataset to first names which were given to over 5,000 American babies in a given year. Our data spans 101 years, from 1920 through 2020.
Skills : SQL, Data Exploration, Data Cleaning and Manipulation
baby_names
column | type | meaning |
---|---|---|
year | int | year |
first_name | varchar | first name |
sex | varchar | sex of babies given first_name |
num | int | number of babies of sex given first_name in that year |
1. Classic American names
Let's get oriented to American baby name tastes by looking at the names that have stood the test of time!
%%sql
postgresql:///names
-- Select first names and the total babies with that first_name
-- Group by first_name and filter for those names that appear in all 101 years
-- Order by the total number of babies with that first_name, descending
SELECT first_name, SUM(num)
FROM baby_names
GROUP BY first_name
HAVING COUNT(year) = 101
ORDER BY SUM(num) DESC;
8 rows affected.
first_name | sum |
---|---|
James | 4748138 |
John | 4510721 |
William | 3614424 |
David | 3571498 |
Joseph | 2361382 |
Thomas | 2166802 |
Charles | 2112352 |
Elizabeth | 1436286 |
2. Timeless or trendy?
Here I have captured the type of popularity that each name in the dataset enjoyed. Was the name classic and popular across many years or trendy, only popular for a few years?
%%sql
-- Classify first names as 'Classic', 'Semi-classic', 'Semi-trendy', or 'Trendy'
-- Alias this column as popularity_type
-- Select first_name, the sum of babies who have ever had that name, and popularity_type
-- Order the results alphabetically by first_name
SELECT first_name, SUM(num),
CASE WHEN COUNT(year) > 90 THEN 'Classic'
WHEN COUNT(year) > 40 THEN 'Semi-classic'
WHEN COUNT(year) > 10 THEN 'Semi-trendy'
ELSE 'Trendy' END AS popularity_type
FROM baby_names
GROUP BY first_name
ORDER BY first_name
LIMIT 10;
* postgresql:///names
10 rows affected.
first_name | sum | popularity_type |
---|---|---|
Aaliyah | 15870 | Trendy |
Aaron | 530592 | Semi-classic |
Abigail | 338485 | Semi-trendy |
Adam | 497293 | Semi-classic |
Addison | 107433 | Semi-trendy |
Adrian | 147741 | Semi-trendy |
Aidan | 68566 | Trendy |
Aiden | 216194 | Semi-trendy |
Alan | 162041 | Semi-trendy |
Albert | 260945 | Semi-trendy |
3. Top-ranked female names since 1920
Here the search is limited to names which were given to female babies.
Window function is used by assigning a rank to female names based on the number of babies that have ever been given that name. What are the top-ranked female names since 1920?
%%sql
-- RANK names by the sum of babies who have ever had that name (descending), aliasing as name_rank
-- Select name_rank, first_name, and the sum of babies who have ever had that name
-- Filter the data for results where sex equals 'F'
-- Limit to ten results
SELECT
RANK() OVER (ORDER BY SUM(num) DESC) AS name_rank,
first_name,
sum(num)
FROM baby_names
WHERE sex ='F'
GROUP BY first_name
LIMIT 10;
* postgresql:///names
10 rows affected.
name_rank | first_name | sum |
---|---|---|
1 | Mary | 3215850 |
2 | Patricia | 1479802 |
3 | Elizabeth | 1436286 |
4 | Jennifer | 1404743 |
5 | Linda | 1361021 |
6 | Barbara | 1343901 |
7 | Susan | 1025728 |
8 | Jessica | 994210 |
9 | Lisa | 920119 |
10 | Betty | 893396 |
4. Picking a baby name
We are analyzing the data for traditionally female name ending with the letter 'a' and a name that has been popular in the years since 2015.
%%sql
-- Select only the first_name column
-- Filter for results where sex is 'F', year is greater than 2015, and first_name ends in 'a'
-- Group by first_name and order by the total number of babies given that first_name
SELECT first_name
FROM baby_names
WHERE sex = 'F' AND year > 2015 AND first_name LIKE '%a'
GROUP BY first_name
ORDER BY SUM(num) DESC;
* postgresql:///names
19 rows affected.
first_name |
---|
Olivia |
Emma |
Ava |
Sophia |
Isabella |
Mia |
Amelia |
Ella |
Sofia |
Camila |
Aria |
Victoria |
Layla |
Nora |
Mila |
Luna |
Stella |
Gianna |
Aurora |
5. The Olivia expansion
Based on the results in the previous task, Olivia is the most popular female name ending in 'A' since 2015. In the next step we will analyze when did the name Olivia become so popular?
The rise of the name Olivia is explored with the help of a window function.
%%sql
-- Select year, first_name, num of Olivias in that year, and cumulative_olivias
-- Sum the cumulative babies who have been named Olivia up to that year; alias as cumulative_olivias
-- Filter so that only data for the name Olivia is returned.
-- Order by year from the earliest year to most recent
SELECT year, first_name, num,
SUM(num) OVER (ORDER BY year) AS cumulative_olivias
FROM baby_names
WHERE first_name = 'Olivia'
ORDER BY year ASC
LIMIT 10;
* postgresql:///names
30 rows affected.
year | first_name | num | cumulative_olivias |
---|---|---|---|
1991 | Olivia | 5601 | 5601 |
1992 | Olivia | 5809 | 11410 |
1993 | Olivia | 6340 | 17750 |
1994 | Olivia | 6434 | 24184 |
1995 | Olivia | 7624 | 31808 |
1996 | Olivia | 8124 | 39932 |
1997 | Olivia | 9477 | 49409 |
1998 | Olivia | 10610 | 60019 |
1999 | Olivia | 11255 | 71274 |
2000 | Olivia | 12852 | 84126 |
2001 | Olivia | 13977 | 98103 |
2002 | Olivia | 14630 | 112733 |
2003 | Olivia | 16152 | 128885 |
2004 | Olivia | 16106 | 144991 |
2005 | Olivia | 15694 | 160685 |
2006 | Olivia | 15501 | 176186 |
2007 | Olivia | 16584 | 192770 |
2008 | Olivia | 17084 | 209854 |
2009 | Olivia | 17438 | 227292 |
2010 | Olivia | 17029 | 244321 |
2011 | Olivia | 17327 | 261648 |
2012 | Olivia | 17320 | 278968 |
2013 | Olivia | 18439 | 297407 |
2014 | Olivia | 19823 | 317230 |
2015 | Olivia | 19710 | 336940 |
2016 | Olivia | 19380 | 356320 |
2017 | Olivia | 18744 | 375064 |
2018 | Olivia | 18011 | 393075 |
2019 | Olivia | 18508 | 411583 |
2020 | Olivia | 17535 | 429118 |
6. Many males with the same name
In the next task, Let's take a look at traditionally male names. We saw in the first task that there are nine traditionally male names given to at least 5,000 babies every single year in our 101-year dataset!
In the next two tasks, we will build up to listing every year along with the most popular male name in that year. This presents a common problem: how do we find the greatest X in a group? Or, in the context of this problem, how do we find the male name given to the highest number of babies in a year?
Subquery can be used in the query. first query is written that selects the year
and the maximum num
of babies given any single male name in that year. For example, in 1989, the male name given to the highest number of babies was given to 65,339 babies. This query is written in this task.
%%sql
-- Select year and maximum number of babies given any one male name in that year, aliased as max_num
-- Filter the data to include only results where sex equals 'M'
SELECT year, MAX(num) as max_num
FROM baby_names
WHERE sex ='M'
GROUP BY year;
* postgresql:///names
101 rows affected.
year | max_num |
---|---|
1970 | 85291 |
2000 | 34483 |
1947 | 94764 |
1962 | 85041 |
1975 | 68451 |
1980 | 68704 |
1931 | 60518 |
1981 | 68776 |
2013 | 18266 |
1972 | 71401 |
1956 | 90665 |
2007 | 24292 |
1948 | 88589 |
1984 | 67745 |
1957 | 92718 |
1961 | 86917 |
2002 | 30579 |
1925 | 60897 |
1992 | 54397 |
2008 | 22603 |
1958 | 90564 |
1971 | 77599 |
1985 | 64924 |
1926 | 61130 |
1988 | 64150 |
1929 | 59804 |
1963 | 83778 |
1928 | 60703 |
2003 | 29643 |
1930 | 62149 |
1951 | 87261 |
1940 | 62476 |
1982 | 68244 |
1920 | 56914 |
1999 | 35367 |
1952 | 87063 |
2020 | 19659 |
1946 | 87439 |
1968 | 81995 |
1996 | 38365 |
2005 | 25837 |
1923 | 57469 |
2009 | 21184 |
1924 | 60801 |
1954 | 88576 |
2004 | 27886 |
1938 | 62269 |
1942 | 77174 |
1966 | 79990 |
1998 | 36616 |
1974 | 67580 |
1949 | 86865 |
1990 | 65302 |
1995 | 41399 |
1973 | 67842 |
1927 | 61671 |
1941 | 66743 |
1977 | 67609 |
2001 | 32554 |
1997 | 37549 |
2014 | 19319 |
1965 | 81021 |
1935 | 56522 |
1944 | 76954 |
1994 | 44472 |
2016 | 19154 |
1960 | 85933 |
1987 | 63654 |
1978 | 67157 |
2018 | 19924 |
2006 | 24850 |
1921 | 58215 |
1993 | 49554 |
1964 | 82642 |
1943 | 80274 |
1937 | 61842 |
1986 | 64224 |
1953 | 86247 |
1959 | 85224 |
1976 | 66947 |
1989 | 65399 |
2012 | 19088 |
2011 | 20378 |
2019 | 20555 |
1955 | 88372 |
1939 | 59653 |
1979 | 67742 |
1991 | 60793 |
2017 | 18824 |
1969 | 85201 |
2015 | 19650 |
2010 | 22139 |
1932 | 59265 |
1967 | 82440 |
1922 | 57280 |
1933 | 54223 |
1934 | 55834 |
1936 | 58499 |
1945 | 74460 |
1950 | 86229 |
1983 | 68010 |
7. Top male names over the years
In the previous task, we found the maximum number of babies given any one male name in each year. Incredibly, the most popular name each year varied from being given to less than 20,000 babies to being given to more than 90,000!
In this task, we find out what that top male name is for each year in our dataset.
%%sql
-- Select year, first_name given to the largest number of male babies, and num of babies given that name
-- Join baby_names to the code in the last task as a subquery
-- Order results by year descending
SELECT b.year, b.first_name, b.num
FROM baby_names AS b
INNER JOIN (
SELECT year, MAX(num) as max_num
FROM baby_names
WHERE sex ='M'
GROUP BY year) AS new_subquery
ON new_subquery.year = b.year AND new_subquery.max_num = b.num
ORDER BY year DESC;
* postgresql:///names
101 rows affected.
year | first_name | num |
---|---|---|
2020 | Liam | 19659 |
2019 | Liam | 20555 |
2018 | Liam | 19924 |
2017 | Liam | 18824 |
2016 | Noah | 19154 |
2015 | Noah | 19650 |
2014 | Noah | 19319 |
2013 | Noah | 18266 |
2012 | Jacob | 19088 |
2011 | Jacob | 20378 |
2010 | Jacob | 22139 |
2009 | Jacob | 21184 |
2008 | Jacob | 22603 |
2007 | Jacob | 24292 |
2006 | Jacob | 24850 |
2005 | Jacob | 25837 |
2004 | Jacob | 27886 |
2003 | Jacob | 29643 |
2002 | Jacob | 30579 |
2001 | Jacob | 32554 |
2000 | Jacob | 34483 |
1999 | Jacob | 35367 |
1998 | Michael | 36616 |
1997 | Michael | 37549 |
1996 | Michael | 38365 |
1995 | Michael | 41399 |
1994 | Michael | 44472 |
1993 | Michael | 49554 |
1992 | Michael | 54397 |
1991 | Michael | 60793 |
1990 | Michael | 65302 |
1989 | Michael | 65399 |
1988 | Michael | 64150 |
1987 | Michael | 63654 |
1986 | Michael | 64224 |
1985 | Michael | 64924 |
1984 | Michael | 67745 |
1983 | Michael | 68010 |
1982 | Michael | 68244 |
1981 | Michael | 68776 |
1980 | Michael | 68704 |
1979 | Michael | 67742 |
1978 | Michael | 67157 |
1977 | Michael | 67609 |
1976 | Michael | 66947 |
1975 | Michael | 68451 |
1974 | Michael | 67580 |
1973 | Michael | 67842 |
1972 | Michael | 71401 |
1971 | Michael | 77599 |
1970 | Michael | 85291 |
1969 | Michael | 85201 |
1968 | Michael | 81995 |
1967 | Michael | 82440 |
1966 | Michael | 79990 |
1965 | Michael | 81021 |
1964 | Michael | 82642 |
1963 | Michael | 83778 |
1962 | Michael | 85041 |
1961 | Michael | 86917 |
1960 | David | 85933 |
1959 | Michael | 85224 |
1958 | Michael | 90564 |
1957 | Michael | 92718 |
1956 | Michael | 90665 |
1955 | Michael | 88372 |
1954 | Michael | 88576 |
1953 | Robert | 86247 |
1952 | James | 87063 |
1951 | James | 87261 |
1950 | James | 86229 |
1949 | James | 86865 |
1948 | James | 88589 |
1947 | James | 94764 |
1946 | James | 87439 |
1945 | James | 74460 |
1944 | James | 76954 |
1943 | James | 80274 |
1942 | James | 77174 |
1941 | James | 66743 |
1940 | James | 62476 |
1939 | Robert | 59653 |
1938 | Robert | 62269 |
1937 | Robert | 61842 |
1936 | Robert | 58499 |
1935 | Robert | 56522 |
1934 | Robert | 55834 |
1933 | Robert | 54223 |
1932 | Robert | 59265 |
1931 | Robert | 60518 |
1930 | Robert | 62149 |
1929 | Robert | 59804 |
1928 | Robert | 60703 |
1927 | Robert | 61671 |
1926 | Robert | 61130 |
1925 | Robert | 60897 |
1924 | Robert | 60801 |
1923 | John | 57469 |
1922 | John | 57280 |
1921 | John | 58215 |
1920 | John | 56914 |
8. The most years at number one
Noah and Liam have ruled the roost in the last few years, but if we scroll down in the results, it looks like Michael and Jacob have also spent a good number of years as the top name! Which name has been number one for the largest number of years? Let's use a common table expression to find out.
%%sql
-- Select first_name and a count of years it was the top name in the last task; alias as count_top_name
-- Use the code from the previous task as a common table expression
-- Group by first_name and order by count_top_name descending
WITH count_top_name_male AS (
SELECT b.year, b.first_name, b.num
FROM baby_names AS b
INNER JOIN (
SELECT year, MAX(num) as max_num
FROM baby_names
WHERE sex ='M'
GROUP BY year) AS new_subquery
ON new_subquery.year = b.year
AND new_subquery.max_num = b.num
ORDER BY year DESC
)
SELECT first_name , COUNT(first_name) as count_top_name
FROM count_top_name_male
GROUP BY first_name
ORDER BY COUNT(first_name) DESC;
* postgresql:///names
8 rows affected.
first_name | count_top_name |
---|---|
Michael | 44 |
Robert | 17 |
Jacob | 14 |
James | 13 |
Noah | 4 |
John | 4 |
Liam | 4 |
David | 1 |