Baseball Examples

These examples depend on this database:

/anvil/projects/tdm/data/lahman/lahman.db

Using lahman, display the first 10 ballparks in the ballparks table.

Click to see solution
SELECT * FROM parks LIMIT 10;
ID parkalias parkkey parkname city state country

1

NA

ALB01

Riverside Park

Albany

NY

US

2

NA

ALT01

Columbia Park

Altoona

PA

US

3

Edison Field; Anaheim Stadium

ANA01

Angel Stadium of Anaheim

Anaheim

CA

US

4

NA

ARL01

Arlington Stadium

Arlington

TX

US

5

The Ballpark in Arlington; Ameriquest Fl

ARL02

Rangers Ballpark in Arlington

Arlington

TX

US

6

NA

ATL01

Atlanta-Fulton County Stadium

Atlanta

GA

US

7

NA

ATL02

Turner Field

Atlanta

GA

US

8

NA

ATL03

Suntrust Park

Atlanta

GA

US

9

NA

BAL01

Madison Avenue Grounds

Baltimore

MD

US

10

NA

BAL02

Newington Park

Baltimore

MD

US

Using lahman, make a list of the names of all of the inactive teams in baseball history.

Click to see solution
SELECT franchName FROM teamsfranchises WHERE active=='N' LIMIT 10;
franchName

Altoona Mountain City

Philadelphia Athletics

Buffalo Bisons

Buffalo Bisons

Baltimore Orioles

Baltimore Terrapins

Baltimore Monumentals

Boston Reds

Brooklyn Gladiators

Boston Reds

Using lahman, find the player with the most runs batted in (RBIs) in a season, using 2 queries. In the first query, find the playerID of the player with the most RBIs. In the second query find the player’s name in the people table.

Click to see solution
-- Find the playerID
SELECT playerID FROM batting WHERE RBI==191;

-- Display the name
SELECT
	nameFirst,
	nameLast
FROM
	people
WHERE
	playerID == 'wilsoha01';
playerID

wilsoha01

Using lahman, figure out the manager of the 1976 "Big Red Machine" (CIN)? Answer this question using 2 queries.

Click to see solution

The "Big Red Machine" was a famous nickname for the dominant Cincinnati Reds of the early 1970s. Many of its team members are HOFers, including their manager, Sparky Anderson.

SELECT
	playerID
FROM
	managers
WHERE
	yearID == 1976
	AND teamID == 'CIN';

SELECT
	nameFirst,
	nameLast
FROM
	people
WHERE
	playerID == 'andersp01';
playerID

andersp01

Using lahman, make a teamID list for teams that were managed by Tony LaRussa. Answer this question using 2 queries.

Click to see solution

Tony LaRussa is renowned for being a very successful, long-time manager. He won the World Series with the St. Louis Cardinals and the Oakland Athletics.

SELECT
	playerID
FROM
	people
WHERE
	nameLast == 'LaRussa'
	AND nameFirst == 'Tony';

SELECT DISTINCT
	teamID
FROM
	managers
WHERE
	playerID == 'larusto01';
playerID

larusto01

Using lahman, figure out what Cecil Fielder’s salary was in 1987. Display the teamID with the salary.

Click to see solution
SELECT
	playerID
FROM
	people
WHERE
	nameFirst == 'Cecil'
	AND nameLast == 'Fielder';

SELECT
	teamID,
	salary
FROM
	salaries
WHERE
	playerID == 'fieldce01'
	AND yearID == 1987;
teamID salary

TOR

78500.0

Using lahman, make a list of all the teams who’ve lost a World Series (WS) since 1990. Put the list in ascending order by yearID.

Click to see solution
SELECT
	teamIDloser,
	yearID
FROM
	seriespost
WHERE
	yearID >= 1990
	AND round == 'WS'
ORDER BY
	yearID ASC
LIMIT 10;
teamIDloser yearID

OAK

1990

ATL

1991

ATL

1992

PHI

1993

CLE

1995

ATL

1996

CLE

1997

SDN

1998

ATL

1999

NYN

2000

Using lahman, find out what Cal Ripken Jr.'s height and weight was. Did he bat right or left-handed? When did he play his final game? Find all of this information out in a single query.

Click to see solution
SELECT
	height,
	weight,
	bats,
	finalgame
