Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 9: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS Dr. Adam Anthony Fall 2012.
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
Introduction to Structured Query Language (SQL)
CPSC-608 Database Systems Fall 2011 Instructor: Jianer Chen Office: HRBB 315C Phone: Notes #3.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Chapter 11.1 and 11.2 Data Manipulation: Relational Algebra and SQL Brian Cobarrubia Introduction to Database Management Systems October 4, 2007.
Introduction to SQL J.-S. Chou Assistant Professor.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
Chapter 3 Single-Table Queries
Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
CSC271 Database Systems Lecture # 12. Summary: Previous Lecture  Row selection using WHERE clause  WHERE clause and search conditions  Sorting results.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Chapter 11 – Data Manipulation: Relational Algebra and SQL1 Unit 7: Data Manipulation: Relational Algebra and SQL IT238: Data Modeling and Database Design.
Using Special Operators (LIKE and IN)
Concepts of Database Management Seventh Edition
DATABASE TRANSACTION. Transaction It is a logical unit of work that must succeed or fail in its entirety. A transaction is an atomic operation which may.
Joins & Sub-queries. Oracle recognizes that you may want data that resides in multiple tables drawn together in some meaningful way. One of the most important.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Oracle DML Dr. Bernard Chen Ph.D. University of Central Arkansas.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Concepts of Database Management Eighth Edition Chapter 3 The Relational Model 2: SQL.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
1 CSE 480: Database Systems Lecture 12: SQL (Nested queries and Aggregate functions)
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Chapter 12 Subqueries and Merge Statements
Concepts of Database Management Seventh Edition Chapter 3 The Relational Model 2: SQL.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
CS 405G: Introduction to Database Systems Instructor: Jinze Liu Fall 2009.
© 2002 by Prentice Hall 1 Structured Query Language David M. Kroenke Database Concepts 1e Chapter 3 3.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
More SQL: Complex Queries,
CS580 Advanced Database Topics
Chapter 12 Subqueries and MERGE Oracle 10g: SQL
Chapter 3 Introduction to SQL(3)
CS580 Advanced Database Topics
Subqueries Schedule: Timing Topic 25 minutes Lecture
Working with Tables: Join, Functions and Grouping
Chapter 2: Intro to Relational Model
Using Subqueries to Solve Queries
(SQL) Aggregating Data Using Group Functions
CS 405G: Introduction to Database Systems
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Database Programming Using Oracle 11g
Aggregating Data Using Group Functions
Presentation transcript:

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.

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 SQL4

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 $ 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

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

Vocabulary Chapter 11 – Data Manipulation: Relational Algebra and SQL7

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Chapter 11 – Data Manipulation: Relational Algebra and SQL20 Use of the ALL Operator in a Subquery (See Example ) 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 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.

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.

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.

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.

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.

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.

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.

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.

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?