chapter 4 of homework 2 databases

45
 Database Design, Application D evelopment, and  Administration, 5 th  Edition Copyright  © 2011 by Michael V. Mannino, All rights reserved. Chapter 4 Query Formulation with SQL

Upload: saurabh-kapadia

Post on 10-Feb-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 1/45

 Database Design, Application Development, and Administration, 5th Edition

Copyright  © 2011 by Michael V. Mannino, All rights reserved.

Chapter 4

Query Formulation with SQL

Page 2: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 2/45

Slide 2Chapter 4: Query Formulation with SQL

Outline

Background

Getting started

Joining tables Summarizing tables

Problem solving guidelines

 Advanced problems

Data manipulation statements

Page 3: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 3/45

Slide 3Chapter 4: Query Formulation with SQL

What is SQL?

Structured Query Language

Language for database definition,

manipulation, and control

International standard

Standalone and embedded usage

Intergalactic database speak

Page 4: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 4/45

Slide 4Chapter 4: Query Formulation with SQL

SQL Statements

Statement Chapter

CREATE TABLE 3, 14, 19

SELECT 4, 9, 10, 17, 18

INSERT, UPDATE 4, 10, 19DELETE 4, 9, 10

CREATE VIEW 10, 17

CREATE TRIGGER 11

GRANT, REVOKE 14COMMIT, ROLLBACK, SET TRANSACTION 15

CREATE TYPE 19

Page 5: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 5/45

Slide 5Chapter 4: Query Formulation with SQL

SQL Standardization

Relatively simple standard: SQL-86 and

revision (SQL-89)

Modestly complex standard: SQL-92

Complex standards: SQL:1999,

SQL:2003, and SQL:2008

Page 6: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 6/45

Slide 6Chapter 4: Query Formulation with SQL

SQL Conformance

No official conformance testing

Vendor claims about conformance

Reasonable conformance on Core SQL Large variance on conformance outside of

Core SQL

Difficult to write portable SQL code outsideof Core SQL

Page 7: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 7/45

Slide 7Chapter 4: Query Formulation with SQL

SELECT Statement Overview

SELECT <list of column expressions>

FROM <list of tables and join operations>

WHERE <list of logical expressions for rows>GROUP BY <list of grouping columns>

HAVING <list of logical expressions for groups>

ORDER BY <list of sorting specifications> 

Expression: combination of columns, constants,

operators, and functions

Page 8: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 8/45

Slide 8Chapter 4: Query Formulation with SQL

University Database

Page 9: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 9/45

Slide 9Chapter 4: Query Formulation with SQL

First SELECT Examples

Example 1SELECT * FROM Faculty

Example 2 (Access)SELECT *

FROM FacultyWHERE FacSSN = '543210987'

Example 3SELECT FacFirstName, FacLastName, FacSalary

FROM Faculty

Example 4SELECT FacFirstName, FacLastName, FacSalary

FROM Faculty

WHERE FacSalary > 65000 AND FacRank = 'PROF'

Page 10: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 10/45

Slide 10Chapter 4: Query Formulation with SQL

Using Expressions

Example 5 (Access)SELECT FacFirstName, FacLastName, FacCity,

FacSalary*1.1 AS IncreasedSalary,FacHireDate

FROM Faculty

WHERE year(FacHireDate) > 1998

Example 5 (Oracle)

SELECT FacFirstName, FacLastName, FacCity,FacSalary*1.1 AS IncreasedSalary,

FacHireDate

FROM Faculty

WHERE to_number(to_char(FacHireDate, 'YYYY')) > 1998

Page 11: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 11/45

Slide 11Chapter 4: Query Formulation with SQL

Inexact Matching

• Match against a pattern: LIKE operator

• Use meta characters to specify patterns

 –  Wildcard (* or %)

 –  Any single character (? or _)

Example 6 (Access)SELECT *

FROM Offering

WHERE CourseNo LIKE 'IS*'

Example 6 (Oracle)SELECT *

FROM Offering

WHERE CourseNo LIKE 'IS%'

Page 12: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 12/45

Slide 12Chapter 4: Query Formulation with SQL

Using Dates

• Dates are numbers

• Date constants and functions are not standard

Example 7 (Access)SELECT FacFirstName, FacLastName, FacHireDate

FROM Faculty

WHERE FacHireDate BETWEEN #1/1/2001# AND #12/31/2002#

Example 7 (Oracle)SELECT FacFirstName, FacLastName, FacHireDate

FROM Faculty

WHERE FacHireDate BETWEEN '1-Jan-2001'

AND '31-Dec-2002'