FROM
	people
WHERE
	nameFirst == 'Cal'
	AND nameLast == 'Ripken'
	AND deathState IS NULL;
height weight bats finalGame

76

200

R

2001-10-06

Using lahman, select all playerID and yearID of the players who were inducted into the Hall of Fame and voted in by the Veterans Committee between 1990 and 2000. Put the list in descending order.

Click to see solution

The Hall of Fame Veterans Committee nominates Hall of Fame players that are forgotten, so to say, in baseball history. The committee allows players to recognize and give justice to their underrated, underviewed, or undervalued peers.

SELECT
	playerID,
	yearID
FROM
	halloffame
WHERE
	votedBy == 'Veterans'
	AND inducted == 'Y'
	AND yearID BETWEEN 1990 AND 2000
ORDER BY
	yearID DESC
LIMIT 10;
playerID yearid

andersp01

2000

mcphebi01

2000

steartu99

2000

cepedor01

1999

chylane99

1999

seleefr99

1999

willijo99

1999

davisge01

1998

dobyla01

1998

macphle99

1998

Using lahman, get a list of attendance by season for the Toronto Blue Jays (TOR). What season had the highest attendance?

Click to see solution

The Toronto Blue Jays were the 1993 World Series champion, meaning not all World Series-winning teams in MLB are American.

SELECT
	yearkey,
	attendance
FROM
	homegames
WHERE
	teamkey == 'TOR'
ORDER BY
	attendance DESC
LIMIT 10;
yearkey attendance

1993

4057747

1992

4028318

1991

4001526

1990

3884384

2016

3392099

2017

3203886

1994

2907949

1995

2826445

2015

2794891

1987

2778459

Using lahman, figure out how many different leagues have represented Major League Baseball over time.

Click to see solution

Major League Baseball has had several leagues that have been represented in its history, though the only leagues currently are the National League and the American League.

SELECT DISTINCT league FROM leagues;
league

American Association

American League

Federal League

Major League

National Association

National League

Players' League

Union Association

Using lahman, find the teams that have won the World Series.

Click to see solution
SELECT teamID, yearID FROM teams WHERE WSWin=='Y' LIMIT 10;
teamID yearID

PRO

1884

SL4

1886

DTN

1887

NY1

1888

NY1

1889

BOS

1903

NY1

1905

CHA

1906

CHN

1907

CHN

1908

Using lahman, list the top 10 seasons in terms of win totals. Include the yearID and the teamID.

Click to see solution
SELECT teamID, yearID, W FROM teams ORDER BY W DESC LIMIT 10;
teamID yearID W

CHN

1906

116

SEA

2001

116

NYA

1998

114

CLE

1954

111

PIT

1909

110

NYA

1927

110

NYA

1961

109

BAL

1969

109

BAL

1970

108

CIN

1975

108

Using lahman, list the pitchers with their teamID, wins (W), and losses (L) that threw complete games (CG) in the 1995 season. Include the number of complete games as well.

Click to see solution
SELECT
	playerID,
	teamID,
	W,
	L,
	CG
FROM
	pitching
WHERE
	CG > 0
	AND yearID == 1995
ORDER BY
	W DESC
LIMIT 10;
playerID teamID W L CG

maddugr01

ATL

19

2

10

mussimi01

BAL

19

9

7

johnsra05

SEA

18

2

6

schoupe01

CIN

18

7

2

martira02

LAN

17

7

4

rogerke01

TEX

17

7

3

glavito02

ATL

16

7

3

hershor01

CLE

16

6

1

nagych01

CLE

16

6

2

wakefti01

BOS

16

8

6

Using lahman, get a printout of the hits (H), and home runs (HR) for Ichiro Suzuki’s career. Answer this question using 2 queries. In the first query, find his playerID. In the second query, list the teamID, yearID, hits (H), and home runs (HR).

Click to see solution

Ichiro Suzuki is regarded as one of the greatest hitters of all time because of his prowess in both American and Japanese professional baseball.

SELECT
	playerID
FROM
	people
WHERE
	nameFirst == 'Ichiro'
	AND nameLast == 'Suzuki';

SELECT
	teamID,
	yearID,
	H,
	HR
FROM
	batting
WHERE
	playerID == 'suzukic01';
teamID yearID H HR

SEA

