SQL Queries and Subqueries - Undergraduate...

Click here to load reader

  • date post

    29-Jun-2018
  • Category

    Documents

  • view

    252
  • download

    2

Embed Size (px)

Transcript of SQL Queries and Subqueries - Undergraduate...

  • SQL Queries and SubqueriesSQLQueriesandSubqueries

    ZakiMalik

    September 04, 2008September04,2008

  • Basic SQL QueryBasicSQLQuery

    SELECT[DISTINCT]targetlistFROMrelationlistWHERE qualification;q f ;

    Relationlist:Alistofrelationnames(possiblywithrangevariable after each name).variable aftereachname).

    Targetlist:Alistofattributesofrelationsinrelationlist Qualification:conditionsonattributes DISTINCT: optional keyword for duplicate removal DISTINCT:optionalkeywordforduplicateremoval.

    Default=noduplicateremoval!

  • SQL Comparison OperatorsSQL Comparison Operators

  • How to evaluate a query?Howtoevaluateaquery?

    SELECT[DISTINCT]targetlistFROMrelationlistWHERE qualification;WHERE qualification;

    Conceptualqueryevaluationusingrelationaloperators:1) Computethecrossproductofrelationlist.2) Discardresultingtuplesiftheyfailqualifications.3) Deleteattributesthatarenotintargetlist.(calledcolumnlist)4) If DISTINCT is specified, eliminate duplicate rows.4) IfDISTINCTisspecified,eliminateduplicaterows.

    SELECT S.snameFROM Sailors S Reserves RFROMSailorsS,ReservesRWHERE S.sid=R.sidANDR.bid=103;

  • Example of Conceptual Evaluation (1)ExampleofConceptualEvaluation(1)

    (1)Computethecrossproductofrelationlist.

    SELECTS.snameFROMSailorsS,ReservesRWHERES.sid=R.sidANDR.bid=103;

    Sailors Reserves

    sid sname rating age

    22 dustin 7 45.0

    sid bid day

    22 101 10/10/96X

    31 lubber 8 55.5

    58 rusty 10 35.0

    58 103 11/12/96X

  • E l f C t l E l ti (2)ExampleofConceptualEvaluation(2)

    (2) Di d t l if th f il SELECT S sname (2) Discard tuples if they fail qualifications.

    SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid=R.sid AND R.bid=103;

    Sailors X Reserves

    S.sid sname rating age R.sid bid day

    22 dustin 7 45.0 22 101 10/10/96

    Sailors X Reserves

    22 dustin 7 45.0 58 103 11/12/96

    31 lubber 8 55.5 22 101 10/10/96

    31 l bb 8 55 5 58 103 11/12/9631 lubber 8 55.5 58 103 11/12/96

    58 rusty 10 35.0 22 101 10/10/96

    58 rusty 10 35 0 58 103 11/12/9658 rusty 10 35.0 58 103 11/12/96

  • Example of Conceptual Evaluation (3)ExampleofConceptualEvaluation(3)

    (3) D l t tt ib t l th t SELECT S sname (3) Delete attribute columns that are not in target-list.

    SELECT S.snameFROM Sailors S, Reserves RWHERE S.sid=R.sid AND R.bid=103;

    sname

    (sid) sname rating age (sid) bid day

    Sailors X Reserves

    sname

    rusty

    (sid) sname rating age (sid) bid day

    22 dustin 7 45.0 22 101 10/10/96

    22 dustin 7 45.0 58 103 11/12/96

    31 lubber 8 55.5 22 101 10/10/96

    31 lubber 8 55.5 58 103 11/12/96

    58 rusty 10 35.0 22 101 10/10/96

    58 rusty 10 35.0 58 103 11/12/96

  • Renaming / AliasingRenaming/Aliasing

    Consider the following SALESREPS relation

    E l R ffiEmpl_num name age Rep_office manager105 Bill 37 13 104104 Bob 33 12 106106 Sam 52 11 NULL

    How do we determine the name of Bobs manager?

  • AliasingAliasing

    SELECT s2.nameFROM SALESREPS s1, SALESREPS s2WHERE 1 B b ANDWHERE s1.name=Bob AND

    s1.manager=s2.empl_num;

    Aliases must be used here. The row referenced by s1 is intended to be Boby while s2 will be his managers. Remember, first FROM, then WHERE, then SELECT

  • RelationalDesignExample Students(PID:string,Name:string,Address:string)

    Professors (PID t i Name t i Office t i Age i t DepartmentName Professors(PID:string,Name:string,Office:string,Age:integer,DepartmentName:string)

    Courses (Number: integer DeptName: string CourseName: string Classroom: Courses(Number:integer,DeptName:string,CourseName:string,Classroom:string,Enrollment:integer)

    Teach (ProfessorPID: string, Number: integer, DeptName: string)Teach(ProfessorPID:string,Number:integer,DeptName:string)

    Take(StudentPID:string,Number: integer,DeptName:string,Grade:string,ProfessorEvaluation:integer)g )

    Departments(Name:string,ChairmanPID:string)

    PreReq(Number:integer,DeptName:string,PreReqNumber:integer, PreReqDeptName:string)

  • MotivationforSubqueriesq

    Find the name of the professor who teaches CS 4604 Findthenameoftheprofessorwhoteaches CS4604.SELECTName

    FROMProfessors,TeachWHERE(PID=ProfessorPID)AND(Number=4604)

    AND(DeptName=CS);

    Doweneedtotakethenaturaljoinoftwobigrelationsjusttogetarelationwithonetuple?

    Canwerewritethequerywithoutusingajoin?

  • NestingNesting

    Aquerycanbeputinsideanotherquery Most commonly in the WHERE clauseMostcommonlyintheWHEREclause SometimesintheFROMclause(dependingonthesoftware) Thissubqueryisexecutedfirst(ifpossible)

  • Subquery ExampleSubqueryExample

    FindthenameoftheprofessorwhoteachesCS4604.SELECTName

    FROM ProfessorsFROMProfessors

    WHEREPID=

    (SELECTProfessorPID

    FROMTeach

    WHERE(Number=4604)AND(DeptName=CS)

    ););

    Whenusing=,thesubquerymustreturnasingletuple

  • ConditionsInvolvingRelationsSQL i l d b f h l SQLincludesanumberofoperatorsthatapplytoarelationandproduceabooleanresult.

    These operators are very useful to apply on results of subTheseoperatorsareveryusefultoapplyonresultsofsubqueries.

    LetRbearelationandtbeatuplewiththesamesetofbattributes.

    EXISTSRistrueifandonlyifRcontainsatleastonetuple. t IN R is true if and only if t equals a tuple in R.tIN RistrueifandonlyiftequalsatupleinR. t>ALL RistrueifandonlyifRisunary(hasoneattribute)and

    tisgreaterthaneveryvalueinR. Can use any of the other five comparison operatorsCanuseanyoftheotherfivecomparisonoperators. Ifweuse,Rneednotbeunary.

    t>ANYR(whichisunary)istrueifandonlyiftisgreaterthanatleast one value in R.leastonevalueinR.

    WecanuseNOT tonegateEXISTS,ALL,andANY.

  • Subqueries Using ConditionsSubqueriesUsingConditions

    FindthedepartmentsofthecoursestakenbythestudentwithnameSuri.

    SELECTDeptName

    FROMTakeFROM Take

    WHEREStudentPIDIN

    (SELECTPID

    FROMStudents

    WHERE(Name=Suri)

    );

  • Correlated vs UncorrelatedCorrelatedvsUncorrelated

    Theprevioussubqueriesdidnotdependonanythingoutsidethesubquery and thus need to be executed just onceandthusneedtobeexecutedjustonce. Thesearecalleduncorrelated.

    A correlated subquery depends on data from the outer query Acorrelated subquerydependsondatafromtheouterquery andthushastobeexecutedforeachrowoftheoutertable(s)

  • Correlated SubqueriesCorrelatedSubqueries

    Findcoursenamesthathavebeenusedfortwoormorecourses.

    SELECT CourseNameSELECTCourseName

    FROMCoursesASFirst

    WHERECourseNameIN

    (SELECTCourseName

    FROMCourses

    WHERE (N mber First N mber)WHERE(NumberFirst.Number)

    AND(DeptNameFirst.DeptName)

    );)

  • Evaluating Correlated SubqueriesEvaluatingCorrelatedSubqueriesSELECTCourseName

    FROM C AS Fi tFROMCoursesASFirst

    WHERECourseNameIN

    (SELECTCourseName

    FROMCourses

    WHERE(NumberFirst.Number)

    AND (DeptName First.DeptName)AND(DeptName First.DeptName)

    );

    EvaluatequerybyloopingovertuplesofFirst,andforeachtupleevaluatethesubquery.

    Scopingrules:anattributeinasubquerybelongstooneofthetuplevariablesinthatsubquerysFROMclause,ortothep q yimmediatelysurroundingsubquery,andsoon.

  • Subqueries in FROM clausesSubqueriesinFROMclauses CanuseasubqueryasarelationinaFROMclause. WemustgivesucharelationanaliasusingtheASkeyword. LetusfinddifferentwaysofwritingthequeryFindthenamesof

    Professors who have taught the student whose first name isProfessorswhohavetaughtthestudentwhosefirstnameisSuri.

    Theoldway:SELECT P f NSELECTProfessors.Name

    FROMProfessors,Take,Teach,Students

    WHERE(Professors.PID=Teach.ProfessorPID)

    AND(Teach.CourseNumber=Take.CourseNumber)

    AND(Teach.DeptName=Take.DeptName)

    AND (Take.StudentPID = Student.PID)AND(Take.StudentPID Student.PID)

    AND(Student.Name=Suri%);

  • Findthenamesof(Professorswhohavetaught(coursestakenb ( t d t ith fi t S i))) by(studentwithfirstnameSuri))).

    SELECT NameSELECTName

    FROMProfessors

    WHEREPIDIN

    (SELECT P f PID(SELECTProfessorPID

    FROMTeach

    WHERE(Number,DeptName)IN

    (SELECTNumber,DeptName

    FROMTakeWHEREStudentPIDIN

    (SELECT PID(SELECTPID

    FROMStudents

    WHEREName=Suri%)

    ))

    );

  • Aggregate OperatorsAggregateOperators

    COUNT(*)( ) COUNT([DISTINCT]A)

    Aisacolumn SUM ( [DISTINCT] A) SUM([DISTINCT]A) AVG([DISTINCT]A) MAX(A) MIN(A) Countthenumberofsailors

    SELECT COUNT (*)SELECTCOUNT( )FROMSailorsS

    21

  • Findtheaverageageofsailorswithrating=10

    Sailors(sid:integer,sname:string,rating:( g , g, ginteger,age:real)

    SELECTAVG(S.age)FROM Sailors SFROMSailorsS

    WHERES.rating=10

    22

  • Countthenumberofdifferentsailornames

    Sailors(sid:integer,sname:string,rating:( g , g, ginteger,age:real)

    SELECT COUNT (DISTINCT S sname)SELECTCOUNT(DISTINCTS.sname)FROMSailorsS

    23

  • Find the age of the oldest sailorFindtheageoftheoldestsailor

    Sailors(sid:integer,sname:string,rating:( g , g, ginteger,age:real)

    SELECT MAX(S AGE)SELECTMAX(S.AGE)FROMSailorsS

    24

  • Findnameandageoftheoldestsailor(s)

    SELECTS.sname,MAX(S.age)FROMSailorsS

    Thisisillegal,butwhy? Cannotcombineacolumnwithavalue

    SELECTS.sname,S.ageFROMSailorsS

    WHERE S (SELECT MAX (S2 ) FROM S il S2)WHERES.age=(SELECTMAX(S2.age)FROMSailorsS2)

    25

  • GROUP BY and HAVINGGROUPBY andHAVING

    Sofar,aggregateoperatorsareappliedtoall(qualifying), gg g p pp (q y g)tuples. Canweapplythemtoeachofseveralgroupsoftuples?

    E l fi d th f th t il f h ti Example:findtheageoftheyoungestsailorforeachratinglevel.

    Ingeneral,wedontknowhowmanyratinglevelsexist,andwhattheratingvaluesfortheselevelsare!

    Supposeweknowthatratingvaluesgofrom1to10;wecanwrite10queriesthatlooklikethis:

    SELECT MIN (S.age)FROM Sailors SFor i = 1, 2, ... , 10:

    26

    WHERE S.rating = i

  • Findtheageoftheyoungestsailorfor each rating levelforeachratinglevel

    SELECTS.rating,MIN(S.age)asageSid Sname Rating Age

    22 Dustin 7 45 0FROMSailorsSGROUPBYS.rating

    (1) The sailors tuples are put into same

    22 Dustin 7 45.0

    31 Lubber 8 55.5

    85 Art 3 25 5(1)Thesailorstuplesareputinto samerating groups.

    (2)ComputetheMinimumageforeach

    85 Art 3 25.5

    32 Andy 8 25.5

    95 Bob 3 63.5ratinggroup.

    Rating Age

    3 25.5 (1)3 25.5

    3 63.5

    7 45.0

    Rating Age

    3 25.5

    (1)

    (2)

    27

    8 55.5

    8 25.5

    7 45.0

    8 25.5

  • Findtheageoftheyoungestsailorforeachratinglevelthathasatleast2

    membersSELECTS.rating,MIN(S.age)as

    i

    Sid Sname Rating Age

    22 Dustin 7 45.0minage

    FROMSailorsSGROUPBYS.rating

    31 Lubber 8 55.5

    85 Art 3 25.5HAVINGCOUNT(*)>1

    1. Thesailorstuplesareputintosame rating groups

    32 Andy 8 25.5

    95 Bob 3 63.5samerating groups.

    2. Eliminategroupsthathave

  • Queries With GROUP BY and HAVINGQueriesWithGROUPBY andHAVINGSELECT[DISTINCT]targetlistFROMrelationlistWHEREqualificationGROUPBYgroupinglistHAVING group qualification

    SELECT S.rating, MIN (S.age) as ageFROM Sailors SGROUP BY S.rating

    HAVINGgroupqualification

    The targetlistcontains(i)attributenames(ii)termswithaggregate operations (e g AVG (S age))

    gHAVING S.rating > 5

    aggregateoperations(e.g.,AVG(S.age)). Theattributelist(e.g.,S.rating)intargetlistmustbein

    groupinglist.The attributes in group qualification must be in grouping list Theattributesingroupqualificationmustbeingroupinglist.

    29

  • Starwars ExercisesStarwarsExercises

    char(name,race,homeworld,affiliation)( , , , )

    planets(name,type,affiliation)

    timetable(cname,pname,movie,arrival,departure)

    WhichplanetdoesPrincessLeiagotoinmovie3?

    SELECTdistinctpname

    FROM i blFROMtimetable

    WHEREcname='PrincessLeia'andmovie=3;

    30

  • Starwars ExercisesStarwarsExercises

    char(name,race,homeworld,affiliation)planets(name,type,affiliation)

    timetable(cname,pname,movie,arrival,departure)

    HowmanyhumansstayonDagobahinmovie3?

    SELECT co nt(*)SELECTcount(*)FROMtimetable,charactersWHEREmovie=3andpname=Dagobahand

    timetable.cname=characters.nameandcharacters.race=Human;

    31

  • Starwars ExercisesStarwarsExercises

    char(name,race,homeworld,affiliation)planets(name,type,affiliation)

    timetable(cname,pname,movie,arrival,departure)

    Whohasbeentohis/herhomeworldinmovie2?

    SELECT distinct c nameSELECTdistinctc.nameFROMcharactersc,timetabletWHEREc.name=t.cnameandt.pname=c.homeworldand

    movie=2;

    32

  • Starwars ExercisesStarwarsExercises

    char(name,race,homeworld,affiliation)planets(name,type,affiliation)

    timetable(cname,pname,movie,arrival,departure)

    Finddistinctnamesoftheplanetsvisitedbythoseofracedroid.

    SELECTdistinctt.pnameFROMcharc,timetabletWHEREc.name=t.cnameandc.race=droid;

    33

  • Starwars ExercisesStarwarsExercises

    char(name,race,homeworld,affiliation)planets(name,type,affiliation)

    timetable(cname,pname,movie,arrival,departure)

    Foreachcharacterandforeachneutralplanet,howmuchtimetotaldidthecharacterspendontheplanet?

    SELECTc.name,p.name,SUM(t.departuret.arrival)asamountFROMcharactersc,timetablet,planetspWHEREt.cname=c.nameandt.pname=p.nameand

    p.affiliation='neutral'GROUP BY c.name, p.name;

    34

    GROUPBYc.name,p.name;