Page 13: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 13/45

Slide 13Chapter 4: Query Formulation with SQL

Other Single Table Examples

Example 8: Testing for null valuesSELECT OfferNo, CourseNo

FROM OfferingWHERE FacSSN IS NULL AND OffTerm = 'SUMMER'

AND OffYear = 2010

Example 9: Mixing AND and OR

SELECT OfferNo, CourseNo, FacSSNFROM Offering

WHERE (OffTerm = 'FALL' AND OffYear = 2009)

OR (OffTerm = 'WINTER' AND OffYear = 2010)

Page 14: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 14/45

Slide 14Chapter 4: Query Formulation with SQL

Join Operator

Most databases have many tables

Combine tables using the join operator

Specify matching condition

Can be any comparison but usually =

PK = FK most common join condition

Relationship diagram useful when combining

tables

Page 15: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 15/45

Slide 15Chapter 4: Query Formulation with SQL

Join Example

FacNo FacName 111-11-1111 joe

 222-22-2222 sue

 333-33-3333 sara

 OfferNo FacNo 1111 111-11-1111

 2222 222-22-2222

 3333 111-11-1111

 FacNo FacName OfferNo

 111-11-1111 joe 1111

 222-22-2222 sue 2222

 111-11-1111 joe 3333

Faculty

Offering

Natural Join of Offering and

Faculty

Page 16: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 16/45

Slide 16Chapter 4: Query Formulation with SQL

Cross Product Style

• List tables in the FROM clause

• List join conditions in the WHERE clause

Example 10 (Access)SELECT OfferNo, CourseNo, FacFirstName,

FacLastName

FROM Offering, FacultyWHERE OffTerm = 'FALL' AND OffYear = 2009

AND FacRank = 'ASST' AND CourseNo LIKE 'IS*'

AND Faculty.FacNo = Offering.FacNo

Page 17: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 17/45

Slide 17Chapter 4: Query Formulation with SQL

Join Operator Style

• Use INNER JOIN and ON keywords

• FROM clause contains join operations

Example 11 (Access)SELECT OfferNo, CourseNo, FacFirstName,

FacLastName

FROM Offering INNER JOIN FacultyON Faculty.FacNo = Offering.FacNo

WHERE OffTerm = 'FALL' AND OffYear = 2009

AND FacRank = 'ASST' AND CourseNo LIKE 'IS*'

Page 18: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 18/45

Slide 18Chapter 4: Query Formulation with SQL

Name Qualification

 Ambiguous column reference More than one table in the query contains a column

referenced in the query

 Ambiguity determined by the query not the database Use column name alone if query is not

ambiguous

Qualify with table name if query is ambiguous

Readability versus writability

Page 19: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 19/45

Slide 19Chapter 4: Query Formulation with SQL

Summarizing Tables

Row summaries important for decision-making

tasks

Row summary

Result contains statistical (aggregate) functions Conditions involve statistical functions

SQL keywords

 Aggregate functions in the output list

GROUP BY: summary columns

HAVING: summary conditions

Page 20: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 20/45

Slide 20Chapter 4: Query Formulation with SQL

GROUP BY Examples

Example 12: Grouping on a single columnSELECT FacRank, AVG(FacSalary) AS AvgSalary

FROM FacultyGROUP BY FacRank

Example 13: Row and group conditions

SELECT StdMajor, AVG(StdGPA) AS AvgGpaFROM Student

WHERE StdClass IN ('JR', 'SR')

GROUP BY StdMajor

HAVING AVG(StdGPA) > 3.1

Page 21: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 21/45

Slide 21Chapter 4: Query Formulation with SQL

SQL Summarization Rules

Columns in SELECT and GROUP BY

SELECT: non aggregate and aggregate

columns

GROUP BY: list all non aggregate columns

WHERE versus HAVING

Row conditions in WHERE

Group conditions in HAVING

Page 22: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 22/45

Slide 22Chapter 4: Query Formulation with SQL

Summarization and Joins

• Powerful combination

• List join conditions in the WHERE clause

Example 14: List the number of students enrolled in each 2008

offering.SELECT Offering.OfferNo,

COUNT(*) AS NumStudents

FROM Enrollment, Offering

WHERE Offering.OfferNo = Enrollment.OfferNo

AND OffYear = 2010

GROUP BY Offering.OfferNo

Page 23: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 23/45

Slide 23Chapter 4: Query Formulation with SQL

Conceptual Evaluation Process

FROM Tables:Cross Product and

Join Operations

GROUP

BY?

Restriction

on WHERE

Conditions

Sort on

GROUP BY

Columns

Compute Aggregates