2001

242

8

SEA

2002

208

8

SEA

2003

212

13

SEA

2004

262

8

SEA

2005

206

15

SEA

2006

224

9

SEA

2007

238

6

SEA

2008

213

6

SEA

2009

225

11

SEA

2010

214

6

Using lahman, figure out how many walks (BB) and strikeouts (SO) Mariano Rivera had in the playoffs. Which year did he give up the most postseason walks? Answer this question using 2 queries.

Click to see solution

More men have walked on the moon than have scored a run on Mariano Rivera in a playoff game. He made the Hall of Fame in 2019.

SELECT
	playerID
FROM
	people
WHERE
	nameFirst == 'Mariano'
	AND nameLast == 'Rivera';

SELECT
	yearID,
	teamID,
	BB,
	SO
FROM
	pitchingpost
WHERE
	playerID == 'riverma01'
ORDER BY
	BB DESC;
yearID teamID BB SO

1996

NYA

3

4

2004

NYA

2

6

2009

NYA

2

4

2009

NYA

2

3

1995

NYA

1

8

1996

NYA

1

5

1996

NYA

1

1

1998

NYA

1

5

1998

NYA

1

2

1999

NYA

1

3

Using lahman, find the pitcher with the most strikeouts (SO), and the batter that struck out the most in the 2014 season. Get the first and last name of the pitcher and the batter. Use 3 queries to figure this out.

Click to see solution

Corey Kluber is a two-time AL Cy Young winner. He is well known for his two-seam fastball that is difficult to hit.

SELECT
	playerID,
	SO
FROM
	pitching
WHERE
	yearID == 2014
ORDER BY
	SO DESC
LIMIT (10);

SELECT
	playerID,
	SO
FROM
	batting
WHERE
	yearID == 2014
ORDER BY
	SO DESC
LIMIT (10);

SELECT
	nameFirst,
	nameLast
FROM
	people
WHERE
	playerID == "klubeco01"
	OR playerID == "howarry01";
nameFirst nameLast

Ryan

Howard

Corey

Kluber

Using lahman, figure out how many teams Bartolo Colon pitched for.

Click to see solution

Bartolo Colon is a well-known journeyman pitcher in baseball. He has pitched with a lot of teams, but it wasn’t until he played for the New York Mets that needed to come to the plate. He had a weird batting stance that is funny to watch. He even hit a home run one season!

SELECT
	playerID
FROM
	people
WHERE
	nameFirst == 'Bartolo'
	AND nameLast == 'Colon';

SELECT COUNT(DISTINCT teamID) FROM pitching WHERE playerID == 'colonba01';
COUNT(DISTINCT teamID)

12

Using lahman, figure out how many times Trevor Bauer came to bat (AB) in 2016. How many hits did he get? Use 2 queries to answer this question.

Click to see solution

As with many in his position, Bauer has a larger reputation as a pitcher than as a hitter.

SELECT
	playerID
FROM
	people
WHERE
	nameFirst == "Trevor"
	AND nameLast == "Bauer";

SELECT
	AB,
	H
FROM
	batting
WHERE
	playerID == "bauertr01"
	AND yearID == "2016";
AB H

5

0

Using lahman, compare Mike Trout and Giancarlo Stanton by season. Who hit more RBI’s in a season? Who has been caught stealing (CS) most in a season? Use 3 queries to answer these questions.

Click to see solution

Mike Trout and Giancarlo Stanton are considered two of the of the best hitters in MLB for very different reasons. Trout is an all-around player known for being indispensible, where Stanton is known as a power hitter.

SELECT
	playerID,
	nameFirst,
	nameLast
FROM
	people
WHERE (nameFirst == 'Giancarlo'
	AND nameLast == 'Stanton')
	OR(nameFirst == 'Mike'
		AND nameLast == 'Trout');

SELECT
	playerID,
	yearID,
	teamID,
	RBI,
	CS
FROM
	batting
WHERE
	playerID == 'stantmi03'
	OR playerID == 'troutmi01'
ORDER BY
	RBI DESC
LIMIT 1;
playerID yearID teamID RBI CS

stantmi03

2017

MIA

132

2

SELECT
	playerID,
	yearID,
	teamID,
	RBI,
	CS
FROM
	batting
