


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth exploration of sql queries and subqueries, focusing on the fundamental concepts of selection, comparison, and aggregation. Various aspects of sql queries, including relation-list, target-list, qualification, sql comparison operators, and aggregate operators. It also delves into the evaluation process of queries and the use of subqueries, correlated subqueries, and aggregate operators with the group by and having clauses.
Typology: Study notes
1 / 34
This page cannot be seen from the preview
Don't miss anything!



























-^ Qualification:^ conditions
-^ DISTINCT: optional keyword for duplicate removal•^ DISTINCT:^ optional^ keyword
-^ Default^ =^ no^ duplicate^ removal!
1)^ Compute^ the^ cross‐product
of^ relation‐list. 2)^ Discard^ resulting^ tuples^ if
they^ fail^ qualifications. 3)^ Delete^ attributes^ that^ are
not^ in^ target‐list.^ (called^ column
‐list) 4)^ If DISTINCT is specified, eliminate duplicate rows.4)^ If^ DISTINCT^ is^ specified,^ eliminate
duplicate^ rows. SELECT S.snameFROM^ Sailors S Reserves R FROM^ Sailors^ S,^ Reserves^ R WHERE S.sid=R.sid^ AND^ R.bid=103;
Example of Conceptual Evaluation (1)Example^ of^ Conceptual
Evaluation^ (1) (1) Compute^ the^ cross‐product^ of^ relation
‐ list. SELECT^ S.snameFROM^ Sailors^ S,^ Reserves^ R WHERE^ S.sid=R.sid^ AND^ R.bid=103;^ Sailors^
Reserves
Example of Conceptual Evaluation (3)Example^ of^ Conceptual
Evaluation^ (3) (3) D l t^ tt ib t^ l^ th t SELECT^ S sname^
(3) Delete attribute columns that arenot in target-list. SELECT^ S.snameFROM Sailors S, Reserves RWHERE S.sid=R.sid AND R.bid=103;
sname (sid)^ sname^ rating^ Sailors X Reservesage^ (sid)^ bid^ day
sname^ rusty (sid)^ sname^ rating^
age^ (sid)^ bid^ day 22 dustin^7
22 dustin^7
31 lubber^8
31 lubber^8
58 rusty^10
58 rusty^10
How do we determine the name of Bob’s manager?
Relational^ Design
Example
-^ 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 )
-^ Find the name of the professor who teaches
SELECT^ NameFROM^ Professors,^ TeachWHERE^ (PID^ =^ ProfessorPID)
-^ Do^ we^ need^ to^ take^ the
-^ Can^ we^ rewrite^ the^ query
SELECT^ NameFROM ProfessorsFROM^ Professors WHERE^ PID^ = (SELECT^ ProfessorPIDFROM^ TeachWHERE^ (Number
=^ 4604)^ AND^ (DeptName^ =^ ’CS’)););
-^ When^ using^ =,^ the^ subquery
-^ SQL^ includes^ a^ number
-^ These operators are very useful to apply on results of sub
-^ t IN R is true if and only if t equals a tuple in R.t^ IN^ R^ is^ true^ if^ and^ only^ if
-^ t^ >^ ALL R^ is^ true^ if^ and^ only
-^ t^ >^ ANY^ R^ (which^ is^ unary)^ is
true^ if^ and^ only^ if^ t^ is^ greater^
than^ at
-^ The^ previous^ subqueries
just^ once.
-^ These^ are^ called^ uncorrelated. • A correlated subquery depends on data from the outer query• A^ correlated^ subquery^ depends
-^ …^ and^ thus^ has^ to^ be^ executed
for^ each^ row^ of^ the^ outer^ table(s)
used^ for^ two^ or^ more^ courses. SELECT CourseNameSELECT^ CourseName FROM^ Courses^ AS^ FirstWHERE^ CourseName^ IN (SELECT
CourseNameFROM CoursesWHERE (N mber <> First N mber)WHERE (Number^ <>^ First.Number)AND (DeptName^ <>^ First.DeptName));)
-^ Can^ use^ a^ subquery^ as^ a
relation^ in^ a^ FROM^ clause.
-^ We^ must^ give^ such^ a^ relation
an^ alias^ using^ the^ AS^ keyword.
-^ Let^ us^ find^ different^ ways
of^ writing^ the^ query^ “Find^
the^ names^ of Professors who have taught the student whose first name isProfessors^ who^ have^ taught
the^ student^ whose^ first^ name
is ’Suri’.” • The^ old^ way:SELECT P^ f^ NSELECT^ Professors.NameFROM^ Professors,^ Take,^ Teach,^ StudentsWHERE^ (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 %’);
-^ “Find^ the^ names^ of^ (Professors
SELECT NameSELECT^ Name FROM^ ProfessorsWHERE^ PID^ IN (SELECT P^ f^ PID(SELECT^ ProfessorPIDFROM^ TeachWHERE^ (Number,^ DeptName)
IN ( SELECT^ Number,^ DeptNameFROM^ TakeWHERE^ StudentPID^ IN (SELECT PID(SELECT
PID FROM StudentsWHERE Name = ’Suri^ %’) )) );