and Reduce

Each Group

to 1 Row

ORDER

BY?

Sort

Columns inORDER BY

1

2

3

4

6

7

Yes

No

Yes

No

Finish

Restriction

on HAVING

Conditions

5

Project

Columns in

SELECT

Page 24: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 24/45

Slide 24Chapter 4: Query Formulation with SQL

Conceptual Evaluation

Lessons

Row operations before group operations

FROM and WHERE before GROUP BY and

HAVING

Check row operations first

Grouping occurs only one time

Use small sample tables

Page 25: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 25/45

Slide 25Chapter 4: Query Formulation with SQL

Conceptual Evaluation Problem

Example 15: List the number of offerings taught in 2010 by faculty

rank and department. Exclude combinations of faculty rank anddepartment with less than two offerings taught.SELECT FacRank, FacDept,

COUNT(*) AS NumOfferings

FROM Faculty, Offering

WHERE Offering.FacNo = Faculty.FacNoAND OffYear = 2010

GROUP BY FacRank, FacDept

HAVING COUNT(*) > 1

Page 26: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 26/45

Slide 26Chapter 4: Query Formulation with SQL

Query Formulation Process

Problem

Statemen

t

Database

Representation

Database Language

Statement

Page 27: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 27/45

Slide 27Chapter 4: Query Formulation with SQL

Critical Questions

What tables?

Columns in output

Conditions to test (including join conditions)

How to combine the tables?

Usually join PK to FK

More complex ways to combine

Individual rows or groups of rows?

 Aggregate functions in output

Conditions with aggregate functions

Page 28: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 28/45

Slide 28Chapter 4: Query Formulation with SQL

Efficiency Considerations

Little concern for efficiency

Intelligent SQL compilers

Correct and non redundant solution No extra tables

No unnecessary grouping

Use HAVING for group conditions only

Chapter 8 provides additional tips for avoiding

inefficient SELECT statements

Page 29: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 29/45

Slide 29Chapter 4: Query Formulation with SQL

 Advanced Problems

Joining multiple tables

Self joins

Grouping after joining multiple tables

Traditional set operators

Page 30: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 30/45

Slide 30Chapter 4: Query Formulation with SQL

Joining Three Tables

Example 16: List Leonard Vince’s teaching schedule in fall 2009. Foreach course, list the offering number, course number, number of units,days, location, and time.

SELECT OfferNo, Offering.CourseNo, OffDays,CrsUnits, OffLocation, OffTime

FROM Faculty, Course, Offering

WHERE Faculty.FacNo = Offering.FacNo

AND Offering.CourseNo = Course.CourseNo

AND OffYear = 2009 AND OffTerm = 'FALL'

AND FacFirstName = 'Leonard'

AND FacLastName = 'Vince'

Page 31: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 31/45

Slide 31Chapter 4: Query Formulation with SQL

Joining Four Tables

Example 17: List Bob Norbert’s course schedule in spring 2010. Foreach course, list the offering number, course number, days, location,time, and faculty name.

SELECT Offering.OfferNo, Offering.CourseNo,OffDays, OffLocation, OffTime,

FacFirstName, FacLastName

FROM Faculty, Offering, Enrollment, Student

WHERE Offering.OfferNo = Enrollment.OfferNo

AND Student.StdNo = Enrollment.StdNo

AND Faculty.FacNo = Offering.FacNo

AND OffYear = 2010 AND OffTerm = 'SPRING'

AND StdFirstName = 'BOB'

AND StdLastName = 'NORBERT'

Page 32: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 32/45

Slide 32Chapter 4: Query Formulation with SQL

Self-Join

Join a table to itself

Usually involve a self-referencing

relationship Useful to find relationships among rows of

the same table

Find subordinates within a preset number of

levels Find subordinates within any number of levels

requires embedded SQL

Page 33: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 33/45

Slide 33Chapter 4: Query Formulation with SQL

Self-Join Example

Example 18: List faculty members who have a highersalary than their supervisor. List the social securitynumber, name, and salary of the faculty and supervisor.

SELECT Subr.FacNo, Subr.FacLastName,

Subr.FacSalary, Supr.FacSSN,

Supr.FacLastName, Supr.FacSalary

FROM Faculty Subr, Faculty Supr

WHERE Subr.FacSupervisor = Supr.FacNoAND Subr.FacSalary > Supr.FacSalary

Page 34: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 34/45

Slide 34Chapter 4: Query Formulation with SQL

Multiple Joins Between Tables

Example 19: List the names of faculty members and the

course number for which the faculty member teaches the

same course number as his or her supervisor in 2010.