WHERE
	playerID == 'stantmi03'
	OR playerID == 'troutmi01'
ORDER BY
	CS DESC
LIMIT 1;
playerID yearID teamID RBI CS

troutmi01

2013

LAA

97

7

Using lahman, make a list of players who walked (BB) more than they struck out (SO) between 1980 and 1985. Of these players, who walked the most? Use the BETWEEN clause in your queries. Use multiple queries.

Click to see solution
SELECT
	playerID,
	yearID,
	teamID,
	BB,
	SO
FROM
	batting
WHERE
	BB > SO
LIMIT 10;
playerID yearID teamID BB SO

addybo01

1871

RC1

4

0

ansonca01

1871

RC1

2

1

barkeal01

1871

RC1

1

0

barnero01

1871

BS1

13

1

battijo01

1871

CL1

1

0

bealsto01

1871

WS3

2

0

bellast01

1871

TRO

9

2

berthha01

1871

WS3

4

2

biermch01

1871

FW1

1

0

birdge01

1871

RC1

3

2

SELECT nameFirst, nameLast FROM people WHERE playerID=='randowi01';
nameFirst nameLast

Willie

Randolph

Using lahman, figure out how many different National League (lgID == NL) catchers (C) won the gold glove between 1990 and 2000.

Click to see solution
SELECT DISTINCT
	playerID
FROM
	awardsplayers
WHERE
	awardID == 'Gold Glove'
	AND notes == 'C'
	AND lgID == 'NL'
	AND yearID BETWEEN 1990 AND 2000;
playerID

santibe01

pagnoto01

manwaki01

johnsch04

liebemi01

mathemi01

Using lahman, figure out how many different 3rd basemen played for the Seattle Mariners between 2000 and 2005. Who had the most errors?

Click to see solution
SELECT DISTINCT
	playerID,
	yearID,
	E
FROM
	fielding
WHERE
	yearID BETWEEN 2000 AND 2005
	AND teamID == 'SEA'
	AND POS == '3B'
ORDER BY
	E DESC
LIMIT 10;
playerID yearID E

guillca01

2000

17

bellda01

2001

14

beltrad01

2005

14

bellda01

2000

12

cirilje01

2002

9

leoneju01

2004

8

mclemma01

2001

7

spiezsc01

2004

7

bloomwi01

2004

5

mabryjo01

2000

4

SELECT
	nameFirst,
	nameLast
FROM
	people
WHERE
	playerID == 'camermi01';
nameFirst nameLast

Mike

Cameron

Using lahman, figure out what seasons Craig Biggio played catcher. Biggio was known for playing second base, but as you’ll see, that’s not all he played. Solve this problem using 2 queries.

Click to see solution
SELECT
	playerID
FROM
	people
WHERE
	nameFirst == 'Craig'
	AND nameLast == 'Biggio';
playerID

biggicr01

SELECT
	teamID,
	yearID,
	POS
FROM
	fielding
WHERE
	playerID == 'biggicr01'
	AND POS == 'C';
teamID yearID POS

HOU

1988

C

HOU

1989

C

HOU

1990

C

HOU

1991

C

HOU

2007

C

Using lahman, find the teams representing the National League that have won the World Series. Display the list with the yearID and the teamID in ascending order.

Click to see solution
SELECT
	teamID,
	yearID
FROM
	teams
WHERE
	WSWin == 'Y'
	AND lgID == 'NL'
ORDER BY
	yearID ASC
LIMIT 10;
teamID yearID

PRO

1884

DTN

1887

NY1

1888

NY1

1889

NY1

1905

CHN

1907

CHN

1908

PIT

1909

BSN

1914

CIN

1919

Using lahman, list the pitchers that threw at least one complete game (CG) in the 1995 season. Please include the wins and losses of the top 10 pitchers. Use the playerID of the pitcher who threw the most complete games to find out the name of the pitcher that had the most complete games.

Click to see solution
SELECT
	playerID,
	W,
	L,
	CG
FROM
	pitching
WHERE
	CG > 0
	AND yearID == 1995
ORDER BY
	CG DESC
LIMIT 10;
playerID W L CG

maddugr01

19

2

10

mcdowja01

15

10

8

ericksc01

9

4

7

leitema01

10

12

7

mussimi01

19

9

7

johnsra05

18

2

6

valdeis01

