Download Chapter 11 – Data Manipulation: Relational Algebra and SQL

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Extensible Storage Engine wikipedia , lookup

Microsoft Jet Database Engine wikipedia , lookup

Entity–attribute–value model wikipedia , lookup

Database wikipedia , lookup

Relational algebra wikipedia , lookup

Microsoft SQL Server wikipedia , lookup

Open Database Connectivity wikipedia , lookup

Clusterpoint wikipedia , lookup

PL/SQL wikipedia , lookup

SQL wikipedia , lookup

Database model wikipedia , lookup

Relational model wikipedia , lookup

Transcript
IT238: Data Modeling and
Database Design
Unit 9:
Data Manipulation: Relational Algebra and
SQL
Instructor: Qing Yan, M.D., Ph.D.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
1
Unit 9 Objectives
• Develop queries using the GROUP BY clause as a
way of grouping selected rows in an SQL query
• Develop queries using the HAVING clause as a way
of restricting groups returned in an SQL query
Chapter 1 – Database Systems: Architecture and
Components
2
Unit 9 To-Do List
• Complete the reading
Textbook and Web
• Participate in the discussion board
30 points
• Attend the introductory seminar or complete FLA quiz
20 points
• Complete the unit assignment
50 points
Chapter 1 – Database Systems: Architecture and
Components
3
Assignment
• 1. Display the highest, lowest, sum, and average
salary of all employees. Label the columns Maximum,
Minimum, Sum, and Average, respectively. Round
your results to the decimal position.
• 2. Display the minimum, maximum, sum, and
average salary for each job type.
• 3. Write a query to display the number of people with
the same job.
• 4. Determine the number of managers without listing
them. Label the column Number of Managers.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
4
Assignment
• 5. Display the manager number and the salary of the
lowest paid employee for that manager. Exclude
anyone whose manager is not known. Exclude any
groups where the minimum salary is less than $1000.
• 6. Write a query to display the department name,
location name, number of employees, and the
average salary for all employees in that department.
Label the columns dname, loc, Number of People,
and Salary, respectively. Round the average salary to
two decimal places.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
5
Key Concepts
• The PRIMARY KEY clause is used in the
specification of an entity integrity constraint.
• The UNIQUE clause is used in the specification of an
alternate key (uniqueness constraint).
• The FOREIGN KEY clause is used to define a tuplelevel referential integrity constraint.
• The REFERENCES clause is used to define both a
tuple-level and a column-level referential integrity
constraint.
Chapter 1 – Database Systems: Architecture and
Components
6
Vocabulary
Chapter 11 – Data Manipulation: Relational Algebra and SQL
7
Optional Add-ons To SELECT Clause
• GROUP BY group_by expression
• HAVING group_condition
• ORDER BY column name(s)
• group_by_expression forms groups of rows with the
same value
• group_condition filters the groups subject to some
condition
• column name(s) specifies the order of the output.
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
8
The COUNT Function
SELECT COUNT(*)
FROM TEXTBOOK;
• Note that COUNT is an aggregate function.
Aggregate functions ignore null values!
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
9
The COUNT Function (continued)
SELECT COUNT(TEXTBOOK.TX_PUBLISHER)
FROM TEXTBOOK;
• Aggregate functions are frequently applied to a group
of rows in a table rather than to all rows in a table.
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
10
The COUNT Function (continued)
SELECT TEXTBOOK.TX_PUBLISHER,
COUNT(TEXTBOOK.TX_PUBLISHER)
FROM TEXTBOOK
GROUP BY TEXTBOOK.TX_PUBLISHER;
space
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
11
The COUNT Function (continued)
SELECT TEXTBOOK.TX_PUBLISHER, TEXTBOOK.TX_YEAR,
COUNT (*)
FROM TEXTBOOK
GROUP BY TEXTBOOK.TX_PUBLISHER,
TEXTBOOK.TX_YEAR;
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
12
Aggregate Function and Grouping
Display the maximum, minimum, total, and average
salary for the professors affiliated with each
department. In addition, count the number of
professors in each department as well as the number
of professors in each department with a not null
salary.
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
13
Aggregate Functions and Grouping (continued)
SELECT DEPARTMENT.DPT_NAME "Dept Name",
DEPARTMENT.DPT_DCODE "Dept Code",
MAX(PROFESSOR.PR_SALARY) "Max Salary",
MIN(PROFESSOR.PR_SALARY) "Min Salary", SUM
(PROFESSOR.PR_SALARY) "Total Salary",
ROUND(AVG(PROFESSOR.PR_SALARY),0) "Avg Salary",
COUNT(*) "Size", COUNT(PROFESSOR.PR_SALARY) "#
Sals"
FROM DEPARTMENT JOIN PROFESSOR ON
DEPARTMENT.DPT_DCODE = ROFESSOR.PR_DPT_DCODE
GROUP BY DEPARTMENT.DPT_NAME,
EPARTMENT.DPT_DCODE
ORDER BY 6 DESC;
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
14
Aggregate Functions and Grouping (continued)
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
15
What is the purpose of the following query?
SELECT TAKES.TK_ST_SID, STUDENT.ST_NAME, COUNT(*)
AS "Sections Taken"
FROM STUDENT JOIN TAKES
ON STUDENT.ST_SID = TAKES.TK_ST_SID
GROUP BY TAKES.TK_ST_SID, STUDENT.ST_NAME
UNION
SELECT STUDENT.ST_SID, STUDENT.ST_NAME, 0
FROM STUDENT
WHERE STUDENT.ST_SID NOT IN
(SELECT TAKES.TK_ST_SID FROM TAKES)
ORDER BY “Sections Taken” DESC;
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
16
Working With Subqueries
• Subqueries are either uncorrelated or correlated
• Uncorrelated: the subquery is executed first and
passes one or more values to the outer query ( IN,
NOT IN, ANY, etc.)
• Correlated: the subquery is executed once for every
row in the outer query ( EXISTS)
• Note: Operators, such as =, <>, >, >=, <, and <= are
single-row operators!
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
17
Use of IN and NOT IN with Subqueries
• When used in conjunction with a subquery, the IN
operator evaluates if rows processed by the outer
query are equal to any of the values returned by the
subquery (i.e., it creates an OR condition).
• Example:
SELECT COURSE.CO_COURSE#, COURSE.CO_NAME,
COURSE.CO_COLLEGE FROM COURSE
WHERE COURSE.CO_COURSE# IN
(SELECT SECTION.SE_CO_COURSE# FROM SECTION);
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
18
User of ALL and ANY with Subqueries
• The ALL and ANY operators can be combined with other
comparison operators to treat the results of a subquery as
a set of values, rather than as individual values.
Operator
>ALL
<ALL
<ANY
>ANY
=ANY
Description
Greater than the highest value returned by the subquery
Less than the lowest value returned by the subquery
Less than the highest value returned by the subquery
Greater than the lowest value returned by the subquery
Equal to any value returned by the subquery (same as
the IN operator)
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
19
Use of the ALL Operator in a Subquery
(See Example 3.1.2.1)
SELECT * FROM PROFESSOR WHERE
PROFESSOR.PR_SALARY > ALL
(SELECT PROFESSOR.PR_SALARY FROM PROFESSOR
WHERE PROFESSOR.PR_DPT_DCODE = 3);
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
20
Replacing >ALL with >=ALL
SELECT * FROM PROFESSOR WHERE
PROFESSOR.PR_SALARY >= ALL
(SELECT PROFESSOR.PR_SALARY FROM PROFESSOR
WHERE PROFESSOR.PR_DPT_DCODE = 3);
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
21
Use of MIN and MAX with Subqueries
Example 1:
SELECT * FROM PROFESSOR WHERE
PROFESSOR.PR_SALARY >
(SELECT MAX(PROFESSOR.PR_SALARY) FROM
PROFESSOR WHERE PROFESSOR.PR_DPT_DCODE = 3);
Example 2:
SELECT * FROM PROFESSOR WHERE
PROFESSOR.PR_SALARY >= (SELECT
MAX(PROFESSOR.PR_SALARY) FROM PROFESSOR
WHERE PROFESSOR.PR_DPT_DCODE = 3);
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
22
Subqueries in FROM and SELECT
Clauses
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
23
Subqueries in the HAVING Clause
Display all professors with a salary that is equal to or exceeds the
average salary of all professors in their department
SELECT DEPARTMENT.DPT_NAME, AVG(PROFESSOR.PR_SALARY)
FROM DEPARTMENT JOIN PROFESSOR ON
DEPARTMENT.DPT_DCODE = ROFESSOR.PR_DPT_DCODE
GROUP BY DEPARTMENT.DPT_NAME
HAVING AVG(PROFESSOR.PR_SALARY) >
(SELECT AVG(PROFESSOR.PR_SALARY) FROM PROFESSOR);
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
24
EXISTS and NOT EXISTS
• A correlated subquery can be used if it is necessary
to check if a nested subquery returns no rows.
• Correlated subqueries make use of the EXISTS
operator which returns the value of true if a set is
non-empty.
• In a correlated subquery, the subquery is executed
once for each row in the outer query. In addition, the
execution of the subquery stops and the EXISTS
condition of the main query is declared true for a
given row should the condition in the subquery be
true.
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
25
EXISTS and NOT EXISTS (continued)
• A correlated nested subquery is processed differently
from an uncorrelated nested subquery.
• Instead of the execution of the subquery serving as
input to its parent query (i.e., the outer query), in a
correlated subquery, the subquery is executed once
for each row in the outer query.
• In addition, execution of the subquery stops and the
EXISTS condition of the main query is declared true
for a given row should the condition in the subquery
be true.
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
26
Display the names of professors who
have offered at least one section
SELECT PROFESSOR.PR_NAME
FROM PROFESSOR
WHERE EXISTS
(SELECT * FROM SECTION WHERE
PROFESSOR.PR_EMPID = SECTION.SE_PR_PROFID);
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
27
Display the names of professors who
have not offered a section
SELECT PROFESSOR.PR_NAME
FROM PROFESSOR
WHERE NOT EXISTS
(SELECT * FROM SECTION WHERE
PROFESSOR.PR_EMPID = SECTION.SE_PR_PROFID);
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Chapter 11 – Data Manipulation: Relational Algebra and SQL
28
Summary
• Using the GROUP BY clause as a way of grouping
selected rows in an SQL query
• Using the HAVING clause as a way of restricting
groups returned in an SQL query
From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
Q&A
• Questions?