Temple University - PowerPoint PPT Presentation

About This Presentation
Title:

Temple University

Description:

Temple University CIS Dept. CIS661 Principles of Database Systems V. Megalooikonomou Relational Model SQL Part I (based on notes by Silberchatz,Korth, and ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 90
Provided by: vasilis
Learn more at: https://cis.temple.edu
Category:

less

Transcript and Presenter's Notes

Title: Temple University


1
Temple University CIS Dept.CIS661 Principles
of Database Systems
  • V. Megalooikonomou
  • Relational Model SQL Part I
  • (based on notes by Silberchatz,Korth, and
    Sudarshan and notes by C. Faloutsos at CMU)

2
General Overview - rel. model
  • Formal query languages
  • rel algebra and calculi
  • Commercial query languages
  • SQL (combination of rel algebra and calculus)
  • QBE, (QUEL)

3
Overview - detailed - SQL
  • Fundamental constructs and concepts
  • check users guide for particular implementation
  • DML
  • select, from, where, renaming
  • set operations
  • ordering
  • aggregate functions
  • nested subqueries
  • other parts DDL, view definition, embedded SQL,
    integrity, authorization, etc

4
DML
  • General form
  • select a1, a2, an
  • from r1, r2, rm
  • where P
  • order by .
  • group by
  • having

5
Reminder our Mini-U db
TAKES
SSN
c-id
grade
123
cis331
A
234
cis331
B
6
DML e.g.
  • find the ssn(s) of everybody called smith
  • select ssn
  • from student
  • where namesmith

7
DML - observation
  • General form
  • select a1, a2, an
  • from r1, r2, rm
  • where P
  • equivalent rel. algebra query?

8
DML - observation
  • General form
  • select a1, a2, an
  • from r1, r2, rm
  • where P

9
DML - observation
  • General form
  • select distinct a1, a2, an
  • from r1, r2, rm
  • where P

10
select clause
  • select distinct all name
  • from student
  • where addressmain

11
where clause
  • find ssn(s) of all smiths on main
  • select ssn
  • from student
  • where addressmain and
  • name smith

12
where clause
  • boolean operators (and, or, not, )
  • comparison operators (lt, gt, , )
  • and more

13
What about strings?
  • find student ssns who live on main (st or str
    or street i.e., main st or main str )

14
What about strings?
  • find student ssns who live on main (st or str
    or street)
  • select ssn
  • from student
  • where address like main
  • variable-length dont care
  • _ single-character dont care

15
from clause
  • find names of people taking cis351

16
from clause
  • find names of people taking cis351
  • select name
  • from student, takes
  • where ???

17
from clause
  • find names of people taking cis351
  • select name
  • from student, takes
  • where student.ssn takes.ssn and
  • takes.c-id cis351

18
renaming - tuple variables
  • find names of people taking cis351
  • select name
  • from ourVeryOwnStudent, studentTakingClasses
  • where ourVeryOwnStudent.ssn
  • studentTakingClasses.ssn
  • and studentTakingClasses.c-id cis351

19
renaming - tuple variables
  • find names of people taking cis351
  • select name
  • from ourVeryOwnStudent as S, studentTakingClasses
    as T
  • where S.ssn T.ssn
  • and T.c-id cis351

20
renaming - self-join
  • self -joins find Toms grandparent(s)

21
renaming - self-join
  • find grandparents of Tom (PC(p-id, c-id))
  • select gp.p-id
  • from PC as gp, PC
  • where gp.c-id PC.p-id
  • and PC.c-id Tom

22
renaming - theta join
  • find course names with more units than cis351
  • select c1.c-name
  • from class as c1, class as c2
  • where c1.units gt c2.units
  • and c2.c-id cis351

23
  • find course names with more units than cis351
  • select c1.name
  • from class as c1, class as c2
  • where c1.units gt c2.units
  • and c2.c-id cis351

24
Overview - detailed - SQL
  • DML
  • select, from, where
  • set operations
  • ordering
  • aggregate functions
  • nested subqueries
  • other parts DDL, view definition, embedded SQL,
    integrity, authorization, etc

25
set operations
  • find ssn of people taking both cis351 and cis331

26
set operations
  • find ssn of people taking both cis351 and cis331
  • select ssn
  • from takes
  • where c-idcis351 and
  • c-idcis331
  • ?

27
set operations
  • find ssn of people taking both cis351 and cis331
  • select ssn
  • from takes
  • where c-idcis351 and
  • c-idcis331

28
set operations
  • find ssn of people taking both cis351 and cis331
  • (select ssn from takes where c-idcis351 )
  • intersect
  • (select ssn from takes where c-idcis331 )
  • other ops union , except

29
Overview - detailed - SQL
  • DML
  • select, from, where
  • set operations
  • ordering
  • aggregate functions
  • nested subqueries
  • other parts DDL, embedded SQL, auth etc

30
Ordering
  • find student records, sorted in name order
  • select
  • from student
  • where

31
Ordering
  • find student records, sorted in name order
  • select
  • from student
  • order by name asc
  • asc is the default

32
Ordering
  • find student records, sorted in name order break
    ties by reverse ssn
  • select
  • from student
  • order by name, ssn desc

33
Overview - detailed - SQL
  • DML
  • select, from, where
  • set operations
  • ordering
  • aggregate functions
  • nested subqueries
  • other parts DDL, embedded SQL, auth etc

34
Aggregate functions
  • find avg grade, across all students
  • select ??
  • from takes

35
Aggregate functions
  • find avg grade, across all students
  • select avg(grade)
  • from takes
  • result a single number
  • Which other functions?

36
Aggregate functions
  • A sum, count, min, max (std)

37
Aggregate functions
  • find total number of enrollments
  • select count()
  • from takes

38
Aggregate functions
  • find total number of students in cis331
  • select count()
  • from takes
  • where c-idcis331

39
Aggregate functions
  • find total number of students in each course
  • select count()
  • from takes
  • where ???

SSN
c-id
grade
123
cis331
4
234
cis331
3
40
Aggregate functions
  • find total number of students in each course
  • select c-id, count()
  • from takes
  • group by c-id

SSN
c-id
grade
123
cis331
4
234
cis331
3
41
Aggregate functions
  • find total number of students in each course
  • select c-id, count()
  • from takes
  • group by c-id
  • order by c-id

SSN
c-id
grade
123
cis331
4
234
cis331
3
42
Aggregate functions
  • find total number of students in each course, and
    sort by count, decreasing
  • select c-id, count() as pop
  • from takes
  • group by c-id
  • order by pop desc

SSN
c-id
grade
123
cis331
4
234
cis331
3
c-id
pop
cis331
2
43
Aggregate functions- having
  • find students with GPA gt 3.0

SSN
c-id
grade
123
cis331
4
234
cis331
3
44
Aggregate functions- having
  • find students with GPA gt 3.0
  • select ???, avg(grade)
  • from takes
  • group by ???

SSN
c-id
grade
123
cis331
4
234
cis331
3
45
Aggregate functions- having
  • find students with GPA gt 3.0
  • select ssn, avg(grade)
  • from takes
  • group by ssn
  • ???

SSN
c-id
grade
123
cis331
4
234
cis331
3
46
Aggregate functions- having
  • find students with GPA gt 3.0
  • select ssn, avg(grade)
  • from takes
  • group by ssn
  • having avg(grade)gt3.0
  • having lt-gt where for groups

SSN
c-id
grade
123
cis331
4
234
cis331
3
47
Aggregate functions- having
  • find students and GPA,
  • for students with gt 5 courses
  • select ssn, avg(grade)
  • from takes
  • group by ssn
  • having count() gt 5

SSN
c-id
grade
123
cis331
4
234
cis331
3
48
Overview - detailed - SQL
  • DML
  • select, from, where, renaming
  • set operations
  • ordering
  • aggregate functions
  • nested subqueries
  • other parts DDL, view definition, embedded SQL,
    integrity, authorization, etc

49
DML
  • General form
  • select a1, a2, an
  • from r1, r2, rm
  • where P
  • order by .
  • group by
  • having

50
Reminder our Mini-U db
51
DML - nested subqueries
  • find names of students of cis351
  • select name
  • from student
  • where ...
  • ssn in the set of people that take cis351

52
DML - nested subqueries
  • find names of students of cis351
  • select name
  • from student
  • where ...
  • select ssn
  • from takes
  • where c-id cis351

53
DML - nested subqueries
  • find names of students of cis351
  • select name
  • from student
  • where ssn in (
  • select ssn
  • from takes
  • where c-id cis351)

54
DML - nested subqueries
  • in compares a value with a set of values
  • in can be combined with other boolean ops
  • it is redundant (but user friendly!)
  • select name
  • from student ..
  • where c-id cis351 .

55
DML - nested subqueries
  • in compares a value with a set of values
  • in can be combined with other boolean ops
  • it is redundant (but user friendly!)
  • select name
  • from student, takes
  • where c-id cis351 and
  • student.ssntakes.ssn

56
DML - nested subqueries
  • find names of students taking cis351 and living
    on main str
  • select name
  • from student
  • where addressmain str and ssn in
  • (select ssn from takes where c-id cis351)

57
DML - nested subqueries
  • in compares a value with a set of values
  • other operators like in ??

58
DML - nested subqueries
  • find student record with highest ssn
  • select
  • from student
  • where ssn
  • is greater than every other ssn

59
DML - nested subqueries
  • find student record with highest ssn
  • select
  • from student
  • where ssn greater than every
  • select ssn from student

60
DML - nested subqueries
  • find student record with highest ssn
  • select
  • from student
  • where ssn gt all (
  • select ssn from student)

almost correct
61
DML - nested subqueries
  • find student record with highest ssn
  • select
  • from student
  • where ssn gt all (
  • select ssn from student)

62
DML - nested subqueries
  • find student record with highest ssn - without
    nested subqueries?
  • select S1.ssn, S1.name, S1.address
  • from student as S1, student as S2
  • where S1.ssn gt S2.ssn
  • is not the answer (what does it give?)

63
DML - nested subqueries
S1
S2
S1 x S2
S1.ssngtS2.ssn
64
DML - nested subqueries
  • select S1.ssn, S1.name, S1.address
  • from student as S1, student as S2
  • where S1.ssn gt S2.ssn
  • gives all but the smallest ssn -
  • aha!

65
DML - nested subqueries
  • find student record with highest ssn - without
    nested subqueries?
  • select S1.ssn, S1.name, S1.address
  • from student as S1, student as S2
  • where S1.ssn lt S2.ssn
  • gives all but the highest - therefore.

66
DML - nested subqueries
  • find student record with highest ssn - without
    nested subqueries?
  • (select from student) except
  • (select S1.ssn, S1.name, S1.address
  • from student as S1, student as S2
  • where S1.ssn lt S2.ssn)

67
DML - nested subqueries
  • (select from student) except
  • (select S1.ssn, S1.name, S1.address
  • from student as S1, student as S2
  • where S1.ssn lt S2.ssn)

select from student where ssn gt all
(select ssn from student)
68
DML - nested subqueries
  • Drill Even more readable than
  • select from student
  • where ssn gt all (select ssn from student)

69
DML - nested subqueries
  • Drill Even more readable than
  • select from student
  • where ssn gt all (select ssn from student)
  • select from student
  • where ssn in
  • (select max(ssn) from student)

70
DML - nested subqueries
  • Drill find the ssn of the student with the
    highest GPA

CLASS
c-id
c-name
units
cis331
d.b.
2
cis321
o.s.
2
grade
234
cis331
B
71
DML - nested subqueries
  • Drill find the ssn and GPA of the student with
    the highest GPA
  • select ssn, avg(grade) from takes
  • where

72
DML - nested subqueries
  • Drill find the ssn and GPA of the student with
    the highest GPA
  • select ssn, avg(grade) from takes
  • group by ssn
  • having avg( grade) ...
  • greater than every other GPA on file

73
DML - nested subqueries
  • Drill find the ssn and GPA of the student with
    the highest GPA
  • select ssn, avg(grade) from takes
  • group by ssn
  • having avg( grade) gt all
  • ( select avg( grade )
  • from student group by ssn )

all GPAs
74
DML - nested subqueries
  • in and gt all compares a value with a set of
    values
  • other operators like these?

75
DML - nested subqueries
  • ltall(), ltgtall() ...
  • ltgtall is identical to not in
  • gtsome(), gt some () ...
  • some() is identical to in
  • exists

76
DML - nested subqueries
  • Drill for exists find all courses that nobody
    enrolled in
  • select c-id from class .with no tuples in
    takes

CLASS
TAKES
c-id
c-name
units
SSN
c-id
grade
cis331
d.b.
2
123
cis331
A
cis321
o.s.
2
234
cis331
B
77
DML - nested subqueries
  • Drill for exists find all courses that nobody
    enrolled in
  • select c-id from class
  • where not exists
  • (select from takes
  • where class.c-id takes.c-id)

78
DML - derived relations
  • find the ssn with the highest GPA
  • select ssn, avg(grade) from takes
  • group by ssn
  • having avg( grade) gt all
  • ( select avg( grade )
  • from student group by ssn )

79
DML - derived relations
  • find the ssn with the highest GPA
  • Query would be easier, if we had a table like
    helpfulTable (ssn, gpa)
  • then what?

80
DML - derived relations
  • select ssn, gpa
  • from helpfulTable
  • where gpa in (select max(gpa)
  • from helpfulTable)

81
DML - derived relations
  • find the ssn with the highest GPA -
  • Query for helpfulTable (ssn, gpa)?

82
DML - derived relations
  • find the ssn with the highest GPA
  • Query for helpfulTable (ssn, gpa)?
  • select ssn, avg(grade)
  • from takes
  • group by ssn

83
DML - derived relations
  • find the ssn with the highest GPA

helpfulTable(ssn,gpa) select ssn,
avg(grade) from takes group by ssn
select ssn, gpa from helpfulTable where gpa
(select max(gpa) from
helpfulTable)
84
DML - derived relations
  • find the ssn with the highest GPA

select ssn, gpa from (select ssn, avg(grade)
from takes group by ssn)
as helpfulTable(ssn, gpa) where gpa in (select
max(gpa) from
helpfulTable)
85
Views
  • find the ssn with the highest GPA -
  • we can create a permanent, virtual table

create view helpfulTable(ssn, gpa) as
select ssn, avg(grade) from takes
group by ssn
86
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)

87
Overview of a DBMS
casual user
DBA
create view..
DML parser
DDL parser
DML precomp.
trans. mgr
buffer mgr
catalog
88
Overview - detailed - SQL
  • DML
  • select, from, where, renaming
  • set operations
  • ordering
  • aggregate functions
  • nested subqueries
  • other parts DDL, embedded SQL, auth etc

89
Overview - detailed - SQL
  • DML
  • other parts
  • modifications
  • joins
  • DDL
  • embedded SQL
  • authorization
Write a Comment
User Comments (0)
About PowerShow.com