Slugging Average
For this challenge, imagine that you are an analyst for the MLB
working to identify the greatest batters in baseball.
You have a table of players' batting attempts in a table called plate_appearances
.
column_name | type | reference |
---|---|---|
id | INTEGER | NULL |
batter_id | INTEGER | batters.id |
strikes | INTEGER | NULL |
balls | INTEGER | NULL |
outcome | VARCHAR | NULL |
Each row holds the outcome of batting attempts, i.e. whether they got on base, or got out, etc.
To see all the distinct values that the outcome
column can hold, you know how to write that query! (hint)
Calculate the slugging average for each player. The slugging average is basically a weighted average for how good a batter is. It is calculated as the number of total bases earned divided by the number of at-bats. In the numerator, "total bases" means 1 base earned if they reached 1st base, 2 bases earned if they reached 2nd base, 3 bases earned if they reached 3rd base, and 4 bases earned if they hit a home run. Everything else is worth 0. The number of "at-bats" in the denominator is the sum total of plate appearances that do not result in a walk, getting hit by a pitch, catcher interference, sacrifice bunt, or sacrifice fly.
Select each batter_id
and calculate their slugging average (name that slugging_average
).
Order the rows in descending order by slugging_average
.
Division with
INTEGER
s can be a little surprising, unlike division withNUMERIC
s.
SELECT
2 / 3 AS integer_division,
2.0 / 3.0 AS numeric_division;
integer_division INTEGER | numeric_division NUMERIC |
---|---|
0 | 0.66666666666666666667 |