13

11

6

wakefti01

16

8

6

coneda01

9

6

5

fernaal01

12

8

5

SELECT
	nameFirst,
	nameLast
FROM
	people
WHERE
	playerID == 'maddugr01';
nameFirst nameLast

Greg

Maddux

From lahman, who was the most recent player manager?

Click to see solution
SELECT
	playerID,
	yearID
FROM
	managers
WHERE
	plyrMgr == 'Y'
ORDER BY
	yearID DESC
LIMIT 10;
playerID yearID

rosepe01

1986

rosepe01

1985

rosepe01

1984

kessido01

1979

torrejo01

1977

robinfr02

1976

robinfr02

1975

tappeel01

1962

bauerha01

1961

hemusso01

1959

SELECT
	nameFirst,
	nameLast
FROM
	people
WHERE
	playerID == 'rosepe01';
nameFirst nameLast

Pete

Rose

Using lahman, get the at-bats, home runs, and stolen bases for Roberto Clements by year in ascending order.

Click to see solution
SELECT
	playerID
FROM
	people
WHERE
	nameFirst == 'Roberto'
	AND nameLast == 'Clemente';
playerID

clemero01

SELECT
	yearID,
	AB,
	HR,
	SB
FROM
	battingpost
WHERE
	playerID == 'clemero01'
ORDER BY
	yearID ASC;
yearID AB HR SB

1960

29

0

0

1970

14

0

0

1971

18

0

0

1971

29

2

0

1972

17

1

0

Using lahman, get a list of distinct World Series winners during the years where Tom Lasorda managed the Los Angeles Dodgers (LAN). Find the years Tom Lasorda was the manager of LAN, then find the distinct teams that won a World Series during his tenure.

Click to see solution
SELECT
	playerID
FROM
	people
WHERE
	nameFirst == 'Tom'
	AND nameLast == 'Lasorda';
playerID

lasorto01

SELECT
	yearID
FROM
	managers
WHERE
	playerID == 'lasorto01'
LIMIT 10;
yearID

1976

1977

1978

1979

1980

1981

1982

1983

1984

1985

SELECT DISTINCT
	teamID
FROM
	teams
WHERE
	WSWin == 'Y'
	AND yearID BETWEEN 1976 AND 1996;
teamID

CIN

NYA

PIT

PHI

LAN

SLN

BAL

DET

KCA

NYN

Using lahman, figure out which teams Kenny Lofton stole more than 20 bases from in a single season. Limit results to seasons after the year 2000.

Click to see solution
SELECT
	playerID
FROM
	people
WHERE
	nameFirst == 'Kenny'
	AND nameLast == 'Lofton';
playerID

loftoke01

SELECT
	teamID,
	yearID,
	SB
FROM
	batting
WHERE
	playerID == 'loftoke01'
	AND SB > 20
	AND yearID > 2000;
teamID yearID SB

CHA

2002

22

PHI

2005

22

LAN

2006

32

TEX

2007

21

Using lahman, figure out how much the Tampa Bay Rays paid Wade Boggs in 1998. Who paid Boggs the most in a single season during his career?

Click to see solution
SELECT
	playerID
FROM
	people
WHERE
	nameFirst == 'Wade'
	AND nameLast == 'Boggs';
playerID

boggswa01

SELECT
	teamID,
	yearID,
	salary
FROM
	salaries
WHERE
	playerID == 'boggswa01'
	AND yearID == 1998;
teamID yearID salary

TBA

1998

1150000

SELECT
	teamID,
	yearID,
	salary
FROM
	salaries
WHERE
	playerID == 'boggswa01'
ORDER BY
	salary DESC
LIMIT 10;
teamID yearID salary

NYA

1995

4724316

NYA

1994

3200000

NYA

1993

2950000

BOS

1991

2750000

BOS

1992

2700000

NYA

1996

2050000

NYA

1997

2000000

BOS

1990

1900000

BOS

1989

1850000

BOS

1987

1675000

Using lahman, get the teamID, wins (W), and losses (L) for the National League in 2015.

Click to see solution
SELECT
	teamID,
	W,
	L
FROM
	teams
WHERE
	divID == 'E'
	AND lgID == 'NL'
	AND yearID == 2015
ORDER BY
	teamrank ASC;
teamID W L

NYN

