SlideShare a Scribd company logo
1 of 52
Database Applications (15-415)
SQL-Part I
Lecture 8, January 30, 2018
Mohammad Hammoud
Today…
 Last Session:
 Relational Calculus
 Today’s Session:
 Standard Query Language (SQL)- Part I
 Announcements:
 PS2 is due on Sunday, Feb 04 by midnight
 Quiz I will be on Sunday, Feb 11
 P1 is due on Thursday, Feb 15 by midnight
 In this week’s recitation, we will practice on SQL
Outline
SQL Major Aspects
Basic SQL Queries
Set Operations
Aggregate Functions & Group By, Having and
Order By Clauses

SQL Major Aspects
 A major strength of the relational model is that it supports
simple and powerful querying of data
 Structured Query Language (SQL) is the most widely used
commercial relational database language
 SQL has several aspects to it:
1. Data Manipulation Language (DML)
 It allows users to pose queries and insert, delete
and modify rows
2. Data Definition Language (DDL)
 It allows users to create, delete, and modify tables and views
SQL Major Aspects
 SQL has several aspects to it:
3. Triggers and Advanced Integrity Constraints
 It supports “triggers”, which are actions executed by the
DBMS whenever changes to the database meet conditions
specified in triggers
4. Embedded and Dynamic Language
 Embedded SQL allows SQL code to be called from a host
language (e.g., Java)
 Dynamic SQL allows SQL queries to be constructed and
executed at run-time
SQL Major Aspects
 SQL has several aspects to it:
3. Triggers and Advanced Integrity Constraints
 It supports “triggers”, which are actions executed by the
DBMS whenever changes to the database meet conditions
specified in triggers
4. Embedded and Dynamic Language
 Embedded SQL allows SQL code to be called from a host
language (e.g., Java)
 Dynamic SQL allows SQL queries to be constructed and
executed at run-time
SQL Major Aspects
 SQL has several aspects to it:
5. Remote Database Access
 It allows connecting client programs to remote
database servers
6. Transaction Management
 It allows users to explicitly control aspects of how a
transaction is to be executed (later in the semester)
7. Security
 It provides mechanisms to control users’ accesses to data
objects (e.g., tables and views)
And others…
Outline
SQL Major Aspects
Basic SQL Queries
Set Operations
Aggregate Functions & Group By, Having and
Order By Clauses

Basic SQL Queries
 The basic form of an SQL query is as follows:
select a1, a2, … an
from r1, r2, … rm
where P
The Column-List
The Relation-List
Qualification (Optional)
Equivalence to Relational Algebra
 The basic form of an SQL query is as follows:
