Upload
randell-ward
View
226
Download
0
Embed Size (px)
Citation preview
1
IT420: Database Management and Organization
SQL part 37 February 2006
Adina Crăiniceanuwww.cs.usna.edu/~adina
2
Announcements
Exam next Tuesday 2 hours Closed book/closed notes No computers Covers all material
Labs returned on Friday
3
Previously
SQL DDL: Data Definition Language CREATE, DROP, ALTER
DML: Data Manipulation Language INSERT DELETE UPDATE SELECT
4
Today
More about SELECT
5
The SQL SELECT Statement
Basic SQL Query: SELECT [DISTINCT] column_name(s)
FROM table_name(s)
[WHERE conditions]
[ORDER BY some_column_names [ASC/DESC] ]
*Terms between [ ] are optional
6
WHERE Clause Options
AND, OR IN, NOT IN, BETWEEN =, >, <, <>, >=
SELECT SNbFROM Students S, Enrolled EWHERE S.SNb = E.Nb AND
E.Cid NOT IN (‘ComSci’, ‘Math’)
7
Calculations in SQL
Simple arithmetic Five SQL aggregate operators:
COUNT SUM AVG MIN MAX
8
Find the age of the youngest student Students(Alpha, LName, FName, Class,
Age)
SELECT MIN(Age)
FROM Students
9
Aggregate Operators
So far, aggregate operations applied to all (qualifying) rows
We want to apply them to each of several groups of rows
Students(Alpha, LName, SName, Class, Age) Find the age of the youngest student for each
class
10
Example
Students(Alpha, LName, FName, Class, Age) Find the age of the youngest student for each
class
If class values go from 1 to 4 we can write 4 queries that look like this:
SELECT MIN (S.Age)
FROM Students S
WHERE S.Class = i
11
GROUP-BY Clause
SELECT grouping_columns(s), aggregates
FROM table_name(s)
[WHERE conditions]
GROUP BY grouping_columns
SELECT Class, MIN(Age)
FROM Students
GROUP BY Class
12
Conceptual Evaluation
SQL query semantics: Compute the cross-product of table_names Discard resulting rows if they fail conditions Delete columns that are not specified in SELECT Remaining rows are partitioned into groups by the
value of the columns in grouping-columns One answer row is generated per group
Note: Does not imply query will actually be evaluated this way!
13
GROUP BY Exercise
Students(Alpha, LName, FName, Class, Age)
For each last name, find the number of students with same last name
14
HAVING Clause
SELECT [grouping_columns(s), aggregates
FROM table_name(s)
[WHERE conditions]
GROUP BY grouping_columns
HAVING group_conditions
HAVING restricts the groups presented in the result
15
Example
SELECT Class, MIN(Age)
FROM Students
WHERE MajDeptName = ‘ComSci’
GROUP BY Class
HAVING Class > 2
What does the query compute?
16
Another GROUP BY Example
SKU_DATA(SKU, SKU_description, Buyer, Department)
SELECT Department, COUNT(*) AS
Dept_SKU_Count
FROM SKU_DATA
WHERE SKU <> 302000
GROUP BY Department
HAVING COUNT (*) > 1
ORDER BY Dept_SKU_Count;
17
Select students with age higher than average SELECT *
FROM Students
WHERE Age > AVG(Age)
Illegal!
18
Subqueries
SELECT *
FROM Students
WHERE Age > (SELECT AVG(Age)
FROM Students)
Second select is a subquery (or nested query) You can have subqueries in FROM or HAVING
clause also
19
Subqueries Exercise
Students(Alpha, LName, FName, Class, Age) Enroll(Alpha, CourseID, Semester, Grade)
1. Find alpha for students enrolled in both ‘IT420’ and ‘IT334’
2. Find name of students enrolled in both ‘IT420’ and ‘IT334’
20
Class Exercise
Students(Alpha, LName, FName, Class, Age)
Enroll(Alpha, CourseID, Semester, Grade)
Find the name of students enrolled in ‘IT420’ Usual way Use subqueries
21
SELECT FName, LName
FROM Students S
WHERE S.Alpha IN
(SELECT Alpha
FROM Enroll E
WHERE E.CourseID = ‘IT420’)
22
Correlated Subqueries
SELECT FName, LName
FROM Students S
WHERE EXISTS
(SELECT *
FROM Enroll E
WHERE E.CourseID = ‘IT420’
AND E.Alpha = S.Alpha)
23
Subqueries versus Joins
Subqueries and joins both process multiple tables.
Subquery can only be used to retrieve data from the top table.
Join can be used to obtain data from any number of tables
Correlated subquery can do work that is not possible with joins.
24
Class Exercise
What does this query compute: SELECT FName, LName
FROM Students S, Enroll E1, Enroll E2WHERE S.Alpha = E1.Alpha
AND S.Alpha = E2.AlphaAND E1.CourseID = ‘IT420’AND E2.CourseID = ‘IT344’
25
JOIN ON Syntax Students(Alpha, LName, FName, Class, Age) Courses(CourseID, Description, Textbook) Enroll(Alpha, CourseID, Semester, Grade)
Find the names of students enrolled in ‘IT420’
SELECT LName, FNameFROM Students S JOIN Enroll C
ON S.Alpha = C.AlphaWHERE CourseID = ‘IT420’
Find the names of students enrolled in ‘Database Management’
26
Multiple JOIN ON
Find the names of students enrolled in ‘Database Management’
SELECT LName, FNameFROMEnroll E JOIN Courses C
ON E.CourseID = C.CourseIDJOIN Students ON E.Alpha = S.Alpha
WHERE C.Description = ‘Database Management’
27
Outer Joins
Find all students and courses in which they are enrolled
SELECT S.Alpha, S.LName, S.FName, E.CourseID
FROM Students S LEFT JOIN Enrolled E
ON S.Alpha = E.Alpha
28
Class Exercise
Students(Alpha, LName, FName, Class, Age)
Courses(CourseID, Description, Textbook) Enroll(Alpha, CourseID, Semester, Grade)
Find the age of youngest student older than 18, for each course with at least one such student enrolled in it