Batting 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
,
as well as the batters themselves in a batters
table ("batters" as in baseball bat, not like cake batter).
column_name | type | reference |
---|---|---|
id | INTEGER | NULL |
batter_id | INTEGER | batters.id |
strikes | INTEGER | NULL |
balls | INTEGER | NULL |
outcome | VARCHAR | NULL |
column_name | type | reference |
---|---|---|
id | INTEGER | NULL |
name | VARCHAR | NULL |
number | INTEGER | NULL |
team_id | INTEGER | teams.id |
This time we'll calculate a similar metric, the batting average. This is similar to the slugging average we calculated, except that it doesn't weigh how many bases were earned. It's an average of how often a batter gets to at least 1st base over the number of "at-bats". Remember that "at-bats" are 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
, their name
, and calculate their batting average (name that batting_average
).
Order the rows in descending order by batting_average
.
If 2 players have the same batting average, order the 1 with the lower ID first.
Division with `INTEGER`s can be a little surprising, unlike division with `NUMERIC`s.
SELECT
2 / 3 AS integer_division,
2.0 / 3.0 AS numeric_division;
integer_division INTEGER | numeric_division NUMERIC |
---|---|
0 | 0.66666666666666666667 |