Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts...

89
Principles of Database Systems V. Megalooikonomou Relational Model – SQL Part I (based on notes by Silberchatz,Korth, and Sudarshan and notes by C. Faloutsos)

Transcript of Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts...

Page 1: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Principles of Database Systems

V. Megalooikonomou

Relational Model – SQL Part I

(based on notes by Silberchatz,Korth, and Sudarshan and notes by C. Faloutsos)

Page 2: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

General Overview - rel. model

Formal query languagesrel algebra and calculi

Commercial query languagesSQL (combination of rel algebra and calculus)QBE, (QUEL)

Page 3: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Overview - detailed - SQLFundamental constructs and concepts

check users’ guide for particular implementationDML

select, from, where, renamingset operationsorderingaggregate functionsnested subqueries

other parts: DDL, view definition, embedded SQL, integrity, authorization, etc

Page 4: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML

General formselect a1, a2, … anfrom r1, r2, … rmwhere P[order by ….][group by …][having …]

Page 5: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Reminder: our Mini-U db

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

TAKESSSN c-id grade

123 cis331 A234 cis331 B

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

Page 6: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML – e.g.:

find the ssn(s) of everybody called “smith”

select ssnfrom studentwhere name=“smith”

Page 7: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - observation

General formselect a1, a2, … anfrom r1, r2, … rmwhere P

equivalent rel. algebra query?

Page 8: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - observation

