SlideShare a Scribd company logo
1 of 11
HW 2 - 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.
1. (10 points each) 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.
2. (15 points each) Write the following queries in SQL, using
the schema provided
above.
(a) Print all teamIDs where the team played against the Phillies
but not against the Braves.
(b) Print all tuples (playerID1, playerID2, team) where
playerID1 and playerID2 are (or have
been) on the same team. Avoid listing self-references or
duplicates, e.g. do not allow
(1,1,”Braves”) or both (2,5,”Phillies”) and (5,2,”Phillies”).
(c) Print all tuples (teamID1, league1, teamID2, league2, date)
where teamID1 and teamID2
played against each other in a World Series game. Although
there is no direct information
about the World Series games in the relations, we can infer that
when two teams from different
leagues play each other, it is a World Series game. So, in this
relation, league1 and league2
should be different leagues.
(d) List all cities that have a team in all leagues. For example,
there are currently two leagues
(National and American). Although not shown in this instance,
New York is home to the Mets in
the National league as well as the Yankees in the American
league (Chicago also has one in
each league, for those of you who are baseball fans).
Remember that your query must work
over all instances of this schema, even if there are more than
two leagues in the instance.
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
HW1 - Relational Algebra
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.
1. (5 points each) Consider the schema given above:
(a) Give a primary key for each relation. Are there any relations
for which there is an alternate
candidate key which you have not chosen as the primary key? If
yes, mention the relations,
candidate keys and the reason (if any) for your choice of the
primary key.
(b) State all referential integrity constraints (inclusion
dependencies) that should hold on these
relations.
(c) Note that there is no way to represent the fact that a player
may have played on several
different teams (for example, Javy Lopez played for the Braves,
Orioles and RedSox before
retiring), or that they are currently retired. How would you
modify the schema to take this into
account? (Hint: try to do it in a way that information is not
repeated unnecessarily.)
------------------------
For the next parts, if a query is long, feel free to break it up into
a series of queries with
intermediate answers stored in temporary relations (e.g. “let
temp =.....”). You may also use just
the first letter of each relation name since they are unique (e.g.
“P" for “Players"). Also, for the
ease of typing, you can use words for operations (e.g., ‘proj’ for
projection)
Note: Your queries must not be “state-dependent", that is, they
should work without modification
even if another instance of the database is given.
2. (8 points each) Write the following queries in relational
algebra, using the
schema provided above.
(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.
(e) Print all teamIDs where the team played against the Phillies
but not against the Braves.
3. (15 points each) Write the following queries in relational
algebra, using the
schema provided above.
(a) Define a relation Members(playerID1, playerID2, team)
where playerID1 and playerID2 are
(or have been) on the same team. Avoid listing self-references
or duplicates, e.g. do not allow
(1,1,”Braves”) or both (2,5,”Phillies”) and (5,2,”Phillies”).
(b) Define a relation WorldSeries(teamID1, league1, teamID2,
league2, date) where teamID1
and teamID2 played against each other in a World Series game.
Although there is no direct
information about the World Series in the relations, we can
infer that when two teams from
different leagues play each other, it is a World Series game. So,
in this relation, league1 and
league2 should be different leagues.
(c) Define a relation AllLeagues(city) for which each city has a
team in all leagues. For
example, there are currently two leagues (National and
American). Although not shown in this
instance, New York is home to the Mets in the National league
as well as the Yankees in the
American league (Chicago also has one in each league, for those
of you who are baseball
fans). Remember that your query must work over all instances
of this schema, even if there
are more than two leagues in the instance.
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

More Related Content

More from wellesleyterresa

HW in teams of 3 studentsAn oil remanufacturing company uses c.docx
HW in teams of 3 studentsAn oil remanufacturing company uses c.docxHW in teams of 3 studentsAn oil remanufacturing company uses c.docx
HW in teams of 3 studentsAn oil remanufacturing company uses c.docxwellesleyterresa
 
HW 5.docxAssignment 5 – Currency riskYou may do this assig.docx
HW 5.docxAssignment 5 – Currency riskYou may do this assig.docxHW 5.docxAssignment 5 – Currency riskYou may do this assig.docx
HW 5.docxAssignment 5 – Currency riskYou may do this assig.docxwellesleyterresa
 
HW#3 – Spring 20181. Giulia is traveling from Italy to China. .docx
HW#3 – Spring 20181. Giulia is traveling from Italy to China. .docxHW#3 – Spring 20181. Giulia is traveling from Italy to China. .docx
HW#3 – Spring 20181. Giulia is traveling from Italy to China. .docxwellesleyterresa
 
HW 2Due July 1 by 500 PM.docx
HW 2Due July 1 by 500 PM.docxHW 2Due July 1 by 500 PM.docx
HW 2Due July 1 by 500 PM.docxwellesleyterresa
 
HW 4 Gung Ho Commentary DUE Thursday, April 20 at 505 PM on.docx
HW 4 Gung Ho Commentary DUE Thursday, April 20 at 505 PM on.docxHW 4 Gung Ho Commentary DUE Thursday, April 20 at 505 PM on.docx
HW 4 Gung Ho Commentary DUE Thursday, April 20 at 505 PM on.docxwellesleyterresa
 
HW 5 Math 405. Due beginning of class – Monday, 10 Oct 2016.docx
HW 5 Math 405. Due beginning of class – Monday, 10 Oct 2016.docxHW 5 Math 405. Due beginning of class – Monday, 10 Oct 2016.docx
HW 5 Math 405. Due beginning of class – Monday, 10 Oct 2016.docxwellesleyterresa
 
HW 5-RSAascii2str.mfunction str = ascii2str(ascii) .docx
HW 5-RSAascii2str.mfunction str = ascii2str(ascii)        .docxHW 5-RSAascii2str.mfunction str = ascii2str(ascii)        .docx
HW 5-RSAascii2str.mfunction str = ascii2str(ascii) .docxwellesleyterresa
 
HW 3 Project Control• Status meeting agenda – shows time, date .docx
HW 3 Project Control• Status meeting agenda – shows time, date .docxHW 3 Project Control• Status meeting agenda – shows time, date .docx
HW 3 Project Control• Status meeting agenda – shows time, date .docxwellesleyterresa
 
HW 1January 19 2017Due back Jan 26, in class.1. (T.docx
HW 1January 19 2017Due back Jan 26, in class.1. (T.docxHW 1January 19 2017Due back Jan 26, in class.1. (T.docx
HW 1January 19 2017Due back Jan 26, in class.1. (T.docxwellesleyterresa
 
Hussam Malibari Heckman MAT 242 Spring 2017Assignment Chapte.docx
Hussam Malibari Heckman MAT 242 Spring 2017Assignment Chapte.docxHussam Malibari Heckman MAT 242 Spring 2017Assignment Chapte.docx
Hussam Malibari Heckman MAT 242 Spring 2017Assignment Chapte.docxwellesleyterresa
 
hw1.docxCS 211 Homework #1Please complete the homework problem.docx
hw1.docxCS 211 Homework #1Please complete the homework problem.docxhw1.docxCS 211 Homework #1Please complete the homework problem.docx
hw1.docxCS 211 Homework #1Please complete the homework problem.docxwellesleyterresa
 
HUS 335 Interpersonal Helping SkillsCase Assessment FormatT.docx
HUS 335 Interpersonal Helping SkillsCase Assessment FormatT.docxHUS 335 Interpersonal Helping SkillsCase Assessment FormatT.docx
HUS 335 Interpersonal Helping SkillsCase Assessment FormatT.docxwellesleyterresa
 
HW #1Tech Alert on IT & Strategy (Ch 3-5Ch 3 -5 IT Strategy opt.docx
HW #1Tech Alert on IT & Strategy (Ch 3-5Ch 3 -5 IT Strategy opt.docxHW #1Tech Alert on IT & Strategy (Ch 3-5Ch 3 -5 IT Strategy opt.docx
HW #1Tech Alert on IT & Strategy (Ch 3-5Ch 3 -5 IT Strategy opt.docxwellesleyterresa
 
HW 2 (1) Visit Monsanto (httpwww.monsanto.com) again and Goog.docx
HW 2 (1) Visit Monsanto (httpwww.monsanto.com) again and Goog.docxHW 2 (1) Visit Monsanto (httpwww.monsanto.com) again and Goog.docx
HW 2 (1) Visit Monsanto (httpwww.monsanto.com) again and Goog.docxwellesleyterresa
 
Hunters Son Dialogue Activity1. Please write 1-2 sentences for e.docx
Hunters Son Dialogue Activity1. Please write 1-2 sentences for e.docxHunters Son Dialogue Activity1. Please write 1-2 sentences for e.docx
Hunters Son Dialogue Activity1. Please write 1-2 sentences for e.docxwellesleyterresa
 
Humanities Commons Learning Goals1. Write about primary and seco.docx
Humanities Commons Learning Goals1. Write about primary and seco.docxHumanities Commons Learning Goals1. Write about primary and seco.docx
Humanities Commons Learning Goals1. Write about primary and seco.docxwellesleyterresa
 
HURRICANE KATRINA A NATION STILL UNPREPARED .docx
HURRICANE KATRINA  A NATION STILL UNPREPARED   .docxHURRICANE KATRINA  A NATION STILL UNPREPARED   .docx
HURRICANE KATRINA A NATION STILL UNPREPARED .docxwellesleyterresa
 
Humanities 115Short Essay Grading CriteriaExcellentPassing.docx
Humanities 115Short Essay Grading CriteriaExcellentPassing.docxHumanities 115Short Essay Grading CriteriaExcellentPassing.docx
Humanities 115Short Essay Grading CriteriaExcellentPassing.docxwellesleyterresa
 
HUMAN RESOURCES’ ROLE IN SUCCESSION PLANNING Succession planni.docx
HUMAN RESOURCES’ ROLE IN SUCCESSION PLANNING Succession planni.docxHUMAN RESOURCES’ ROLE IN SUCCESSION PLANNING Succession planni.docx
HUMAN RESOURCES’ ROLE IN SUCCESSION PLANNING Succession planni.docxwellesleyterresa
 
Humanities vs. Human Inquiry· · Due Feb 08, 559 PM · Graded.docx
Humanities vs. Human Inquiry· · Due Feb 08, 559 PM · Graded.docxHumanities vs. Human Inquiry· · Due Feb 08, 559 PM · Graded.docx
Humanities vs. Human Inquiry· · Due Feb 08, 559 PM · Graded.docxwellesleyterresa
 

More from wellesleyterresa (20)

HW in teams of 3 studentsAn oil remanufacturing company uses c.docx
HW in teams of 3 studentsAn oil remanufacturing company uses c.docxHW in teams of 3 studentsAn oil remanufacturing company uses c.docx
HW in teams of 3 studentsAn oil remanufacturing company uses c.docx
 
HW 5.docxAssignment 5 – Currency riskYou may do this assig.docx
HW 5.docxAssignment 5 – Currency riskYou may do this assig.docxHW 5.docxAssignment 5 – Currency riskYou may do this assig.docx
HW 5.docxAssignment 5 – Currency riskYou may do this assig.docx
 
HW#3 – Spring 20181. Giulia is traveling from Italy to China. .docx
HW#3 – Spring 20181. Giulia is traveling from Italy to China. .docxHW#3 – Spring 20181. Giulia is traveling from Italy to China. .docx
HW#3 – Spring 20181. Giulia is traveling from Italy to China. .docx
 
HW 2Due July 1 by 500 PM.docx
HW 2Due July 1 by 500 PM.docxHW 2Due July 1 by 500 PM.docx
HW 2Due July 1 by 500 PM.docx
 
HW 4 Gung Ho Commentary DUE Thursday, April 20 at 505 PM on.docx
HW 4 Gung Ho Commentary DUE Thursday, April 20 at 505 PM on.docxHW 4 Gung Ho Commentary DUE Thursday, April 20 at 505 PM on.docx
HW 4 Gung Ho Commentary DUE Thursday, April 20 at 505 PM on.docx
 
HW 5 Math 405. Due beginning of class – Monday, 10 Oct 2016.docx
HW 5 Math 405. Due beginning of class – Monday, 10 Oct 2016.docxHW 5 Math 405. Due beginning of class – Monday, 10 Oct 2016.docx
HW 5 Math 405. Due beginning of class – Monday, 10 Oct 2016.docx
 
HW 5-RSAascii2str.mfunction str = ascii2str(ascii) .docx
HW 5-RSAascii2str.mfunction str = ascii2str(ascii)        .docxHW 5-RSAascii2str.mfunction str = ascii2str(ascii)        .docx
HW 5-RSAascii2str.mfunction str = ascii2str(ascii) .docx
 
HW 3 Project Control• Status meeting agenda – shows time, date .docx
HW 3 Project Control• Status meeting agenda – shows time, date .docxHW 3 Project Control• Status meeting agenda – shows time, date .docx
HW 3 Project Control• Status meeting agenda – shows time, date .docx
 
HW 1January 19 2017Due back Jan 26, in class.1. (T.docx
HW 1January 19 2017Due back Jan 26, in class.1. (T.docxHW 1January 19 2017Due back Jan 26, in class.1. (T.docx
HW 1January 19 2017Due back Jan 26, in class.1. (T.docx
 
Hussam Malibari Heckman MAT 242 Spring 2017Assignment Chapte.docx
Hussam Malibari Heckman MAT 242 Spring 2017Assignment Chapte.docxHussam Malibari Heckman MAT 242 Spring 2017Assignment Chapte.docx
Hussam Malibari Heckman MAT 242 Spring 2017Assignment Chapte.docx
 
hw1.docxCS 211 Homework #1Please complete the homework problem.docx
hw1.docxCS 211 Homework #1Please complete the homework problem.docxhw1.docxCS 211 Homework #1Please complete the homework problem.docx
hw1.docxCS 211 Homework #1Please complete the homework problem.docx
 
HUS 335 Interpersonal Helping SkillsCase Assessment FormatT.docx
HUS 335 Interpersonal Helping SkillsCase Assessment FormatT.docxHUS 335 Interpersonal Helping SkillsCase Assessment FormatT.docx
HUS 335 Interpersonal Helping SkillsCase Assessment FormatT.docx
 
HW #1Tech Alert on IT & Strategy (Ch 3-5Ch 3 -5 IT Strategy opt.docx
HW #1Tech Alert on IT & Strategy (Ch 3-5Ch 3 -5 IT Strategy opt.docxHW #1Tech Alert on IT & Strategy (Ch 3-5Ch 3 -5 IT Strategy opt.docx
HW #1Tech Alert on IT & Strategy (Ch 3-5Ch 3 -5 IT Strategy opt.docx
 
HW 2 (1) Visit Monsanto (httpwww.monsanto.com) again and Goog.docx
HW 2 (1) Visit Monsanto (httpwww.monsanto.com) again and Goog.docxHW 2 (1) Visit Monsanto (httpwww.monsanto.com) again and Goog.docx
HW 2 (1) Visit Monsanto (httpwww.monsanto.com) again and Goog.docx
 
Hunters Son Dialogue Activity1. Please write 1-2 sentences for e.docx
Hunters Son Dialogue Activity1. Please write 1-2 sentences for e.docxHunters Son Dialogue Activity1. Please write 1-2 sentences for e.docx
Hunters Son Dialogue Activity1. Please write 1-2 sentences for e.docx
 
Humanities Commons Learning Goals1. Write about primary and seco.docx
Humanities Commons Learning Goals1. Write about primary and seco.docxHumanities Commons Learning Goals1. Write about primary and seco.docx
Humanities Commons Learning Goals1. Write about primary and seco.docx
 
HURRICANE KATRINA A NATION STILL UNPREPARED .docx
HURRICANE KATRINA  A NATION STILL UNPREPARED   .docxHURRICANE KATRINA  A NATION STILL UNPREPARED   .docx
HURRICANE KATRINA A NATION STILL UNPREPARED .docx
 
Humanities 115Short Essay Grading CriteriaExcellentPassing.docx
Humanities 115Short Essay Grading CriteriaExcellentPassing.docxHumanities 115Short Essay Grading CriteriaExcellentPassing.docx
Humanities 115Short Essay Grading CriteriaExcellentPassing.docx
 
HUMAN RESOURCES’ ROLE IN SUCCESSION PLANNING Succession planni.docx
HUMAN RESOURCES’ ROLE IN SUCCESSION PLANNING Succession planni.docxHUMAN RESOURCES’ ROLE IN SUCCESSION PLANNING Succession planni.docx
HUMAN RESOURCES’ ROLE IN SUCCESSION PLANNING Succession planni.docx
 
Humanities vs. Human Inquiry· · Due Feb 08, 559 PM · Graded.docx
Humanities vs. Human Inquiry· · Due Feb 08, 559 PM · Graded.docxHumanities vs. Human Inquiry· · Due Feb 08, 559 PM · Graded.docx
Humanities vs. Human Inquiry· · Due Feb 08, 559 PM · Graded.docx
 

Recently uploaded

Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 

HW 2 - SQL The database you will use for this assignme.docx

  • 1. HW 2 - 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.
  • 2. ● 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. 1. (10 points each) Write the following queries in SQL, using the schema provided above. (Note: Your queries must not be “state-dependent", that
  • 3. 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. 2. (15 points each) Write the following queries in SQL, using the schema provided above. (a) Print all teamIDs where the team played against the Phillies but not against the Braves. (b) Print all tuples (playerID1, playerID2, team) where playerID1 and playerID2 are (or have been) on the same team. Avoid listing self-references or duplicates, e.g. do not allow (1,1,”Braves”) or both (2,5,”Phillies”) and (5,2,”Phillies”). (c) Print all tuples (teamID1, league1, teamID2, league2, date)
  • 4. where teamID1 and teamID2 played against each other in a World Series game. Although there is no direct information about the World Series games in the relations, we can infer that when two teams from different leagues play each other, it is a World Series game. So, in this relation, league1 and league2 should be different leagues. (d) List all cities that have a team in all leagues. For example, there are currently two leagues (National and American). Although not shown in this instance, New York is home to the Mets in the National league as well as the Yankees in the American league (Chicago also has one in each league, for those of you who are baseball fans). Remember that your query must work over all instances of this schema, even if there are more than two leagues in the instance. Players playerID playerName team position birthYear 1 Javy Lopez Braves Catcher 1970
  • 5. 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
  • 6. 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 HW1 - Relational Algebra 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
  • 7. 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. 1. (5 points each) Consider the schema given above: (a) Give a primary key for each relation. Are there any relations for which there is an alternate candidate key which you have not chosen as the primary key? If yes, mention the relations, candidate keys and the reason (if any) for your choice of the primary key.
  • 8. (b) State all referential integrity constraints (inclusion dependencies) that should hold on these relations. (c) Note that there is no way to represent the fact that a player may have played on several different teams (for example, Javy Lopez played for the Braves, Orioles and RedSox before retiring), or that they are currently retired. How would you modify the schema to take this into account? (Hint: try to do it in a way that information is not repeated unnecessarily.) ------------------------ For the next parts, if a query is long, feel free to break it up into a series of queries with intermediate answers stored in temporary relations (e.g. “let temp =.....”). You may also use just the first letter of each relation name since they are unique (e.g. “P" for “Players"). Also, for the ease of typing, you can use words for operations (e.g., ‘proj’ for projection) Note: Your queries must not be “state-dependent", that is, they should work without modification even if another instance of the database is given. 2. (8 points each) Write the following queries in relational algebra, using the schema provided above.
  • 9. (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. (e) Print all teamIDs where the team played against the Phillies but not against the Braves. 3. (15 points each) Write the following queries in relational algebra, using the schema provided above. (a) Define a relation Members(playerID1, playerID2, team) where playerID1 and playerID2 are (or have been) on the same team. Avoid listing self-references or duplicates, e.g. do not allow (1,1,”Braves”) or both (2,5,”Phillies”) and (5,2,”Phillies”). (b) Define a relation WorldSeries(teamID1, league1, teamID2, league2, date) where teamID1 and teamID2 played against each other in a World Series game. Although there is no direct information about the World Series in the relations, we can infer that when two teams from different leagues play each other, it is a World Series game. So, in this relation, league1 and league2 should be different leagues. (c) Define a relation AllLeagues(city) for which each city has a team in all leagues. For example, there are currently two leagues (National and American). Although not shown in this instance, New York is home to the Mets in the National league
  • 10. as well as the Yankees in the American league (Chicago also has one in each league, for those of you who are baseball fans). Remember that your query must work over all instances of this schema, even if there are more than two leagues in the instance. 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
  • 11. 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