90

72

WAS

83

79

MIA

71

91

ATL

67

95

PHI

63

99

Using lahman, make a list of teams, wins, losses, and years for National League East teams that have won the World Series. Which team had the most wins?

Click to see solution
SELECT
	teamID,
	yearID,
	W,
	L
FROM
	teams
WHERE
	lgID == 'NL'
	AND divID == 'E'
	AND WSWin == 'Y'
ORDER BY
	W DESC;
teamID yearID W L

NYN

1986

108

54

NYN

1969

100

62

PIT

1979

98

64

PIT

1971

97

65

WAS

2019

93

69

SLN

1982

92

70

FLO

1997

92

70

PHI

2008

92

70

PHI

1980

91

71

FLO

2003

91

71

Using lahman, get a playerID list of managers who won more games than they lost (better record than .500) between 1930 and 1950. Get the manager’s name and name of their team for the winningest (best record) manager in that time period.

Click to see solution
SELECT
	playerID,
	teamID,
	yearID,
	W,
	L
FROM
	managers
WHERE
	yearID BETWEEN 1930 AND 1950
	AND W > L
ORDER BY
	W DESC
LIMIT 10;
playerID teamID yearID W L

mackco01

PHA

1931

107

45

mccarjo99

NYA

1932

107

47

mccarjo99

NYA

1939

106

45

southbi01

SLN

1942

106

48

southbi01

SLN

1943

105

49

southbi01

SLN

1944

105

49

durocle01

BRO

1942

104

50

cronijo01

BOS

1946

104

50

mccarjo99

NYA

1942

103

51

mackco01

PHA

1930

102

52

SELECT
	nameFirst,
	nameLast
FROM
	people
WHERE
	playerID == 'mackco01';
nameFirst nameLast

Connie

Mack

SELECT
	franchName
FROM
	teamsfranchises
WHERE
	franchID == 'PHA';
franchName

Philadelphia Athletics

Using lahman, get the top 5 seasons for overall attendance for Florida teams (Florida Marlins, Tampa Bay Rays, and Miami Marlins). How many of the top 5 seasons by attendance have been since 2000?

Click to see solution
SELECT
	teamID,
	yearID,
	attendance
FROM
	teams
WHERE
	franchID == 'TBD'
	OR franchID == 'FLA'
ORDER BY
	attendance DESC
LIMIT 10;
teamID yearID attendance

FLO

1993

3064847

TBA

1998

2506293

FLO

1997

2364387

MIA

2012

2219444

FLO

1994

1937467

TBA

2009

1874962

FLO

2005

1852608

TBA

2010

1843445

TBA

2008

1811986

MIA

2015

1752235

Using lahman, figure out which pitcher has thrown the most shutouts (SHO) in the American League since 2010? What about for the National League? Get the first and last names of the pitchers.

Click to see solution
SELECT
	playerID,
	teamID,
	yearID,
	SHO
FROM
	pitching
WHERE
	yearID > 2010
	AND lgID == 'NL'
ORDER BY
	SHO DESC
LIMIT 10;
playerID teamID yearID SHO

leecl02

PHI

2011

6

dickera01

NYN

2012

3

alvarhe01

MIA

2014

3

wainwad01

SLN

2014

3

arrieja01

CHN

2015

3

kershcl01

LAN

2015

3

scherma01

WAS

2015

3

kershcl01

LAN

2016

3

carpech01

SLN

2011

2

garcija02

SLN

2011

2

SELECT
	playerID,
	teamID,
	yearID,
	SHO
FROM
	pitching
WHERE
	yearID > 2010
	AND lgID == 'AL'
ORDER BY
	SHO DESC
LIMIT 10;
playerID teamID yearID SHO

hernafe02

SEA

2012

5

hollade01

TEX

2011

4

shielja02

TBA

2011

4

harenda01

LAA

2011

3

vargaja01

SEA

2011

3

morrobr01

TOR

2012

3

colonba01

OAK

2013

3

masteju01

CLE

2013

3

porceri01

DET

2014

3

klubeco01

CLE

2017

3

SELECT
	nameFirst,
	nameLast
FROM
	people
WHERE
	playerID == 'leecl02'
	OR playerID == 'hernafe02';
nameFirst nameLast

Felix

Hernandez

Cliff

Lee