In this project we will be working with the commonly-used Lahman baseball statistics database (our friends at the San Francisco Giants tell us they use it!) The database contains pitching, hitting, and fielding statistics for Major League Baseball from 1871 through 2019. It includes data from the two current leagues (American and National), four other "major" leagues (American Association, Union Association, Players League, and Federal League), and the National Association of 1871-1875.
At this point you should be able to run SQLite and view the database using either
./sqlite3 -header lahman.db (if in the previous section you downloaded a precompiled binary) or
sqlite3 -header lahman.db otherwise.
$ sqlite3 lahman.dbSQLite version 3.33.0 2020-08-14 13:23:32Enter ".help" for usage hints.sqlite> .tables
Try running a few sample commands in the SQLite console and see what they do:
sqlite> .schema people
sqlite> SELECT playerid, namefirst, namelast FROM people;
sqlite> SELECT COUNT(*) FROM fielding;
The database is comprised of the following main tables:
People - Player names, date of birth (DOB), and biographical infoBatting - batting statisticsPitching - pitching statisticsFielding - fielding statistics
It is supplemented by these tables:
AllStarFull - All-Star appearancesHallofFame - Hall of Fame voting dataManagers - managerial statisticsTeams - yearly stats and standingsBattingPost - post-season batting statisticsPitchingPost - post-season pitching statisticsTeamFranchises - franchise informationFieldingOF - outfield position dataFieldingPost- post-season fielding dataFieldingOFsplit - LF/CF/RF splitsManagersHalf - split season data for managersTeamsHalf - split season data for teamsSalaries - player salary dataSeriesPost - post-season series informationAwardsManagers - awards won by managersAwardsPlayers - awards won by playersAwardsShareManagers - award voting for manager awardsAwardsSharePlayers - award voting for player awardsAppearances - details on the positions a player appeared atSchools - list of colleges that players attendedCollegePlaying - list of players and the colleges they attendedParks - list of major league ballparlsHomeGames - Number of homegames played by each team in each ballpark
For more detailed information, see the docs online.
We've provided a skeleton solution file,
proj1.sql, to help you get started. In the file, you'll find a
CREATE VIEW statement for each part of the first 4 questions below, specifying a particular view name (like
q2i) and list of column names (like
lastname). The view name and column names constitute the interface against which we will grade this assignment. In other words, don't change or remove these names. Your job is to fill out the view definitions in a way that populates the views with the right tuples.
For example, consider Question 0: "What is the highest
era (earned run average) recorded in baseball history?".
proj1.sql file we provide:
CREATE VIEW q0(era) ASSELECT 1 -- replace this line;
You would edit this with your answer, keeping the schema the same:
-- solution you provideCREATE VIEW q0(era) ASSELECT MAX(era)FROM pitching;
To complete the project, create a view for
q0 as above (via copy-paste), and for all of the following queries, which you will need to write yourself.
You can confirm the test is now passing by running
python3 test.py -q 0
> python3 test.py -q 0PASS q0
More details on testing can be found in the Testing section.
SQLite doesn't support every SQL feature covered in lecture, specifically:
There is support for
LEFT OUTER JOIN but not
RIGHT OUTER or
To get equivalent output to
RIGHT OUTER you can reverse the order of the tables (i.e.
A RIGHT JOIN B is the same as
B LEFT JOIN A.
While it isn't required to complete this assignment, the equivalent to
FULL OUTER JOIN can be done by
RIGHT OUTER and
There is no regex match (
~) tilde operator. You can use
There is no
i. In the
people table, find the
birthyear for all players with weight greater than 300 pounds.
ii. Find the
birthyear of all players whose
namefirst field contains a space. Order the results by
namefirst, breaking ties with
namelast both in ascending order
iii. From the
people table, group together players with the same
birthyear, and report the
height, and number of players for each
birthyear. Order the results by
birthyear in ascending order.
Note: Some birth years have no players; your answer can simply skip those years. In some other years, you may find that all the players have a
NULL height value in the dataset (i.e.
height IS NULL); your query should return
NULL for the height in those years.
iv. Following the results of part iii, now only include groups with an average height >
70. Again order the results by
birthyear in ascending order.
i. Find the
yearid of all people who were successfully inducted into the Hall of Fame in descending order of
yearid. Break ties on
Note: a player with id
drewj.01 is listed as having failed to be inducted into the Hall of Fame, but does not show up in the
peopletable. Your query may assume that all players inducted into the Hall of Fame appear in the
ii. Find the people who were successfully inducted into the Hall of Fame and played in college at a school located in the state of California. For each person, return their
yearid in descending order of
yearid. Break ties on
schoolid, playerid (ascending). For this question,
yearid refers to the year of induction into the Hall of Fame.
Note: a player may appear in the results multiple times (once per year in a college in California).
Note: There's a discrepancy between the documentation for the data set and the data set itself. The
schools.schoolState column is actually
schools.state in the data set.
iii. Find the
schoolid of all people who were successfully inducted into the Hall of Fame -- whether or not they played in college. Return people in descending order of
playerid. Break ties on
schoolid (ascending). (Note:
schoolid will be
NULL if they did not play in college.)
i. Find the
yearid and single-year
slg (Slugging Percentage) of the players with the 10 best annual Slugging Percentage recorded over all time. For statistical significance, only include players with more than 50 at-bats in the season. Order the results by
slg descending, and break ties by
yearid, playerid (ascending).
Baseball note: Slugging Percentage is not provided in the database; it is computed according to a simple formula you can calculate from the data in the database.
SQL note: You should compute
slg properly as a floating point number---you'll need to figure out how to convince SQL to do this!
Data set note: The online documentation
batting mentions two columns
3B. On your local copy of the data set these have been renamed
H3B respectively (columns starting with numbers are tedious to write queries on).
Data set note: The column
H o f the
batting table represents all hits = (# singles) + (# doubles) + (# triples) + (# home runs), not just (# singles) so you’ll need to account for some double-counting
If a player played on multiple teams during the same season (for example
anderma02 in 2006) treat their time on each team separately for this calculation
ii. Following the results from Part i, find the
lslg (Lifetime Slugging Percentage) for the players with the top 10 Lifetime Slugging Percentage. Lifetime Slugging Percentage (LSLG) uses the same formula as Slugging Percentage (SLG), but it uses the number of singles, doubles, triples, home runs, and at bats each player has over their entire career, rather than just over a single season.
Note that the database only gives batting information broken down by year; you will need to convert to total information across all time (from the earliest date recorded up to the last date recorded) to compute
lslg. Order the results by
lslg (descending) and break ties by
Note: Make sure that you only include players with more than 50 at-bats across their lifetime.
iii. Find the
namelast and Lifetime Slugging Percentage (
lslg) of batters whose lifetime slugging percentage is higher than that of San Francisco favorite Willie Mays.
You may include Willie Mays'
playerid in your query (
mayswi01), but you may not include his slugging percentage -- you should calculate that as part of the query. (Test your query by replacing
mayswi01 with the playerid of another player -- it should work for that player as well! We may do the same in the autograder.)
Note: Make sure that you still only include players with more than 50 at-bats across their lifetime.
Just for fun: For those of you who are baseball buffs, variants of the above queries can be used to find other more detailed SaberMetrics, like Runs Created or Value Over Replacement Player. Wikipedia has a nice page on baseball statistics; most of these can be computed fairly directly in SQL.
Also just for fun: SF Giants VP of Baseball Operations, Yeshayah Goldfarb, suggested the following:
Using the Lahman database as your guide, make an argument for when MLBs “Steroid Era” started and ended. There are a number of different ways to explore this question using the data.
(Please do not include your "just for fun" answers in your solution file! They will break the autograder.)
i. Find the
yearid, min, max and average of all player salaries for each year recorded, ordered by
yearid in ascending order.
Historical note: In previous semesters we asked students to compute standard deviation (the square root of variance) for this question, but SQLite3 is so light it doesn't even come with a way to take square roots!
ii. For salaries in 2016, compute a histogram. Divide the salary range into 10 equal bins from min to max, with
binids 0 through 9, and count the salaries in each bin. Return the
high boundaries for each bin, as well as the number of salaries in each bin, with results sorted from smallest bin to largest.
binid 0 corresponds to the lowest salaries, and
binid 9 corresponds to the highest. The ranges are left-inclusive (i.e.
[low, high)) -- so the
high value is excluded. For example, if bin 2 has a
high value of 100000, salaries of 100000 belong in bin 3, and bin 3 should have a
low value of 100000.
high value for bin 9 may be inclusive).
Note: The test for this question is broken into two parts. Use
python3 test.py -q 4ii_bins_0_to_8 and
python3 test.py -q 4ii_bin_9 to run the tests
Hidden testing advice: we will be testing the case where a bin has zero player salaries in it. The correct behavior in this case is to display the correct
high with a
count of zero, NOT just excluding the bin altogether.
Some useful information:
You may find it helpful to have a table containing all the possible
binids. You can make a table/view with the possible values as follows:
binids(binid) AS (VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9))
We'll only be testing with these possible binid's (there aren't any hidden tests using say, 100 bins) so hardcoding in the values is fine
If you want to take the floor of a positive float value you can do
CAST (some_value AS INT)
iii. Now let's compute the Year-over-Year change in min, max and average player salary. For each year with recorded salaries after the first, return the
avgdiff with respect to the previous year. Order the output by
yearid in ascending order. (You should omit the very first year of recorded salaries from the result.)
iv. In 2001, the max salary went up by over $6 million. Write a query to find the players that had the max salary in 2000 and 2001. Return the
yearid for those two years. If multiple players tied for the max salary in a year, return all of them.
Note on notation: you are computing a relational variant of the argmax for each of those two years.
v. Each team has at least 1 All Star and may have multiple. For each team in the year 2016, give the
diffAvg (the difference between the team's highest paid all-star's salary and the team's lowest paid all-star's salary).
Note: Due to some discrepancies in the database, please draw your team names from the All-Star table (so use
allstarfull.teamid in the SELECT statement for this).
python3 test.py to see if you're passing tests. If so, follow the instructions in the next section to submit your work.