Download presentation
Presentation is loading. Please wait.
Published byBethanie Byrd Modified over 8 years ago
1
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design Instructor: Qing Yan, M.D., Ph.D.
2
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
3
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
4
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 SQL4
5
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 SQL5
6
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 tuple- level 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
7
Vocabulary Chapter 11 – Data Manipulation: Relational Algebra and SQL7
8
8 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.
9
Chapter 11 – Data Manipulation: Relational Algebra and SQL9 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.
10
Chapter 11 – Data Manipulation: Relational Algebra and SQL10 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.
11
Chapter 11 – Data Manipulation: Relational Algebra and SQL11 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.
12
Chapter 11 – Data Manipulation: Relational Algebra and SQL12 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.
13
Chapter 11 – Data Manipulation: Relational Algebra and SQL13 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.
14
Chapter 11 – Data Manipulation: Relational Algebra and SQL14 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.
15
Chapter 11 – Data Manipulation: Relational Algebra and SQL15 Aggregate Functions and Grouping (continued) From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
16
Chapter 11 – Data Manipulation: Relational Algebra and SQL16 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.
17
Chapter 11 – Data Manipulation: Relational Algebra and SQL17 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.
18
Chapter 11 – Data Manipulation: Relational Algebra and SQL18 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.
19
Chapter 11 – Data Manipulation: Relational Algebra and SQL19 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. OperatorDescription >ALLGreater than the highest value returned by the subquery <ALLLess than the lowest value returned by the subquery <ANYLess than the highest value returned by the subquery >ANYGreater than the lowest value returned by the subquery =ANYEqual 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.
20
Chapter 11 – Data Manipulation: Relational Algebra and SQL20 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.
21
Chapter 11 – Data Manipulation: Relational Algebra and SQL21 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.
22
Chapter 11 – Data Manipulation: Relational Algebra and SQL22 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.
23
Chapter 11 – Data Manipulation: Relational Algebra and SQL23 Subqueries in FROM and SELECT Clauses From: Umanath, N.S., & Scamell, R. (2007). Data Modeling and Database Design. Boston: Thomson Course Technology.
24
Chapter 11 – Data Manipulation: Relational Algebra and SQL24 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.
25
Chapter 11 – Data Manipulation: Relational Algebra and SQL25 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.
26
Chapter 11 – Data Manipulation: Relational Algebra and SQL26 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.
27
Chapter 11 – Data Manipulation: Relational Algebra and SQL27 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.
28
Chapter 11 – Data Manipulation: Relational Algebra and SQL28 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.
29
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.
30
Q & A Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.