SELECT FacFirstName, FacLastName, O1.CourseNo

FROM Faculty, Offering O1, Offering O2

WHERE Faculty.FacNo = O1.FacSSN

AND Faculty.FacSupervisor = O2.FacNoAND O1.OffYear = 2010 AND O2.OffYear = 2010

AND O1.CourseNo = O2.CourseNo

Page 35: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 35/45

Slide 35Chapter 4: Query Formulation with SQL

Multiple Column Grouping

Example 20: List the course number, the offering number,

and the number of students enrolled. Only include courses

offered in spring 2010.

SELECT CourseNo, Enrollment.OfferNo,

Count(*) AS NumStudents

FROM Offering, Enrollment

WHERE Offering.OfferNo = Enrollment.OfferNoAND OffYear = 2010 AND OffTerm = 'SPRING'

GROUP BY Enrollment.OfferNo, CourseNo

Page 36: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 36/45

Slide 36Chapter 4: Query Formulation with SQL

Traditional Set Operators

A UNION B

A INTERSECT B

A MINUS B

Page 37: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 37/45

Slide 37Chapter 4: Query Formulation with SQL

Union Compatibility

Requirement for the traditional set

operators

Strong requirement

Same number of columns

Each corresponding column is compatible

Positional correspondence

 Apply to similar tables by removingcolumns first

Page 38: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 38/45

Slide 38Chapter 4: Query Formulation with SQL

SQL UNION Example

Example 21: Retrieve basic data about all university people

SELECT FacNo AS PerNo, FacFirstName AS FirstName,

FacLastName AS LastName, FacCity AS City,

FacState AS State

FROM Faculty

UNIONSELECT StdNo AS PerNo, StdFirstName AS FirstName,

StdLastName AS LastName, StdCity AS City,

StdState AS State

FROM Student

Page 39: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 39/45

Slide 39Chapter 4: Query Formulation with SQL

Oracle INTERSECT Example

Example 22: Show teaching assistants, faculty who arestudents. Only show the common columns in the result.

SELECT FacNo AS PerNo, FacFirstName AS

FirstName, FacLastName AS LastName,

FacCity AS City, FacState AS State

FROM Faculty

INTERSECT

SELECT StdNo AS PerNo, StdFirstName AS

FirstName, StdLastName AS LastName,StdCity AS City, StdState AS State

FROM Student

Page 40: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 40/45

Slide 40Chapter 4: Query Formulation with SQL

Oracle MINUS Example

Example 23: Show faculty who are not students (purefaculty). Only show the common columns in the result.

SELECT FacNo AS PerNo, FacFirstName AS

FirstName, FacLastName AS LastName,

FacCity AS City, FacState AS State

FROM Faculty

MINUS

SELECT StdNo AS PerNo, StdFirstName AS

FirstName, StdLastName AS LastName,StdCity AS City, StdState AS State

FROM Student

Page 41: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 41/45

Slide 41Chapter 4: Query Formulation with SQL

Data Manipulation Statements

INSERT: adds one or more rows

UPDATE: modifies one or more rows

DELETE: removes one or more rows Use SELECT statement to INSERT multiple

rows

UPDATE and DELETE can use a WHEREclause

Not as widely used as SELECT statement

Page 42: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 42/45

Slide 42Chapter 4: Query Formulation with SQL

INSERT Example

Example 24: Insert a row into the Student  table supplying

values for all columns.

INSERT INTO Student

(StdNo, StdFirstName, StdLastName,

StdCity, StdState, StdZip, StdClass,

StdMajor, StdGPA)

VALUES('999999999','JOE','STUDENT','SEATAC',

'WA','98042-1121','FR','IS', 0.0) 

Page 43: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 43/45

Slide 43Chapter 4: Query Formulation with SQL

UPDATE Example

Example 25: Change the major and class ofHomer Wells. 

UPDATE StudentSET StdMajor = 'ACCT',

StdClass = 'SO'

WHERE StdFirstName = 'HOMER'AND StdLastName = 'WELLS'

Page 44: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 44/45

Slide 44Chapter 4: Query Formulation with SQL

DELETE Example

Example 26: Delete all IS majors who are seniors.

DELETE FROM Student

WHERE StdMajor = 'IS'AND StdClass = 'SR'

Page 45: chapter 4 of homework 2 databases

7/22/2019 chapter 4 of homework 2 databases

http://slidepdf.com/reader/full/chapter-4-of-homework-2-databases 45/45

Summary

SQL is a broad language

SELECT statement is complex

Use problem solving guidelines

Lots of practice to master query

formulation and SQL