Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

SQL The database you will use for this assignment contains information related t

ID: 3722053 • Letter: S

Question

SQL

           

The database you will use for this assignment contains information related to Major League Baseball (MLB) about players, teams, and games. The relations are:

Players(playerID, playerName, team, position, birthYear)

     playerID is a player identifier used in MLB, and all players throughout the history of baseball have a unique ID

     playerName is player’s name

     team is the name of the MLB team the player is currently playing on (or the last team the player played for if they are not currently playing)

     position is the position of the player

     birthYear is the year that player was born

Teams(teamID, teamName, home, leagueName)

     teamID is a unique ID internal to MLB.

     teamName is the name of the team

     home is the home city of the team

     leagueName is the league the team is in, i.e. either “National” or “American”, which stands for “National League” and “American League”, respectively

Games(gameID, homeTeamID, guestTeamID, date)

     gameID is a unique ID used internally in MLB

     homeTeamID is the ID of the hometeam

     guestTeamID is the ID of the visiting team

     date is the date of the game.

A sample instance of this database is given at the end of this homework handout. Since it is just one instance of the database designed to give you some intuition, you should not “customize” your answer to work only with this instance.

Write the following queries in SQL, using the schema provided above. (Note: Your queries must not be “state-dependent", that is, they should work without modification even if another instance of the database is given.)

(a) Print the names of all players who were born in 1970 and played for the Braves.

(b) Print the names of teams that do not have a pitcher.

(c) Print names of all players who have played in the National League.

(d) Print all gameIDs with Phillies as the home team.

Players

playerID

playerName

team

position

birthYear

1

Javy Lopez

Braves

Catcher

1970

2

Cliff Lee

Phillies

Pitcher

1978

3

Derek Jeter

Yankees

Infielder

1974

4

Skip Schumaker

Cardinals

Infielder

1980

5

Dominic Brown

Phillies

Outfielder

1987

Teams

teamID

teamName

home

leagueName

1

Phillies

Philadelphia

National

2

Braves

Atlanta

National

3

Yankees

New York

American

4

Twins

Minnesota

American

5

Rangers

Texas

American

6

Cubs

Chicago

National

Games

gameID

homeTeamID

guestTeamID

date

1

3

6

04/21/2010

2

1

4

04/21/2010

3

2

5

04/30/2010

4

6

3

05/02/2010

5

4

5

05/02/2010

6

1

5

05/06/2010

playerID

playerName

team

position

birthYear

1

Javy Lopez

Braves

Catcher

1970

2

Cliff Lee

Phillies

Pitcher

1978

3

Derek Jeter

Yankees

Infielder

1974

4

Skip Schumaker

Cardinals

Infielder

1980

5

Dominic Brown

Phillies

Outfielder

1987

Explanation / Answer

Answer)

(a) Print the names of all players who were born in 1970 and played for the Braves.

select * from Players where birthYear=1970 and team='Braves';

//This is quite a simple query using and condition

(b) Print the names of teams that do not have a pitcher.

select teamName from Teams where teamName not in (

select distinct team from Players where position='Pitcher');

//Here we use subqueries to find out the team where pitchers are there, then filter those results which do not have any pitchers from the Teams table.

(c) Print names of all players who have played in the National League.

select playerName from Players where team in (select teamName from Teams where leagueName='National');

//Here we use subqueries to list out the teamNames where they have played National leagues and select the playerName from those Teams.

(d) Print all gameIDs with Phillies as the home team.

select gameID from Games where homeTeamID in (select teamID from Teams where teamName='Phillies');

//Here we have the subquery to query the teamId where teamName is Phillies. This in turn helps us to find the gameID where the homeTeamId is the teamID foreign key to primary key relationship.