24
Intro Basic Query Structure Set Operation Agg. Func. Subqueries CS 582 Database Management Systems II Review of SQL Basics Huiping Cao Huiping Cao CS 582 Database Management Systems II

CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

  • Upload
    others

  • View
    3

  • Download
    1

Embed Size (px)

Citation preview

Page 1: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

CS 582 Database Management Systems IIReview of SQL Basics

Huiping Cao

Huiping Cao CS 582 Database Management Systems II

Page 2: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

SQL overview

Several parts

Data-definition language (DDL): insert, delete, modify schemasData-manipulation language (DML): insert, delete, modifytuplesIntegrityView definitionTransaction controlEmbedded SQL and Dynamic SQLAuthorization

Huiping Cao CS 582 Database Management Systems II

Page 3: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Database Schema

classroom (building,room number, capacity)

department (dept name, building, capacity)

course (course id, title, dept name, credits)

instructor (ID, name, dept name, salary)

section (course id, sec id, semester, year, building, room number, time slot id)

teaches (ID, course id, sec id, semester, year)

student (ID, name, dept name, tot credit)

takes (ID, course id, sec id, semester, year, grade)

advisor(s ID, i ID)

time slot (time slot id, day, start time, end time)

prereq (course id, prereq id)

Huiping Cao CS 582 Database Management Systems II

Page 4: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Basic Query Structure

A typical SQL query has the form:select A1, A2, · · · , An

from r1, r2, · · · , rmwhere P

Ai represents an attributeri represents a relationP is a predicate

The result of an SQL query is a relation.

Huiping Cao CS 582 Database Management Systems II

Page 5: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

The Select Clause

The select clause lists the attributes desired in the result of aquery

corresponds to the projection (∏

) operation of the relationalalgebra

Example: find the names of all instructors:select namefrom instructor

