chapter 5-1 cse 4701 chapter 5 6e & 8 5e: complex sql prof. steven a. demurjian, sr. computer...
Post on 28-Dec-2015
234 Views
Preview:
TRANSCRIPT
Chapter 5-1
CSE 4701
Chapter 5 6e & 8 5e: Complex SQL
Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department
The University of Connecticut191 Auditorium Road, Box U-155
Storrs, CT 06269-3155steve@engr.uconn.edu
http://www.engr.uconn.edu/~steve(860) 486 - 4818
About one third of these slides are being used with the permission of Dr. Ling Lui, Associate Professor, College of Computing, Georgia Tech.
About one-half of these slides have been adapted from the AWL web site for the textbook.
Chapter 5-2
CSE 4701
Variety of Complex SQL Queries
Nested Queries Grouping and Aggregation Order by and Having Views Sets Tuple Variable Other Complex Queries and Operations
Chapter 5-3
CSE 4701
Recall Earlier Query 1
Query 1: Retrieve Name and Address of all Employees who work for the 'Research' DepartmentSELECT FNAME, MINIT, LNAME, ADDRESS, DNAMEFROM EMPLOYEE, DEPARTMENTWHERE DNAME='Research' AND DNUMBER=DNO
What Action is Being Performed?
Chapter 5-4
CSE 4701
Nested Queries
SQL SELECT Nested Query is Specified within WHERE-clause of another Query (the Outer Query)
Query 1A: Retrieve the Name and Address of all Employees who Work for the 'Research' DepartmentSELECT FNAME, LNAME, ADDRESSFROM EMPLOYEEWHERE DNO IN
(SELECT DNUMBERFROM DEPARTMENTWHERE DNAME='Research' )
Note: This Reformulates Earlier Query 1 (not in book)
Chapter 5-5
CSE 4701
How Does Nested Query Work?
The Nested Query Selects Number of 'Research' Dept. The Outer Query Selects an EMPLOYEE Tuple If Its
DNO Value Is in the Result of Either Nested Query IN represents Set Inclusion of Result Set We Can Have Several Levels of Nested Queries SELECT FNAME, LNAME, ADDRESS
FROM EMPLOYEEWHERE DNO IN
(SELECT DNUMBERFROM DEPARTMENTWHERE Dname=’Research' )
Chapter 5-6
CSE 4701
Correlated Nested Queries
When WHERE-clause of a Nested Query References an Attribute of a Relation Declared in the Outer Query
Query 16: Retrieve the Name of each Employee who has a Dependent with the Same First Name as the EmployeeSELECT E.FNAME, E.LNAMEFROM EMPLOYEE AS EWHERE E.SSN IN (SELECT ESSN FROM DEPENDENT WHERE ESSN=E.SSN AND E.FNAME=DEPENDENT_NAME)
Note: This Differs Slightly from 16 in book.
Chapter 5-7
CSE 4701
Query Equivalence
Query 16: SELECT E.FNAME, E.LNAMEFROM EMPLOYEE AS EWHERE E.SSN IN (SELECT ESSN FROM DEPENDENT WHERE ESSN=E.SSN AND E.FNAME=DEPENDENT_NAME)
Query 16A: SELECT E.FNAME, E.LNAMEFROM EMPLOYEE E, DEPENDENT DWHERE E.SSN=D.SSN AND E.FNAME=D.DEPENDENT_NAME
Chapter 5-8
CSE 4701
EXISTS Nested Queries
EXISTS checks Whether the Result of a Correlated Nested Query is Empty (contains no tuples) or not
Query 16B: Retrieve the Name of each Employee who has a Dependent with the Same First Name as the EmployeeSELECT FNAME, LNAMEFROM EMPLOYEE WHERE EXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN AND FNAME=DEPENDENT_NAME)
There is a analogous NOT EXISTS
Chapter 5-9
CSE 4701
NULLS in SQL Queries
SQL Allows Queries that Check if a value is NULL (Missing or Undefined or not Applicable)
SQL uses IS or IS NOT to compare NULLs since it Considers each NULL value Distinct from other NULL Values, so Equality Comparison is not Appropriate
Query 18: Retrieve the names of all employees who do not have supervisors.SELECT FNAME, LNAMEFROM EMPLOYEE WHERE SUPERSSN IS NULL
Why Would Such a Capability be Useful? Downloading/Crossloading a Database Promoting a Attribute to PK/FK
Chapter 5-10
CSE 4701
Aggregate Functions in SQL Queries
Query 19: Find Maximum Salary, Minimum Salary, and Average Salary among all EmployeesSELECT MAX(SALARY), MIN(SALARY), AVG(SALARY)FROM EMPLOYEE
Query 20: Find maximum and Minimum Salaries among 'Research' Department EmployeesSELECT MAX(SALARY), MIN(SALARY) FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO
What Does Query 22 Do? SELECT COUNT(*)FROM EMPLOYEE, DEPARTMENTWHERE DNAME='Research' AND DNUMBER=DNO
Chapter 5-11
CSE 4701
Grouping in SQL Queries
In Many Cases, We Want to Apply the Aggregate Functions to Subgroups of Tuples in a Relation
Each Subgroup of Tuples is Set of Tuples that Have the Same Value for the Grouping Attribute(s)
Function is Applied to Each Subgroup Independently Query 24: For Each Department, Retrieve the DNO,
Number of Employees, and Their Average SalarySELECT DNO, COUNT (*), AVG (SALARY)FROM EMPLOYEEGROUP BY DNO
Chapter 5-12
CSE 4701
Grouping in SQL Queries
Query 24: For Each Department, Retrieve the DNO, Number of Employees, and Their Average SalarySELECT DNO, COUNT (*), AVG (SALARY)FROM EMPLOYEEGROUP BY DNO
EMPLOYEE tuples are Divided into Groups; each group has the Same Value for Grouping Attribute DNO
COUNT and AVG functions are applied to each Group of Tuples Aeparately
SELECT-clause Includes only the Grouping Attribute and the Functions to be Applied on each Tuple Group
Chapter 5-13
CSE 4701
Results of Query 24:
SELECT DNO, COUNT (*), AVG (SALARY)FROM EMPLOYEEGROUP BY DNO
Chapter 5-14
CSE 4701
Joins and Grouping in SQL Queries
A Join Condition can be used in Conjunction with Grouping
Query 25: For each Project, Retrieve its Number, Name, and Number of Employees working on ProjectSELECT PNUMBER, PNAME, COUNT (*)FROM PROJECT, WORKS_ONWHERE PNUMBER=PNOGROUP BY PNUMBER, PNAME
In this case, the Grouping and Functions are Applied after the Joining of the two Relations
Chapter 5-15
CSE 4701
The HAVING Clause in SQL Queries
In Some Cases, we want to retrieve values of Functions for only those Groups that Satisfy Certain Condition(s)
The HAVING-clause is used for Specifying a Selection Condition on Groups (rather than Individual Tuples)
Query 26: For each Project on which more than two employees work, Retrieve its Number, Name, and Number of Employees working on Project projectSELECT PNUMBER, PNAME, COUNT (*)FROM PROJECT, WORKS_ONWHERE PNUMBER=PNOGROUP BY PNUMBER, PNAMEHAVING COUNT (*) > 2
Chapter 5-16
CSE 4701
Results of Query 26:
After Applying the WHERE/GROUP BY Clauses
Tw
o G
roup
s N
ot S
elec
ted
Bas
ed
on H
avin
g C
onst
rain
t
Chapter 5-17
CSE 4701
Results of Query 26:
After Applying the HAVING Clause Condition
3333
Chapter 5-18
CSE 4701
Substring Comparison in SQL Queries
In Regard to Strings, Most DBMSs Support SQL Queries for Exact, Near, and Starts with Matching
LIKE is Used to Compare Partial Strings '%' (or '*') Replaces an Arbitrary # of characters
'_' replaces a single arbitrary character Query 12: Retrieve all Employees whose Address is in
Houston, Texas. SELECT FNAME, LNAMEFROM EMPLOYEEWHERE ADDRESS LIKE '%Houston,TX% '
Houston, TX can be anywhere within the ADDRESS VAR CHAR String
Chapter 5-19
CSE 4701
Substring Comparison in SQL Queries
The LIKE Operator Allows us to get Around the Fact that each Value is Considered Atomic and Indivisible
SQL: Character String Attribute values are not Atomic Query 12A: Retrieve all employees who were born
during the 1950s. SELECT FNAME, LNAMEFROM EMPLOYEEWHERE BDATE LIKE ' __5_______'
There are two “_” before 5 and seven “_” after 5
Chapter 5-20
CSE 4701
Arithmetic Operations in SQL Queries
Standard Arithmetic Operators '+', '-'. '*', and '/' can be Applied to Numeric Values in an SQL Query Result
Query 13: Show the Effect of Giving all Employees who work on the 'ProductX' project a 10% raise.SELECT FNAME, LNAME, 1.1*SALARYFROM EMPLOYEE, WORKS_ON, PROJECTWHERE SSN=ESSN AND PNO=PNUMBER
AND PNAME='ProductX'
Chapter 5-21
CSE 4701
ORDER BY Clause in SQL Queries
ORDER BY used to Sort the Tuples in a Query Result based on the Values of one or More Attribute(s)
Query 15: Retrieve a list of Employees and the Projects each works in, ordered by Dept., and within each Dept., alphabetically by Employee last nameSELECT DNAME, LNAME, FNAME, PNAMEFROM DEPARTMENT, EMPLOYEE,
WORKS_ON, PROJECTWHERE DNUMBER=DNO AND SSN=ESSN
AND PNO=PNUMBERORDER BY DNAME, LNAME
Default is Ascending - Can be ASC/DESC as we’ll see in a Later Example
Chapter 5-22
CSE 4701
SQL Support for Views
Views are Part of the SQL DDL Abstracting from Conceptual to External Schema
View Hides the Details One or More Tables in Conceptual Schema May be
Combined (in Part) to Form a View Don’t Include FKs and Other Internal Attributes Typically, View is Formed by Join of Two or More
Relations Utilizing FKs, PKs, etc. As a Result - View is Independent
Once Formed - View Static/Unchangeable to Insulate User Applications from Conceptual Schema
Similar in Concept/Intent to “Public Interface”
Chapter 5-23
CSE 4701
Features of Views View Represents a Restricted Portion (Rows,
Columns) of a Relation - External Schema in SQL View is Virtual Table View (Not Stored) and Must
be Re-evaluated Every Time - Dynamic Like Relation, a View Can Be Deleted at Any Time Attributes Can Be Renamed in View
Reasons for Views Security Increasing Application-Data Independence
CREATE VIEW PQ(P#, SUMQTY) AS SELECT P#, SUM(QTY) FROM SP GROUP BY P#;
SQL View Definition
Chapter 5-24
CSE 4701
First View: Attribute Names are InheritedCREATE VIEW WORKS_ON1 ASSELECT FNAME, LNAME, PNAME, HOURSFROM EMPLOYEE, PROJECT, WORKS_ONWHERE SSN=ESSN AND PNO=PNUMBER ;
Second View: View attribute names are Aliased via a one-to-one Correspondence with the SELECT-clauseCREATE VIEW DEPT_INFO(DEPT_NAME, NO_OF_EMPS, TOTAL_SAL) AS SELECT DNAME, COUNT (*), SUM (SALARY) FROM DEPARTMENT, EMPLOYEE WHERE DNUMBER=DNO GROUP BY DNAME ;
View Definition in Ongoing Example
Chapter 5-25
CSE 4701
SQL View for Licensing Inquiries
CREATE OR REPLACE VIEW Individual_Demographic_View ASSELECT Ind_Last_Name, Ind_First_Name, Ind_Middle_Name,
Ind_Title, Ind_Resident, Ind_Status, Ind_Ce, Ind_Nipr,Lic_License_Number, Lic_Tin,Lic_Class, Lic_Status, Lic_License_Date, Lic_Cancel_Date, Lic_Suspend_Date, Lic_Reinstatement_Date, Lic_Amendment_Date, Add_Address_Type, Add_Lic_Tin, Add_Address1, Add_Address2, Add_Address3, Add_Address4, Add_City, Add_State, Add_Postal_Code, Add_Country, Add_Phone1, Add_Phone2, Add_Fax, Add_Email_Address, Add_Url
FROM Individual, License, Addresses WHERE ((Ind_SSN(+)=Lic_TIN) AND (Lic_TIN(+)=Add_Lic_TIN));
Ind: Individual Table Lic: License Table Add: Addresses Table
Chapter 5-26
CSE 4701
Queries on Views
Retrieve the Last Name and First Name of All Employees Who Work on 'ProjectX'.SELECT PNAME, FNAME, LNAMEFROM WORKS_ON1WHERE PNAME='ProjectX' ;
Without the View WORKS_ON1, this Query Specification Would Require Two Join Conditions
A View Can Be Defined to Simplify Frequently Occurring Queries
DBMS Keeps the View Up-to-date if the Base Tables on Which the View is Defined are Modified
Hence, the View is Realized at the Time we Specify a Query on the View
Chapter 5-27
CSE 4701
What is View Update Problem? Retrieval over View Mirrors a Retrieval over Relation However, Update over View may cause Problems! In general, a View Update may Introduce Ambiguity
when there is more than one way to Update Underlying Relations
Consider the view PQ Created Below:
Try to Change the Total Quantity SUMQTY of P1 in PQ from “30” to “40”
Why Does a view Update Problem occur?
CREATE VIEW PQ(P#, SUMQTY) AS SELECT P#, SUM(QTY) FROM SP GROUP BY P#;
Chapter 5-28
CSE 4701
Special Join Syntax in SQL2
SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAMEFROM EMPLOYEE E SWHERE E.SUPERSSN=S.SSN
vs.SELECT E.FNAME, E.LNAME, S.FNAME, S.LNAMEFROM (EMPLOYEE E LEFT OUTER JOIN
EMPLOYEE S ON E.SUPERSSN=S.SSN)SELECT FNAME, LNAME, ADDRESSFROM EMPLOYEE, DEPARTMENTWHERE DNAME='Research' AND DNUMBER=DNO
vs.SELECT FNAME, LNAME, ADDRESSFROM (EMPLOYEE JOIN DEPARTMENT
ON DNUMBER=DNO)WHERE DNAME='Research'
Chapter 5-29
CSE 4701
The Book Example - DDL - Create Tables
Create a Table in the MY_BOOK_DB Schema: What do ISBNNUMBER and PUBLISHERID
Represent?
CREATE TABLE BOOK_CATALOG(ISBN ISBNNUMBER NOT NULL, TITLE VARCHAR(25), AUTHORS VARCH(100), ...
PUBLISHER PUBLISHERID, PRIMARY KEY (ISBN) FOREIGN KEY (PUBLISHER)
REFERENCES PUBLISHING_HOUSE(NAME);
Chapter 5-30
CSE 4701
DDL - Create Tables (continued)
CREATE TABLE PUBLISHING_HOUSE(PUB_ID PUBLISHERID NOT NULL,
PUB_NAME VARCHAR(50) LOC CITYNAME
CONTACT ADDRESS,UNIQUE(PUB_NAME, LOC);
CREATE TABLE ORDER(ISBN ISBNNUMBER NOT NULL, PUBLISHER PUBLISHERID
DATE DATE NOT NULL,PRIMARY KEY(ISBN, PUBLISHER),FOREIGN KEY ISBN
REFERENCES BOOK_CATALOG(ISBN), FOREIGN KEY PUBLISHER REFERENCES PUBLISHING_HOUSE(PUB_ID));
Chapter 5-31
CSE 4701
DDL - Change Table Structure
Add a Column to a Table: ALTER TABLE BOOK
ADD PRICE DECIMAL(7,2),
ADD MEMBER_DISCOUNT, DEFAULT 5; No DEFAULT Implies NULL Values for all Tuples
Drop a Column from a tableALTER TABLE BOOK DROP PRICE RESTRICT (or CASCADE);
Restrict: Drop Operation Fails if Column is Referenced
Cascade: Drop Operation Removes Referencing View and Constraint Definitions
Chapter 5-32
CSE 4701
The Book Example - DML Operations
BOOK(ISBN, TITLE,AUTHORS,PRICE,PUBLISHER,YEAR) ORDER(ISBN, CUST_NAME, LOC, DATE,WEEKDAY) Find the books Written by Maier SELECT TITLE, ISBN FROM BOOK; WHERE AUTHOR LIKE ‘%MAIER’ ; Find ISBN of the Books Whose Price is at Least 5%
less than the Average Price of the Books by Maier SELECT ISBN, PRICE FROM BOOK WHERE PRICE*(1-0.05) < SELECT AVG(PRICE) FROM BOOK WHERE AUTHORS LIKE “%Maier”;
Chapter 5-33
CSE 4701
Other SQL Search: String Matching Wildcard:
%: Matches any Substring _: Matches any Character
SELECT * FROM BOOK WHERE PUBLISHER LIKE “%IEEE%”;
“%can%” Matches American, Canada, Scandinavian, ... “Ca%” Matches Canada, Canadian, ... “ %” Matches any string with at least two characters
Chapter 5-34
CSE 4701
Other SQL Queries Find ISBN and Price of Books Published by ACM SELECT ISBN, PRICE FROM BOOK WHERE PUBLISHER=“ACM”; Find ISBN and price for all books ordered from Atlanta
with a price over $50 SELECT ISBN, PRICE FROM BOOK, ORDER WHERE BOOK.ISBN = ORDER.ISBN AND ORDER.LOC=‘ATL’ AND PRICE > 50.00; Note the Distinguishing Between Attributes with Same
Name in Different Tables (TableName.AttributeName)
Chapter 5-35
CSE 4701
Using Tuple Variables Tuple Variables Simplify Query Since Don’t Need to
Repeat the Entire Table Name Find ISBN and Price for all Books ordered from
Atlanta with a price over $50 SELECT B.ISBN, B.PRICE FROM BOOK B, ORDER O WHERE B.ISBN = O.ISBN AND O.LOC=‘ATL’ AND B.PRICE > 50.00; Also Useful if Relation is Used “twice” in a Query: SELECT B1.ISBN, B1.TITLE, B1.AUTHORS FROM BOOK B1, BOOK B2 WHERE B1.PRICE > B2.PRICE AND B2.ISBN = “111001100”;
Chapter 5-36
CSE 4701
Ordering Results
Order by Clause Sorts the rows in a Query Result in Ascending (asc) or Descending (desc) Order
Find all books Published by ACM in the Ascending order of Price and Descending order of year
SELECT *
FROM BOOK
WHERE PUBLISHER LIKE “ACM%”
ORDER BY PRICE ASC, YEAR DESC; Questions:
What Does “*” Indicate? What Does ACM% Retrieve?
Chapter 5-37
CSE 4701
Set Operations Find books written by Mary or Lisa SELECT * FROM BOOK WHERE AUTHORS LIKE “LISA%” UNION SELECT * FROM BOOK WHERE AUTHORS LIKE “MARY%”;
UNION, INTERSECT, EXCEPT UNION ALL, INTERSECT ALL, and EXCEPT
ALL Preserve Duplicates
SELECT *FROM BOOKWHERE AUTHORS LIKE “LISA%” OR AUTHORS LIKE “MARY%”;
Chapter 5-38
CSE 4701
Built-in Aggregate Functions Count (COUNT), Sum (SUM), Average (AVG),
Minimum (MIN), Maximum (MAX) Count Books Ordered on 2/16 SELECT COUNT( *) FROM ORDER WHERE ORDER.DATE = “2/16/2000”; Find the Average Price of Books by each Publisher SELECT PUBLISHER, AVG(PRICE) FROM BOOK GROUP BY PUBLISHER;
Chapter 5-39
CSE 4701
Built-in Aggregate Functions
Find the Average Book Price of all Publishers that have Published more than 1000 Books
SELECT PUBLISHER, AVG(PRICE) FROM BOOK GROUP BY PUBLISHER HAVING COUNT (ISBN) >= 1000;
Find the Highest Priced book(s) by Maier SELECT ISBN, MAX(PRICE) FROM BOOK WHERE AUTHORS LIKE “%Maier%”;
Chapter 5-40
CSE 4701
Nested Subqueries Nested Subqueries Allow us to Ask More Complex
Questions Regarding the Database Content Queries are Nested and Involve Set Relationships Relationships Supported Include:
Set Membership: IN, NOT IN Set Comparison
(=, <, <=, >, >=, <>) ALL (=, <, <=, >, >=, <>) SOME
Test Empty Relation: EXISTS, NOT EXISTS Let’s see Some Examples…
Chapter 5-41
CSE 4701
Set Membership: IN, NOT IN Find Title of the Books Ordered on Mondays SELECT DISTINCT TITLE FROM BOOK WHERE ISBN IN (SELECT ISBN FROM ORDER WHERE WEEKDAY = “MON”); Find Titles of Books ordered on Wednesday to Friday SELECT DISTINCT ISBN FROM BOOK WHERE WEEKDAY NOT IN (“MON”, “TUE”);
Chapter 5-42
CSE 4701
Set Comparison Operators
(=, <, <=, >, >=, <>) ALL (=, <, <=, >, >=, <>) SOME
Find ISBN of Books Published by ACM, which are Cheaper than all Books Ordered by Smith
SELECT ISBN FROM BOOK WHERE PUBLISHER LIKE “%ACM%” AND PRICE < ALL (SELECT B.PRICE FROM BOOK B, ORDER O WHERE CUST_NAME LIKE “%SMITH%” AND B.ISBN = O.ISBN);
Chapter 5-43
CSE 4701
EXISTS, NOT EXISTS Find ISBN of Books with a Price so low That There is
not any Cheaper Books from ACM SELECT B.ISBN FROM BOOK B WHERE B.PUBLISHER LIKE “%ACM%” AND NOT EXISTS (SELECT T.ISBN FROM BOOK T WHERE T.PUBLISHER LIKE “%ACM%” AND T.PRICE < B.PRICE);
Chapter 5-44
CSE 4701
Cancel all orders by Mary on 2/17/2000 DELETE FROM BOOK WHERE DATE=2000-02-17 AND ISBN IN (SELECT ISBN FROM ORDER WHERE CUST_NAME LIKE “%Mary%”); Update all Orders for Customers on 2/15/2002 by
giving a discount of 5% UPDATE ORDER SET PRICE = PRICE * (1-0.05) WHERE DATE=2002-02-50;
Delete and Update
Chapter 5-45
CSE 4701
View Concepts/ExamplesREM updatable view CREATE VIEW LOW-PRICE-BOOKS AS SELECT * FROM BOOKS WHERE PRICE<50.00;REM moves row outside viewUPDATE LOW-PRICE-BOOKS SET PRICE = 60.00 WHERE PUBLISHER LIKE “%ACM%”;REMcreate row outside viewINSERT INTO LOW-PRICE-BOOKS VALUES (“1010110022”, ”Java Beans”, “Smith”, 45, ”ACM”); REM prevents updates outside the viewCREATE VIEW LOW-PRICE-BOOKS AS SELECT * FROM BOOK WHERE PRICE<50.00 WITH CHECK OPTION;
Chapter 5-46
CSE 4701
Concluding Remarks What have we Seen in Chapter 5?
Complex Data Manipulation in SQLNested QueriesGrouping and AggregationOrder by and HavingViewsSets Tuple VariableOther Complex Queries and Operations
Strongly Encouraged to Engage in Practice with your DBMS of Choice for your Project
top related