Ctr

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).


plate_appearances
column_name type reference
id
INTEGER
NULL
batter_id
INTEGER
batters.id
strikes
INTEGER
NULL
balls
INTEGER
NULL
outcome
VARCHAR
NULL

batters
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

© 2022 Andrew Carlson. All rights reserved.

id
INTEGER
name
VARCHAR
batting_average
NUMERIC
1
8255
Otis Pouros
0.41935483870967741935
2
5594
Clayton Pouros
0.37931034482758620690
3
1048
Devin Schimmel
0.37037037037037037037
4
2139
Russell Klocko
0.35714285714285714286
5
6551
Jake Renner
0.34782608695652173913
6
8492
Bob Ward
0.34782608695652173913
7
4989
Cary Cronin
0.33333333333333333333
8
8382
Fred Jenkins
0.33333333333333333333
9
1528
Vernon Stamm
0.27272727272727272727
10
5281
Vernon Dickens
0.25000000000000000000
11
6258
Edgar Hoeger
0.25000000000000000000
12
2037
Garry Haag
0.24242424242424242424
13
4268
Arthur Murazik
0.22580645161290322581
14
5497
Cameron Jacobson
0.17647058823529411765
15
8929
Timmy Will
0.10526315789473684211
15 rows

You haven't solved this challenge yet!
Are you sure you want to reveal the answer?