General formselect a1, a2, … anfrom r1, r2, … rmwhere P

))...21((,...2,1 rmrrPanaa ×××σπ

Page 9: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - observation

General formselect distinct a1, a2, … anfrom r1, r2, … rmwhere P

))...21((,...2,1 rmrrPanaa ×××σπ

Page 10: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

select clause

select [distinct | all ] namefrom studentwhere address=“main”

Page 11: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

where clause

find ssn(s) of all “smith”s on “main”select ssnfrom studentwhere address=“main” and

name = “smith”

Page 12: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

where clause

boolean operators (and, or, not, …)comparison operators (<, >, =, …)and more…

Page 13: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

What about strings?

find student ssns who live on “main” (stor str or street – i.e., “main st” or “main str” …)

Page 14: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

What about strings?

find student ssns who live on “main” (stor str or street) select ssnfrom studentwhere address like “main%”

%: variable-length don’t care_: single-character don’t care

Page 15: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

from clause

find names of people taking cis351

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

TAKESSSN c-id grade

123 cis331 A234 cis331 B

Page 16: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

from clause

find names of people taking cis351select namefrom student, takeswhere ???

Page 17: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

from clause

find names of people taking cis351select namefrom student, takeswhere student.ssn = takes.ssn and

takes.c-id = “cis351”

Page 18: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

renaming - tuple variables

find names of people taking cis351select namefrom ourVeryOwnStudent,

studentTakingClasseswhere ourVeryOwnStudent.ssn =

studentTakingClasses.ssnand studentTakingClasses.c-id = “cis351”

Page 19: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

renaming - tuple variables

find names of people taking cis351select namefrom ourVeryOwnStudent as S,

studentTakingClasses as Twhere S.ssn =T.ssn

and T.c-id = “cis351”

Page 20: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

renaming - self-join

self -joins: find Tom’s grandparent(s)

PCp-id c-idMary TomPeter MaryJohn Tom

PCp-id c-idMary TomPeter MaryJohn Tom

Page 21: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

renaming - self-join

find grandparents of “Tom” (PC(p-id, c-id))

select gp.p-idfrom PC as gp, PCwhere gp.c-id= PC.p-id

and PC.c-id = “Tom”

Page 22: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

renaming - theta join

find course names with more units than cis351

select c1.c-namefrom class as c1, class as c2where c1.units > c2.units

and c2.c-id = “cis351”

Page 23: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

find course names with more units than cis351select c1.namefrom class as c1, class as c2where c1.units > c2.units

and c2.c-id = “cis351”

])}[2][][1][2

351][1(21|{

nameccnamectunitscunitsc

cisidccCLASScCLASSct

−=−∧>

∧=−∈∃∈∃

Page 24: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Overview - detailed - SQL

DMLselect, from, whereset operationsorderingaggregate functionsnested subqueries

other parts: DDL, view definition, embedded SQL, integrity, authorization, etc

Page 25: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

set operations

find ssn of people taking both cis351 and cis331

TAKESSSN c-id grade

123 cis331 A234 cis331 B

Page 26: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

set operations

find ssn of people taking both cis351 and cis331

select ssnfrom takeswhere c-id=“cis351” and

c-id=“cis331”?

Page 27: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

set operations

find ssn of people taking both cis351 and cis331

select ssnfrom takeswhere c-id=“cis351” and

c-id=“cis331”

Page 28: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

set operationsfind ssn of people taking both cis351 and cis331

(select ssn from takes where c-id=“cis351” )intersect(select ssn from takes where c-id=“cis331” )

other ops: union , except

Page 29: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Overview - detailed - SQL

DMLselect, from, whereset operationsorderingaggregate functionsnested subqueries

other parts: DDL, embedded SQL, auth etc

Page 30: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Ordering

find student records, sorted in name orderselect *from studentwhere

Page 31: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Ordering

find student records, sorted in name orderselect *from studentorder by name asc

asc is the default

Page 32: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Ordering

find student records, sorted in name order; break ties by reverse ssnselect *from studentorder by name, ssn desc

Page 33: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Overview - detailed - SQL

DMLselect, from, whereset operationsorderingaggregate functionsnested subqueries

other parts: DDL, embedded SQL, auth etc

Page 34: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions

find avg grade, across all studentsselect ??from takes

SSN c-id grade123 cis331 4234 cis331 3

Page 35: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions

find avg grade, across all studentsselect avg(grade)from takes

result: a single numberWhich other functions?

SSN c-id grade123 cis331 4234 cis331 3

Page 36: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions

A: sum, count, min, max (std)

Page 37: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functionsfind total number of enrollments

select count(*)from takes

SSN c-id grade123 cis331 4234 cis331 3

Page 38: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions

find total number of students in cis331select count(*)from takeswhere c-id=“cis331”

SSN c-id grade123 cis331 4234 cis331 3

Page 39: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions

find total number of students in each courseselect count(*)from takeswhere ???

SSN c-id grade123 cis331 4234 cis331 3

Page 40: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions

find total number of students in each courseselect c-id, count(*)from takesgroup by c-id

SSN c-id grade123 cis331 4234 cis331 3

c-id countcis331 2

Page 41: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions

find total number of students in each courseselect c-id, count(*)from takesgroup by c-idorder by c-id

SSN c-id grade123 cis331 4234 cis331 3

c-id countcis331 2

Page 42: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions

find total number of students in each course, and sort by count, decreasingselect c-id, count(*) as popfrom takesgroup by c-idorder by pop desc

SSN c-id grade123 cis331 4234 cis331 3

c-id popcis331 2

Page 43: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions- ‘having’

find students with GPA > 3.0

SSN c-id grade123 cis331 4234 cis331 3

Page 44: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions- ‘having’

find students with GPA > 3.0select ???, avg(grade)from takesgroup by ??? SSN c-id grade

123 cis331 4234 cis331 3

Page 45: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions- ‘having’

find students with GPA > 3.0select ssn, avg(grade)from takesgroup by ssn???

SSN c-id grade123 cis331 4234 cis331 3

SSN avg(grade)123 4234 3

Page 46: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions- ‘having’

find students with GPA > 3.0select ssn, avg(grade)from takesgroup by ssnhaving avg(grade)>3.0

‘having’ <-> ‘where’ for groups

SSN c-id grade123 cis331 4234 cis331 3

SSN avg(grade)123 4234 3

Page 47: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Aggregate functions- ‘having’

find students and GPA,for students with > 5 courses

select ssn, avg(grade)from takesgroup by ssnhaving count(*) > 5

SSN c-id grade123 cis331 4234 cis331 3

SSN avg(grade)123 4234 3

Page 48: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Overview - detailed - SQL

DMLselect, from, where, renamingset operationsorderingaggregate functionsnested subqueries

other parts: DDL, view definition, embedded SQL, integrity, authorization, etc

Page 49: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML

General formselect a1, a2, … anfrom r1, r2, … rmwhere P[order by ….][group by …][having …]

Page 50: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Reminder: our Mini-U db

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

TAKESSSN c-id grade

123 cis331 A234 cis331 B

Page 51: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

find names of students of cis351select namefrom studentwhere ...

“ssn in the set of people that take cis351”

Page 52: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

find names of students of cis351select namefrom studentwhere ………...

select ssnfrom takeswhere c-id =“cis351”

Page 53: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

find names of students of cis351select namefrom studentwhere ssn in (

select ssnfrom takeswhere c-id =“cis351”)

Page 54: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

‘in’ compares a value with a set of values‘in’ can be combined with other boolean opsit is redundant (but user friendly!):select namefrom student …..where c-id = “cis351” ….

Page 55: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

‘in’ compares a value with a set of values‘in’ can be combined with other boolean opsit is redundant (but user friendly!):select namefrom student, takeswhere c-id = “cis351” and

student.ssn=takes.ssn

Page 56: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

find names of students taking cis351 and living on “main str”

select namefrom studentwhere address=“main str” and ssn in

(select ssn from takes where c-id =“cis351”)

Page 57: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

‘in’ compares a value with a set of valuesother operators like ‘in’ ??

Page 58: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

find student record with highest ssnselect *from studentwhere ssn

is greater than every other ssn

Page 59: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

find student record with highest ssnselect *from studentwhere ssn greater than every

select ssn from student

Page 60: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

find student record with highest ssnselect *from studentwhere ssn > all (

select ssn from student)almost correct

Page 61: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

find student record with highest ssnselect *from studentwhere ssn >= all (

select ssn from student)

Page 62: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

find student record with highest ssn -without nested subqueries?

select S1.ssn, S1.name, S1.addressfrom student as S1, student as S2where S1.ssn > S2.ssn

is not the answer (what does it give?)

Page 63: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

S1 S2

S1. ssn S2.ssn ….123 123 …234 123 …123 234234 234

S1 x S2

S1.ssn>S2.ssn

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

Page 64: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

select S1.ssn, S1.name, S1.addressfrom student as S1, student as S2where S1.ssn > S2.ssn

gives all but the smallest ssn -aha!

Page 65: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

find student record with highest ssn -without nested subqueries?

select S1.ssn, S1.name, S1.addressfrom student as S1, student as S2where S1.ssn < S2.ssn

gives all but the highest - therefore….

Page 66: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueriesfind student record with highest ssn - without

nested subqueries?(select * from student) except(select S1.ssn, S1.name, S1.addressfrom student as S1, student as S2where S1.ssn < S2.ssn)

Page 67: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

(select * from student) except(select S1.ssn, S1.name, S1.addressfrom student as S1, student as S2where S1.ssn < S2.ssn)

select *from studentwhere ssn >= all (select ssn from student)

Page 68: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

Drill: Even more readable than select * from studentwhere ssn >= all (select ssn fromstudent)

Page 69: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

Drill: Even more readable than select * from studentwhere ssn >= all (select ssn fromstudent)

select * from studentwhere ssn in(select max(ssn) from student)

Page 70: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

Drill: find the ssn of the student with the highest GPA

STUDENTSsn Name Address

123 smith main str234 jones forbes ave

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

grade

234 cis331 B

TAKESSSN c-id

123 cis331 A

Page 71: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

Drill: find the ssn and GPA of the student with the highest GPA

select ssn, avg(grade) from takeswhere

Page 72: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

Drill: find the ssn and GPA of the student with the highest GPA

select ssn, avg(grade) from takesgroup by ssnhaving avg( grade) …...

greater than every other GPA on file

Page 73: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

Drill: find the ssn and GPA of the student with the highest GPA

select ssn, avg(grade) from takesgroup by ssnhaving avg( grade) >= all

( select avg( grade )from student group by ssn )

} all GPAs

Page 74: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

‘in’ and ‘>= all’ compares a value with a set of valuesother operators like these?

Page 75: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

<all(), <>all() ...‘<>all’ is identical to ‘not in’>some(), >= some () ...‘= some()’ is identical to ‘in’exists

Page 76: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

Drill for ‘exists’: find all courses that nobody enrolled in

select c-id from class ….with no tuples in ‘takes’

CLASSc-id c-name unitscis331 d.b. 2cis321 o.s. 2

TAKESSSN c-id grade

123 cis331 A234 cis331 B

Page 77: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - nested subqueries

Drill for ‘exists’: find all courses that nobody enrolled in

select c-id from class where not exists

(select * from takeswhere class.c-id = takes.c-id)

Page 78: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - derived relations

find the ssn with the highest GPA

select ssn, avg(grade) from takesgroup by ssnhaving avg( grade) >= all

( select avg( grade )from student group by ssn )

Page 79: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - derived relations

find the ssn with the highest GPAQuery would be easier, if we had a table

like: helpfulTable (ssn, gpa):

then what?

HelpfulTableSsn Gpa123 3.5678 3.3

Page 80: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - derived relations

select ssn, gpafrom helpfulTablewhere gpa in (select max(gpa)

from helpfulTable)

HelpfulTableSsn Gpa123 3.5678 3.3

Page 81: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - derived relations

find the ssn with the highest GPA -Query for helpfulTable (ssn, gpa)?

Page 82: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - derived relations

find the ssn with the highest GPAQuery for helpfulTable (ssn, gpa)?

select ssn, avg(grade)from takesgroup by ssn

Page 83: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - derived relations

find the ssn with the highest GPA

helpfulTable(ssn,gpa)

select ssn, avg(grade)from takesgroup by ssn

select ssn, gpafrom helpfulTablewhere gpa = (select max(gpa)

from helpfulTable)

Page 84: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

DML - derived relations

find the ssn with the highest GPAselect ssn, gpafrom (select ssn, avg(grade)

from takesgroup by ssn)as helpfulTable(ssn, gpa)

where gpa in (select max(gpa) from helpfulTable)

Page 85: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Views

find the ssn with the highest GPA -we can create a permanent, virtual table:

create view helpfulTable(ssn, gpa) asselect ssn, avg(grade)from takesgroup by ssn

Page 86: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Views

views are recorded in the schema, for ever (i.e., until ‘drop view…’)typically, they take little disk space, because they are computed on the fly(but: materialized views…)

Page 87: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Overview of a DBMSDBAcasual

user

DML parser

buffer mgr

trans. mgr

DMLprecomp.

DDL parser

catalog

create view..

Page 88: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Overview - detailed - SQL

DMLselect, from, where, renamingset operationsorderingaggregate functionsnested subqueries

other parts: DDL, embedded SQL, auth etc

Page 89: Principles of Database Systems · Overview - detailed - SQL Fundamental constructs and concepts check users’ guide for particular implementation DML select, from, where, renaming

Overview - detailed - SQL

DMLother parts:

modificationsjoinsDDLembedded SQLauthorization