Note: SQL names are case insensitive (i.e., you may useupper- or lower-case letters

E.g., Name ≡ NAME ≡ nameSome people use upper case wherever we use bold font.

Huiping Cao CS 582 Database Management Systems II

Page 6: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

The Select Clause – distinct

SQL allows duplicates in relations as well as in query results.

To force the elimination of duplicates, insert the keyworddistinct after select.

Find the names of all departments with instructor, andremove duplicates

select distinct dept namefrom instructor

The keyword all specifies that duplicates not be removed.select all dept namefrom instructor

Huiping Cao CS 582 Database Management Systems II

Page 7: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

The Select Clause

An asterisk in the select clause denotes “all attributes”select *from instructor

The select clause can contain arithmetic expressions involvingthe operation, +, −, ∗, and /, and operating on constants orattributes of tuples.

The query:select ID, name, dept name, salary/12from instructor

would return a relation that is the same as the instructorrelation, except that the value of the attribute salary isdivided by 12.

Huiping Cao CS 582 Database Management Systems II

Page 8: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

The Where Clause

The where clause specifies conditions that the result mustsatisfy

Corresponds to the selection (σ) predicate of relational algebra.

Example: find all instructors in Comp. Sci. dept with salary>80000

select namefrom instructorwhere dept name = ’Comp. Sci.’ and salary>80000

Comparison results can be combined using the logicalconnectives and, or, and not.

Comparisons can be applied to results of arithmeticexpressions.

Huiping Cao CS 582 Database Management Systems II

Page 9: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

The from Clause

The from clause lists the relations involved in the query

Corresponds to the Cartesian product (×) operation of therelational algebra.

Example: find the Cartesian product instructor × teachesselect *from instructor , teaches

generates every possible instructor − teaches pair, with allattributes from both relations.

Cartesian product is not very useful directly, but usefulcombined with where-clause condition.

Huiping Cao CS 582 Database Management Systems II

Page 10: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Cartesian Product

©Silberschatz, Korth and Sudarshan3.16Database System Concepts - 6th Edition

Cartesian ProductCartesian Productinstructor teaches

Huiping Cao CS 582 Database Management Systems II

Page 11: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Joins - Examples

For all instructors who have taught courses, find their names and the course IDof the courses they taught.

select name, course idfrom instructor , teacheswhere instructor .ID = teaches.ID

Find the course ID, semester, year and title of each course offered by the Comp.Sci. department

select section, course id , semester , year , titlefrom section, coursewhere section.course id = course.course id and

dept name = ’Comp. Sci.’

More complicated joins: discussed later

Huiping Cao CS 582 Database Management Systems II

Page 12: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Natural Join

Natural join matches tuples with the same values for allcommon attributes, and retains only one copy of eachcommon column

select *from instructor natural join teaches;

©Silberschatz, Korth and Sudarshan3.19Database System Concepts - 6th Edition

Natural JoinNatural Join

Natural join matches tuples with the same values for all commonattributes, and retains only one copy of each common column

select *from instructor natural join teaches;

Huiping Cao CS 582 Database Management Systems II

Page 13: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Natural Join – Example

Danger in natural join: beware of unrelated attributes withsame name which get equated incorrectly

Example: List the names of instructors along with the thetitles of courses that they teach

Incorrect version (equates course.dept name withinstructor .dept name)

select name, title

from instructor natural join teaches natural join course;

Correct versionselect name, titlefrom instructor natural join teaches, course

where teaches.course id= course.course id ;

Huiping Cao CS 582 Database Management Systems II

Page 14: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Ordering the Display of Tuples

List in alphabetic order the names of all instructorsselect distinct namefrom instructororder by name

We may specify desc for descending order or asc for ascendingorder, for each attribute; ascending order is the default.

Example: order by name desc

Can sort on multiple attributesExample: order by dept name desc, name asc

Huiping Cao CS 582 Database Management Systems II

Page 15: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Duplicates

In relations with duplicates, SQL can define how many copiesof tuples appear in the result.

Multiset versions of some of the relational algebra operators –given multiset relations r1 and r2:

σθ(r1): If there are c1 copies of tuple t1 in r1, and t1 satisfiesselections σθ, then there are c1 copies of t1 in σθ(r1)∏

A(r1): For each copy of tuple t1 in r, there is a copy of tuple∏A(t1) in

∏A(r1), where

∏A(t1) denotes the projection of the

single tuple t1

r1 × r2: If there are c1 copies of tuple t1 in r1 and c2 copies oftuple t2 in r2, there are c1 × c2 copies of the tuple t1 · t2 inr1 × r2

Huiping Cao CS 582 Database Management Systems II

Page 16: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Duplicates (cont.)

Example: Suppose multiset relations r1(A,B) and r2(C ) areas follows:

r1 = {(1, a)(2, a)}, r2 = {(2), (3), (3)}Then

∏B(r1) would be {(a), (a)}, while

∏B(r1)× r2 would be

{(a, 2), (a, 2), (a, 3), (a, 3), (a, 3), (a, 3)}

SQL duplicate semantics:select A1,A2, · · · ,An

from r1, r2, · · · , rmwhere Pis equivalent to the multiset version of the expression:∏

A1,A2,··· ,An(σP(r1 × r2 × · · · × rm))

Huiping Cao CS 582 Database Management Systems II

Page 17: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Set Operations

Find courses that ran in Fall 2009 or in Spring 2010(select course id from section where semester = ‘Fall’ and year = 2009)

union

(select course id from section where semester = ‘Spring’ and year = 2010)

Find courses that ran in Fall 2009 and in Spring 2010(select course id from section where semester = ‘Fall’ and year = 2009)

intersect

(select course id from section where semester = ‘Spring’ and year = 2010)

Find courses that ran in Fall 2009 but not in Spring 2010(select course id from section where semester = ’Fall’ and year = 2009)except

(select course id from section where semester = ’Spring’ and year = 2010)

Huiping Cao CS 582 Database Management Systems II

Page 18: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Set Operations (2)

Set operations union, intersect, and except

Each of the above operations automatically eliminatesduplicates

To retain all duplicates use the corresponding multisetversions union all, intersect all and except all.

Suppose a tuple occurs m times in r and n times in s, then, itoccurs:

m + n times in r union all smin(m, n) times in r intersect all smax(0,m − n) times in r except all s

Huiping Cao CS 582 Database Management Systems II

Page 19: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Aggregate Functions

These functions operate on the multiset of values of a columnof a relation, and return a value

avg: average valuemin: minimum valuemax: maximum valuesum: sum of valuescount: number of values

Huiping Cao CS 582 Database Management Systems II

Page 20: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Aggregate Functions – Example

Find the average salary of instructors in the Computer Sciencedepartment

select avg (salary)from instructorwhere dept name= ’Comp. Sci.’;

Find the total number of instructors who teach a course in theSpring 2010 semester

select count (distinct ID)from teacheswhere semester = ’Spring’ and year = 2010;

Find the number of tuples in the course relationselect count (*) from course;

Huiping Cao CS 582 Database Management Systems II

Page 21: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Aggregate Functions - Group By

Find the average salary of instructors in each departmentselect dept name, avg (salary)from instructorgroup by dept name;

©Silberschatz, Korth and Sudarshan3.33Database System Concepts - 6th Edition

Aggregate Functions Aggregate Functions –– Group By Group By

Find the average salary of instructors in each department select dept_name, avg (salary)

from instructorgroup by dept_name;

avg_salary

Huiping Cao CS 582 Database Management Systems II

Page 22: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Aggregation Functions – Group By (2)

Attributes in select clause outside of aggregate functions mustappear in group by list

/* erroneous query */select dept name, ID, avg(salary)from instructorgroup by dept name;

Huiping Cao CS 582 Database Management Systems II

Page 23: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Aggregate Functions - Having Clause

Find the names and average salaries of all departments whoseaverage salary is greater than 42000

select dept name, avg(salary)from instructorgroup by dept namehaving avg(salary)>42000;

Note: predicates in the having clause are applied after theformation of groups whereas predicates in the where clauseare applied before forming groups

Huiping Cao CS 582 Database Management Systems II

Page 24: CS 582 Database Management Systems II · Intro Basic Query Structure Set Operation Agg. Func. Subqueries SQL overview Several parts Data-de nition language (DDL): insert, delete,

Intro Basic Query Structure Set Operation Agg. Func. Subqueries

Nested Subqueries

SQL provides a mechanism for the nesting of subqueries.

A subquery is a select-from-where expression that is nestedwithin another query.

A common use of subqueries is to perform tests for setmembership, set comparisons, and set cardinality.

Huiping Cao CS 582 Database Management Systems II