select a1, a2, … an
from r1, r2, … rm
where P
))
...
2
1
(
(
,...
2
,
1 rm
r
r
P
an
a
a 




join
Reminder: Our Mini-U DB
STUDENT
Ssn Name Address
123 smith main str
234 jones QF ave
CLASS
c-id c-name units
15-413 s.e. 2
15-412 o.s. 2
TAKES
SSN c-id grade
123 15-413 A
234 15-413 B
The WHERE Clause
 Find the ssn(s) of everybody called “smith”
select ssn
from student
where name=‘smith’
STUDENT
Ssn Name Address
123 smith main str
234 jones QF ave
The WHERE Clause
 Find ssn(s) of all “smith”s on “main”
select ssn
from student
where address=‘main’ and
name = ‘smith’
STUDENT
Ssn Name Address
123 smith main str
234 jones QF ave
The WHERE Clause
 Boolean operators (and, or, not)
 Comparison operators (<, ≤, >, ≥, =, ≠)
 And more…
What About Strings?
 Find student ssn(s) who live on “main” (st or
str or street – i.e., “main st” or “main str” or
“main street”)
select ssn
from student
where address like ‘main%’
%: Variable-length do not care (i.e., stands for 0 or more arbitrary characters)
_: Single-character do not care (i.e., stands for any 1 character)
Another Example on Pattern Matching
 Find the ages of sailors whose names begin and end
with B and have at least 3 characters
Sailors
Sid Sname Rating age
22 Dustin 7 45.0
29 Brutus 1 33.0
select S.age
from Sailors S
where S.sname like ‘B_%B’
The FROM Clause
 Find the names of students taking 15-415
STUDENT
Ssn Name Address
123 smith main str
234 jones QF ave
CLASS
c-id c-name units
15-413 s.e. 2
15-412 o.s. 2
TAKES
SSN c-id grade
123 15-413 A
234 15-413 B
2-way Join!
The FROM Clause
 Find the names of students taking 15-415
select Name
from STUDENT, TAKES
where ???
The FROM Clause
 Find the names of students taking 15-415
select Name
from STUDENT, TAKES
where STUDENT.ssn = TAKES.ssn
and TAKES.c-id = ‘15-415’
Renaming: Tuple Variables
 Find the names of students taking 15-415
select Name
from STUDENT as S, TAKES as T
where S.ssn = T.ssn
and T.c-id = “15-415”
Optional!
Renaming: Self-Joins
 Find Tom’s grandparent(s)
PC
p-id c-id
Mary Tom
Peter Mary
John Tom
PC
p-id c-id
Mary Tom
Peter Mary
John Tom
select gp.p-id
from PC as gp, PC
where gp.c-id= PC.p-id
and PC.c-id = ‘Tom’
More on Self-Joins
 Find names and increments for the ratings of persons
who have sailed two different boats on the same day
Sailors
Sid Sname Rating age
22 Dustin 7 45.0
29 Brutus 1 33.0
Reserves
Sid Bid Day
22 101 10/10/2013
22 102 10/10/2013
More on Self-Joins
 Find names and increments for the ratings of persons
who have sailed two different boats on the same day
Sailors
Sid Sname Rating age
22 Dustin 7 45.0
29 Brutus 1 33.0
Reserves
Sid Bid Day
22 101 10/10/2013
22 102 10/10/2013
select S.sname, S.rating+1 as rating
from Sailors S, Reserves R1, Reserves R2
where S.sid = R1.sid and S.sid = R2.sid
and R1.day = R2.day and R1.bid != R2.bid
Renaming: Theta Joins
 Find course names with more units than 15-415
select c1.c-name
from class as c1, class as c2
where c1.units > c2.units
and c2.c-id = ‘15-415’
CLASS
c-id c-name units
15-413 s.e. 2
15-412 o.s. 2
Outline
SQL Major Aspects
Basic SQL Queries
Set Operations
Aggregate Functions & Group By, Having and
Order By Clauses

Set Operations
 Find ssn(s) of students taking both 15-415 and 15-413
TAKES
SSN c-id grade
123 15-413 A
234 15-413 B
select ssn
from takes
where c-id=‘15-415’ and
c-id=‘15-413’
Set Operations
 Find ssn(s) of students taking both 15-415 and 15-413
TAKES
SSN c-id grade
123 15-413 A
234 15-413 B
(select ssn from takes where c-id=“15-415” )
intersect
(select ssn from takes where c-id=“15-413” )
Other operations: union , except
Set Operations
 Find ssn(s) of students taking 15-415 or 15-413
TAKES
SSN c-id grade
123 15-413 A
234 15-413 B
(select ssn from takes where c-id=“15-415” )
union
(select ssn from takes where c-id=“15-413” )
Set Operations
 Find ssn(s) of students taking 15-415 but not 15-413
TAKES
SSN c-id grade
123 15-413 A
234 15-413 B
(select ssn from takes where c-id=“15-415” )
except
(select ssn from takes where c-id=“15-413” )
Another Example on Set Operations
 Find the names of sailors who have reserved both a
red and a green boat
Sailors
Sid Sname Rating age
22 Dustin 7 45.0
29 Brutus 1 33.0
Reserves
Sid Bid Day
22 101 10/10/2013
22 102 10/11/2013
Boats
Bid Bname Color
101 Interlake Red
102 Clipper Green
Another Example on Set Operations
 Find the names of sailors who have reserved both a
red and a green boat
(select S.sname from Sailors S, Reserves R, Boats B
where S.sid = R.sid and R.bid = B.bid and B.color = ‘green’)
intersect
(select S2.sname from Sailors S2, Reserves R2, Boats B2
where S2.sid = R2.sid and R2.bid = B2.bid and B2.color = ‘red’)
The query contains a “subtle bug” which arises because we are using sname to
identify Sailors, and “sname” is not a key for Sailors!
We can compute the names of such Sailors using a NESTED query (which we
cover next lecture!)
Outline
SQL Major Aspects
Basic SQL Queries
Set Operations
Aggregate Functions & Group By, Having and
Order By Clauses 
Aggregate Functions
 Find average grade, across all students
SSN c-id grade
123 15-413 4
234 15-413 3
select ??
from takes
Aggregate Functions
 Find average grade, across all students
SSN c-id grade
123 15-413 4
234 15-413 3
select avg(grade)
from takes
Other functions: Count ([Distinct] A), Sum ([Distinct] A), Max (A), Min (A),
assuming column A
Aggregate Functions
 Find total number of enrollments
SSN c-id grade
123 15-413 4
234 15-413 3
select count(*)
from takes
Aggregate Functions
 Find total number of students in 15-415
SSN c-id grade
123 15-413 4
234 15-413 3
select count(*)
from takes
where c-id=‘15-415’
Aggregate Functions
 Find the name and age of the oldest sailor
select S.sname, max (S.age)
from Sailors S
This query is illegal in SQL- If the “select” clause uses an aggregate function, it
must use ONLY aggregate function unless the query contains a “group by” clause!
Sailors
Sid Sname Rating age
22 Dustin 7 45.0
29 Brutus 1 33.0
The GROUP BY and HAVING Clauses
 Find the age of the youngest sailor for each rating level
 In general, we do not know how many rating levels exist, and what
the rating values for these levels are!
 Suppose we know that rating values go from 1 to 10; we can write
10 queries that look like this (!):
Sailors
Sid Sname Rating age
22 Dustin 7 45.0
29 Brutus 1 33.0
SELECT MIN (S.age)
FROM Sailors S
WHERE S.rating = i
For i = 1, 2, ... , 10:
The GROUP BY and HAVING Clauses
 Find the age of the youngest sailor for each rating level
 Using the GROUP BY clause, we can write this query as follows:
Sailors
Sid Sname Rating age
22 Dustin 7 45.0
29 Brutus 1 33.0
select S.rating, min (S.age)
from Sailors S
group by S.rating
“Every” column that appears in the Column-List
“must” also appear in the Grouping-List
The Grouping-List
The GROUP BY and HAVING Clauses
 Find age of the youngest sailor with age ≥ 18, for each
rating level with at least 2 sailors
Sailors
Sid Sname Rating age
22 Dustin 7 45.0
29 Brutus 1 33.0
SELECT S.rating, MIN (S.age) AS minage
FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
HAVING COUNT (*) > 1
The GROUP BY and HAVING Clauses
 Find age of the youngest sailor with age ≥ 18, for each
rating level with at least 2 sailors
rating age
7 45.0
1 33.0
8 55.5
8 25.5
10 35.0
7 35.0
10 16.0
9 35.0
3 25.5
3 63.5
3 25.5
rating minage
3 25.5
7 35.0
8 25.5
rating age
1 33.0
3 25.5
3 63.5
3 25.5
7 45.0
7 35.0
8 55.5
8 25.5
9 35.0
10 35.0
The GROUP BY and HAVING Clauses
 Find age of the youngest sailor with age ≥ 18, for each
rating level with at least 2 sailors, and with every sailor
under 60
SELECT S.rating, MIN (S.age) AS minage
FROM Sailors S
WHERE S.age >= 18
GROUP BY S.rating
HAVING COUNT (*) > 1 AND EVERY (S.age <=60)
The GROUP BY and HAVING Clauses
 Find age of the youngest sailor with age ≥ 18, for each
rating level with at least 2 sailors, and with every sailor
under 60
rating age
7 45.0
1 33.0
8 55.5
8 25.5
10 35.0
7 35.0
10 16.0
9 35.0
3 25.5
3 63.5
3 25.5
rating age
1 33.0
3 25.5
3 63.5
3 25.5
7 45.0
7 35.0
8 55.5
8 25.5
9 35.0
10 35.0
rating minage
7 35.0
8 25.5
What would be the result if
we change EVERY to ANY in
“HAVING COUNT (*) > 1
AND EVERY (S.age <=60)”?
The GROUP BY and HAVING Clauses
 Find age of the youngest sailor with age ≥ 18, for each
rating level with at least 2 sailors between 18 and 60
SELECT S.rating, MIN (S.age) AS minage
FROM Sailors S
WHERE S.age >= 18 AND S.age <= 60
GROUP BY S.rating
HAVING COUNT (*) > 1
Will this give the same result as the previous query which uses the
EVERY clause?
Will this give the same result as the previous query which uses the
ANY clause?
select *
from student
where ??
The ORDER BY Clause
 Find student records, sorted in name order
select *
from student
order by name asc
The ORDER BY Clause
 Find student records, sorted in name order
asc is the default
select *
from student
order by name, ssn desc
The ORDER BY Clause
 Find student records, sorted in name order;
break ties by reverse ssn
More Examples
 Find the total number of students in each course
SSN c-id grade
123 15-413 4
234 15-413 3
select count(*)
from takes
where ???
More Examples
 Find the total number of students in each course
SSN c-id grade
123 15-413 4
234 15-413 3
select c-id, count(*)
from takes
group by c-id
c-id count
15-413 2
More Examples
 Find total number of students in each course,
and sort by count, in decreasing order
SSN c-id grade
123 15-413 4
234 15-413 3
select c-id, count(*) as pop
from takes
group by c-id
order by pop desc
c-id pop
15-413 2
Concluding Remarks
 SQL was an important factor in the early acceptance of
the relational model
 It is more natural than earlier procedural
query languages
 SQL is relationally complete; in fact, significantly more
expressive power than relational algebra
 Even queries that can be expressed in relational
algebra can often be expressed more naturally in SQL
Next Class
SQL- Part II

More Related Content

Similar to Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test

Dbms presentationnnnn
Dbms presentationnnnnDbms presentationnnnn
Dbms presentationnnnnsumi haque
 
Relational_Intro_1.ppt
Relational_Intro_1.pptRelational_Intro_1.ppt
Relational_Intro_1.pptIrfanRashid36
 
CA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxCA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxArvindShukla81
 
DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6YOGESH SINGH
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbmsVignesh Saravanan
 
intro-slides.pdf very important for computer science students
intro-slides.pdf very important for computer science studentsintro-slides.pdf very important for computer science students
intro-slides.pdf very important for computer science studentssairevanth504
 
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docxL1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docxDIPESH30
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing conceptskavitham66441
 
Database managment System Relational Algebra
Database managment System  Relational AlgebraDatabase managment System  Relational Algebra
Database managment System Relational AlgebraUttara University
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Prosanta Ghosh
 
Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL MuhammadWaheed44
 
Lecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptxLecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptxHanzlaNaveed1
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Deepak Singh
 

Similar to Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test (20)

2 rel-algebra
2 rel-algebra2 rel-algebra
2 rel-algebra
 
Rdbms (2)
Rdbms (2)Rdbms (2)
Rdbms (2)
 
Dbms presentationnnnn
Dbms presentationnnnnDbms presentationnnnn
Dbms presentationnnnn
 
Relational_Intro_1.ppt
Relational_Intro_1.pptRelational_Intro_1.ppt
Relational_Intro_1.ppt
 
Jdbc
Jdbc Jdbc
Jdbc
 
CA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptxCA2_CYS101_31184422012_Arvind-Shukla.pptx
CA2_CYS101_31184422012_Arvind-Shukla.pptx
 
DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6DEE 431 Introduction to MySql Slide 6
DEE 431 Introduction to MySql Slide 6
 
Integrity constraints in dbms
Integrity constraints in dbmsIntegrity constraints in dbms
Integrity constraints in dbms
 
intro-slides.pdf very important for computer science students
intro-slides.pdf very important for computer science studentsintro-slides.pdf very important for computer science students
intro-slides.pdf very important for computer science students
 
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docxL1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
L1 Intro to Relational DBMS LP.pdfIntro to Relational .docx
 
Algebra
AlgebraAlgebra
Algebra
 
data structure and c programing concepts
data structure and c programing conceptsdata structure and c programing concepts
data structure and c programing concepts
 
Database managment System Relational Algebra
Database managment System  Relational AlgebraDatabase managment System  Relational Algebra
Database managment System Relational Algebra
 
Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013Dbms ii mca-ch7-sql-2013
Dbms ii mca-ch7-sql-2013
 
Unit 04 dbms
Unit 04 dbmsUnit 04 dbms
Unit 04 dbms
 
Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL Basics of SELECT Statement - Oracle SQL
Basics of SELECT Statement - Oracle SQL
 
Unit 03 dbms
Unit 03 dbmsUnit 03 dbms
Unit 03 dbms
 
Lecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptxLecture-2 - Relational Model.pptx
Lecture-2 - Relational Model.pptx
 
Unit03 dbms
Unit03 dbmsUnit03 dbms
Unit03 dbms
 
Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++Revision notes for exam 2011 computer science with C++
Revision notes for exam 2011 computer science with C++
 

More from ssuser9dddf7

PPT-UEU-Bahasa-Pemrograman-Pertemuan-1.pptx
PPT-UEU-Bahasa-Pemrograman-Pertemuan-1.pptxPPT-UEU-Bahasa-Pemrograman-Pertemuan-1.pptx
PPT-UEU-Bahasa-Pemrograman-Pertemuan-1.pptxssuser9dddf7
 
Ppt03-MainComponent-edit-02.pptx
Ppt03-MainComponent-edit-02.pptxPpt03-MainComponent-edit-02.pptx
Ppt03-MainComponent-edit-02.pptxssuser9dddf7
 
2b. Representasi Pengetahuan.pptx
2b. Representasi Pengetahuan.pptx2b. Representasi Pengetahuan.pptx
2b. Representasi Pengetahuan.pptxssuser9dddf7
 
Peng Kep Indeks Kinerja-kom.ppt
Peng Kep Indeks Kinerja-kom.pptPeng Kep Indeks Kinerja-kom.ppt
Peng Kep Indeks Kinerja-kom.pptssuser9dddf7
 
Pengambilan Keputusan Dengan AHP.pptx
Pengambilan Keputusan Dengan AHP.pptxPengambilan Keputusan Dengan AHP.pptx
Pengambilan Keputusan Dengan AHP.pptxssuser9dddf7
 
Application Modernization with Microsoft Azure.pptx
Application Modernization with Microsoft Azure.pptxApplication Modernization with Microsoft Azure.pptx
Application Modernization with Microsoft Azure.pptxssuser9dddf7
 
04242015094818.pptx
04242015094818.pptx04242015094818.pptx
04242015094818.pptxssuser9dddf7
 
EO-TH-v2-End-Users.pptx
EO-TH-v2-End-Users.pptxEO-TH-v2-End-Users.pptx
EO-TH-v2-End-Users.pptxssuser9dddf7
 
cd react app with docker.pdf
cd react app with docker.pdfcd react app with docker.pdf
cd react app with docker.pdfssuser9dddf7
 

More from ssuser9dddf7 (11)

PPT-UEU-Bahasa-Pemrograman-Pertemuan-1.pptx
PPT-UEU-Bahasa-Pemrograman-Pertemuan-1.pptxPPT-UEU-Bahasa-Pemrograman-Pertemuan-1.pptx
PPT-UEU-Bahasa-Pemrograman-Pertemuan-1.pptx
 
Ppt03-MainComponent-edit-02.pptx
Ppt03-MainComponent-edit-02.pptxPpt03-MainComponent-edit-02.pptx
Ppt03-MainComponent-edit-02.pptx
 
semantic.ppt
semantic.pptsemantic.ppt
semantic.ppt
 
2b. Representasi Pengetahuan.pptx
2b. Representasi Pengetahuan.pptx2b. Representasi Pengetahuan.pptx
2b. Representasi Pengetahuan.pptx
 
Peng Kep Indeks Kinerja-kom.ppt
Peng Kep Indeks Kinerja-kom.pptPeng Kep Indeks Kinerja-kom.ppt
Peng Kep Indeks Kinerja-kom.ppt
 
Pengambilan Keputusan Dengan AHP.pptx
Pengambilan Keputusan Dengan AHP.pptxPengambilan Keputusan Dengan AHP.pptx
Pengambilan Keputusan Dengan AHP.pptx
 
Application Modernization with Microsoft Azure.pptx
Application Modernization with Microsoft Azure.pptxApplication Modernization with Microsoft Azure.pptx
Application Modernization with Microsoft Azure.pptx
 
04242015094818.pptx
04242015094818.pptx04242015094818.pptx
04242015094818.pptx
 
EO-TH-v2-End-Users.pptx
EO-TH-v2-End-Users.pptxEO-TH-v2-End-Users.pptx
EO-TH-v2-End-Users.pptx
 
cd react app with docker.pdf
cd react app with docker.pdfcd react app with docker.pdf
cd react app with docker.pdf
 
Azure-AD.pptx
Azure-AD.pptxAzure-AD.pptx
Azure-AD.pptx
 

Recently uploaded

Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...dajasot375
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一F La
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...Suhani Kapoor
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxEmmanuel Dauda
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Sapana Sha
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDRafezzaman
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAbdelrhman abooda
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPramod Kumar Srivastava
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptSonatrach
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样vhwb25kk
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...Pooja Nehwal
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxFurkanTasci3
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 

Recently uploaded (20)

Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
Indian Call Girls in Abu Dhabi O5286O24O8 Call Girls in Abu Dhabi By Independ...
 
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
办理(Vancouver毕业证书)加拿大温哥华岛大学毕业证成绩单原版一比一
 
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
VIP High Class Call Girls Jamshedpur Anushka 8250192130 Independent Escort Se...
 
Customer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptxCustomer Service Analytics - Make Sense of All Your Data.pptx
Customer Service Analytics - Make Sense of All Your Data.pptx
 
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
VIP Call Girls Service Charbagh { Lucknow Call Girls Service 9548273370 } Boo...
 
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
Saket, (-DELHI )+91-9654467111-(=)CHEAP Call Girls in Escorts Service Saket C...
 
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTDINTERNSHIP ON PURBASHA COMPOSITE TEX LTD
INTERNSHIP ON PURBASHA COMPOSITE TEX LTD
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptxAmazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
Amazon TQM (2) Amazon TQM (2)Amazon TQM (2).pptx
 
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptxPKS-TGC-1084-630 - Stage 1 Proposal.pptx
PKS-TGC-1084-630 - Stage 1 Proposal.pptx
 
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.pptdokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
dokumen.tips_chapter-4-transient-heat-conduction-mehmet-kanoglu.ppt
 
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
1:1定制(UQ毕业证)昆士兰大学毕业证成绩单修改留信学历认证原版一模一样
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...{Pooja:  9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
{Pooja: 9892124323 } Call Girl in Mumbai | Jas Kaur Rate 4500 Free Hotel Del...
 
Data Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptxData Science Jobs and Salaries Analysis.pptx
Data Science Jobs and Salaries Analysis.pptx
 
Call Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort ServiceCall Girls in Saket 99530🔝 56974 Escort Service
Call Girls in Saket 99530🔝 56974 Escort Service
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
(PARI) Call Girls Wanowrie ( 7001035870 ) HI-Fi Pune Escorts Service
 
E-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptxE-Commerce Order PredictionShraddha Kamble.pptx
E-Commerce Order PredictionShraddha Kamble.pptx
 

Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test

  • 1. Database Applications (15-415) SQL-Part I Lecture 8, January 30, 2018 Mohammad Hammoud
  • 2. Today…  Last Session:  Relational Calculus  Today’s Session:  Standard Query Language (SQL)- Part I  Announcements:  PS2 is due on Sunday, Feb 04 by midnight  Quiz I will be on Sunday, Feb 11  P1 is due on Thursday, Feb 15 by midnight  In this week’s recitation, we will practice on SQL
  • 3. Outline SQL Major Aspects Basic SQL Queries Set Operations Aggregate Functions & Group By, Having and Order By Clauses 
  • 4. SQL Major Aspects  A major strength of the relational model is that it supports simple and powerful querying of data  Structured Query Language (SQL) is the most widely used commercial relational database language  SQL has several aspects to it: 1. Data Manipulation Language (DML)  It allows users to pose queries and insert, delete and modify rows 2. Data Definition Language (DDL)  It allows users to create, delete, and modify tables and views
  • 5. SQL Major Aspects  SQL has several aspects to it: 3. Triggers and Advanced Integrity Constraints  It supports “triggers”, which are actions executed by the DBMS whenever changes to the database meet conditions specified in triggers 4. Embedded and Dynamic Language  Embedded SQL allows SQL code to be called from a host language (e.g., Java)  Dynamic SQL allows SQL queries to be constructed and executed at run-time
  • 6. SQL Major Aspects  SQL has several aspects to it: 3. Triggers and Advanced Integrity Constraints  It supports “triggers”, which are actions executed by the DBMS whenever changes to the database meet conditions specified in triggers 4. Embedded and Dynamic Language  Embedded SQL allows SQL code to be called from a host language (e.g., Java)  Dynamic SQL allows SQL queries to be constructed and executed at run-time
  • 7. SQL Major Aspects  SQL has several aspects to it: 5. Remote Database Access  It allows connecting client programs to remote database servers 6. Transaction Management  It allows users to explicitly control aspects of how a transaction is to be executed (later in the semester) 7. Security  It provides mechanisms to control users’ accesses to data objects (e.g., tables and views) And others…
  • 8. Outline SQL Major Aspects Basic SQL Queries Set Operations Aggregate Functions & Group By, Having and Order By Clauses 
  • 9. Basic SQL Queries  The basic form of an SQL query is as follows: select a1, a2, … an from r1, r2, … rm where P The Column-List The Relation-List Qualification (Optional)
  • 10. Equivalence to Relational Algebra  The basic form of an SQL query is as follows: select a1, a2, … an from r1, r2, … rm where P )) ... 2 1 ( ( ,... 2 , 1 rm r r P an a a      join
  • 11. Reminder: Our Mini-U DB STUDENT Ssn Name Address 123 smith main str 234 jones QF ave CLASS c-id c-name units 15-413 s.e. 2 15-412 o.s. 2 TAKES SSN c-id grade 123 15-413 A 234 15-413 B
  • 12. The WHERE Clause  Find the ssn(s) of everybody called “smith” select ssn from student where name=‘smith’ STUDENT Ssn Name Address 123 smith main str 234 jones QF ave
  • 13. The WHERE Clause  Find ssn(s) of all “smith”s on “main” select ssn from student where address=‘main’ and name = ‘smith’ STUDENT Ssn Name Address 123 smith main str 234 jones QF ave
  • 14. The WHERE Clause  Boolean operators (and, or, not)  Comparison operators (<, ≤, >, ≥, =, ≠)  And more…
  • 15. What About Strings?  Find student ssn(s) who live on “main” (st or str or street – i.e., “main st” or “main str” or “main street”) select ssn from student where address like ‘main%’ %: Variable-length do not care (i.e., stands for 0 or more arbitrary characters) _: Single-character do not care (i.e., stands for any 1 character)
  • 16. Another Example on Pattern Matching  Find the ages of sailors whose names begin and end with B and have at least 3 characters Sailors Sid Sname Rating age 22 Dustin 7 45.0 29 Brutus 1 33.0 select S.age from Sailors S where S.sname like ‘B_%B’
  • 17. The FROM Clause  Find the names of students taking 15-415 STUDENT Ssn Name Address 123 smith main str 234 jones QF ave CLASS c-id c-name units 15-413 s.e. 2 15-412 o.s. 2 TAKES SSN c-id grade 123 15-413 A 234 15-413 B 2-way Join!
  • 18. The FROM Clause  Find the names of students taking 15-415 select Name from STUDENT, TAKES where ???
  • 19. The FROM Clause  Find the names of students taking 15-415 select Name from STUDENT, TAKES where STUDENT.ssn = TAKES.ssn and TAKES.c-id = ‘15-415’
  • 20. Renaming: Tuple Variables  Find the names of students taking 15-415 select Name from STUDENT as S, TAKES as T where S.ssn = T.ssn and T.c-id = “15-415” Optional!
  • 21. Renaming: Self-Joins  Find Tom’s grandparent(s) PC p-id c-id Mary Tom Peter Mary John Tom PC p-id c-id Mary Tom Peter Mary John Tom select gp.p-id from PC as gp, PC where gp.c-id= PC.p-id and PC.c-id = ‘Tom’
  • 22. More on Self-Joins  Find names and increments for the ratings of persons who have sailed two different boats on the same day Sailors Sid Sname Rating age 22 Dustin 7 45.0 29 Brutus 1 33.0 Reserves Sid Bid Day 22 101 10/10/2013 22 102 10/10/2013
  • 23. More on Self-Joins  Find names and increments for the ratings of persons who have sailed two different boats on the same day Sailors Sid Sname Rating age 22 Dustin 7 45.0 29 Brutus 1 33.0 Reserves Sid Bid Day 22 101 10/10/2013 22 102 10/10/2013 select S.sname, S.rating+1 as rating from Sailors S, Reserves R1, Reserves R2 where S.sid = R1.sid and S.sid = R2.sid and R1.day = R2.day and R1.bid != R2.bid
  • 24. Renaming: Theta Joins  Find course names with more units than 15-415 select c1.c-name from class as c1, class as c2 where c1.units > c2.units and c2.c-id = ‘15-415’ CLASS c-id c-name units 15-413 s.e. 2 15-412 o.s. 2
  • 25. Outline SQL Major Aspects Basic SQL Queries Set Operations Aggregate Functions & Group By, Having and Order By Clauses 
  • 26. Set Operations  Find ssn(s) of students taking both 15-415 and 15-413 TAKES SSN c-id grade 123 15-413 A 234 15-413 B select ssn from takes where c-id=‘15-415’ and c-id=‘15-413’
  • 27. Set Operations  Find ssn(s) of students taking both 15-415 and 15-413 TAKES SSN c-id grade 123 15-413 A 234 15-413 B (select ssn from takes where c-id=“15-415” ) intersect (select ssn from takes where c-id=“15-413” ) Other operations: union , except
  • 28. Set Operations  Find ssn(s) of students taking 15-415 or 15-413 TAKES SSN c-id grade 123 15-413 A 234 15-413 B (select ssn from takes where c-id=“15-415” ) union (select ssn from takes where c-id=“15-413” )
  • 29. Set Operations  Find ssn(s) of students taking 15-415 but not 15-413 TAKES SSN c-id grade 123 15-413 A 234 15-413 B (select ssn from takes where c-id=“15-415” ) except (select ssn from takes where c-id=“15-413” )
  • 30. Another Example on Set Operations  Find the names of sailors who have reserved both a red and a green boat Sailors Sid Sname Rating age 22 Dustin 7 45.0 29 Brutus 1 33.0 Reserves Sid Bid Day 22 101 10/10/2013 22 102 10/11/2013 Boats Bid Bname Color 101 Interlake Red 102 Clipper Green
  • 31. Another Example on Set Operations  Find the names of sailors who have reserved both a red and a green boat (select S.sname from Sailors S, Reserves R, Boats B where S.sid = R.sid and R.bid = B.bid and B.color = ‘green’) intersect (select S2.sname from Sailors S2, Reserves R2, Boats B2 where S2.sid = R2.sid and R2.bid = B2.bid and B2.color = ‘red’) The query contains a “subtle bug” which arises because we are using sname to identify Sailors, and “sname” is not a key for Sailors! We can compute the names of such Sailors using a NESTED query (which we cover next lecture!)
  • 32. Outline SQL Major Aspects Basic SQL Queries Set Operations Aggregate Functions & Group By, Having and Order By Clauses 
  • 33. Aggregate Functions  Find average grade, across all students SSN c-id grade 123 15-413 4 234 15-413 3 select ?? from takes
  • 34. Aggregate Functions  Find average grade, across all students SSN c-id grade 123 15-413 4 234 15-413 3 select avg(grade) from takes Other functions: Count ([Distinct] A), Sum ([Distinct] A), Max (A), Min (A), assuming column A
  • 35. Aggregate Functions  Find total number of enrollments SSN c-id grade 123 15-413 4 234 15-413 3 select count(*) from takes
  • 36. Aggregate Functions  Find total number of students in 15-415 SSN c-id grade 123 15-413 4 234 15-413 3 select count(*) from takes where c-id=‘15-415’
  • 37. Aggregate Functions  Find the name and age of the oldest sailor select S.sname, max (S.age) from Sailors S This query is illegal in SQL- If the “select” clause uses an aggregate function, it must use ONLY aggregate function unless the query contains a “group by” clause! Sailors Sid Sname Rating age 22 Dustin 7 45.0 29 Brutus 1 33.0
  • 38. The GROUP BY and HAVING Clauses  Find the age of the youngest sailor for each rating level  In general, we do not know how many rating levels exist, and what the rating values for these levels are!  Suppose we know that rating values go from 1 to 10; we can write 10 queries that look like this (!): Sailors Sid Sname Rating age 22 Dustin 7 45.0 29 Brutus 1 33.0 SELECT MIN (S.age) FROM Sailors S WHERE S.rating = i For i = 1, 2, ... , 10:
  • 39. The GROUP BY and HAVING Clauses  Find the age of the youngest sailor for each rating level  Using the GROUP BY clause, we can write this query as follows: Sailors Sid Sname Rating age 22 Dustin 7 45.0 29 Brutus 1 33.0 select S.rating, min (S.age) from Sailors S group by S.rating “Every” column that appears in the Column-List “must” also appear in the Grouping-List The Grouping-List
  • 40. The GROUP BY and HAVING Clauses  Find age of the youngest sailor with age ≥ 18, for each rating level with at least 2 sailors Sailors Sid Sname Rating age 22 Dustin 7 45.0 29 Brutus 1 33.0 SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1
  • 41. The GROUP BY and HAVING Clauses  Find age of the youngest sailor with age ≥ 18, for each rating level with at least 2 sailors rating age 7 45.0 1 33.0 8 55.5 8 25.5 10 35.0 7 35.0 10 16.0 9 35.0 3 25.5 3 63.5 3 25.5 rating minage 3 25.5 7 35.0 8 25.5 rating age 1 33.0 3 25.5 3 63.5 3 25.5 7 45.0 7 35.0 8 55.5 8 25.5 9 35.0 10 35.0
  • 42. The GROUP BY and HAVING Clauses  Find age of the youngest sailor with age ≥ 18, for each rating level with at least 2 sailors, and with every sailor under 60 SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 GROUP BY S.rating HAVING COUNT (*) > 1 AND EVERY (S.age <=60)
  • 43. The GROUP BY and HAVING Clauses  Find age of the youngest sailor with age ≥ 18, for each rating level with at least 2 sailors, and with every sailor under 60 rating age 7 45.0 1 33.0 8 55.5 8 25.5 10 35.0 7 35.0 10 16.0 9 35.0 3 25.5 3 63.5 3 25.5 rating age 1 33.0 3 25.5 3 63.5 3 25.5 7 45.0 7 35.0 8 55.5 8 25.5 9 35.0 10 35.0 rating minage 7 35.0 8 25.5 What would be the result if we change EVERY to ANY in “HAVING COUNT (*) > 1 AND EVERY (S.age <=60)”?
  • 44. The GROUP BY and HAVING Clauses  Find age of the youngest sailor with age ≥ 18, for each rating level with at least 2 sailors between 18 and 60 SELECT S.rating, MIN (S.age) AS minage FROM Sailors S WHERE S.age >= 18 AND S.age <= 60 GROUP BY S.rating HAVING COUNT (*) > 1 Will this give the same result as the previous query which uses the EVERY clause? Will this give the same result as the previous query which uses the ANY clause?
  • 45. select * from student where ?? The ORDER BY Clause  Find student records, sorted in name order
  • 46. select * from student order by name asc The ORDER BY Clause  Find student records, sorted in name order asc is the default
  • 47. select * from student order by name, ssn desc The ORDER BY Clause  Find student records, sorted in name order; break ties by reverse ssn
  • 48. More Examples  Find the total number of students in each course SSN c-id grade 123 15-413 4 234 15-413 3 select count(*) from takes where ???
  • 49. More Examples  Find the total number of students in each course SSN c-id grade 123 15-413 4 234 15-413 3 select c-id, count(*) from takes group by c-id c-id count 15-413 2
  • 50. More Examples  Find total number of students in each course, and sort by count, in decreasing order SSN c-id grade 123 15-413 4 234 15-413 3 select c-id, count(*) as pop from takes group by c-id order by pop desc c-id pop 15-413 2
  • 51. Concluding Remarks  SQL was an important factor in the early acceptance of the relational model  It is more natural than earlier procedural query languages  SQL is relationally complete; in fact, significantly more expressive power than relational algebra  Even queries that can be expressed in relational algebra can often be expressed more naturally in SQL

Editor's Notes

  1. CMU - 15-415/615
  2. CMU - 15-415/615
  3. CMU - 15-415/615
  4. CMU - 15-415/615
  5. CMU - 15-415/615