Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 4 Aggregating Data Using Group Functions.
Advertisements

SQL Subqueries Objectives of the Lecture : To consider the general nature of subqueries. To consider simple versus correlated subqueries. To consider the.
Subqueries 11. Objectives After completing this lesson, you should be able to do the following: Describe the types of problems that subqueries can solve.
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
AGGREGATE FUNCTIONS Prof. Sin-Min Lee Surya Bhagvat CS 157A – Fall 2005.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
5 Copyright © 2007, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
DATABASE PROGRAMMING Sections 5-7. Write a query that shows the average, maximum, and minimum salaries for all employees with jobs in the programming.
4 การใช้ SQL Functions. Copyright © 2007, Oracle. All rights reserved What Are Group Functions? Group functions operate on sets of rows to give.
Chapter 11 Group Functions
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
6 6 Subqueries Important Legal Notice:  Materials on this lecture are from a book titled “Oracle Education” by Kochhar, Gravina, and Nathan (1999), published.
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.
SQL - Part 2 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Chapter 3 Single-Table Queries
1 ICS 184: Introduction to Data Management Lecture Note 10 SQL as a Query Language (Cont.)
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
ACTION QUERIES (SQL COMMANDS ) STRUCTURED QUERY LANGUAGE.
Subqueries.
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.
5 Copyright © Oracle Corporation, All rights reserved. Aggregating Data Using Group Functions.
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.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
Structured Query Language Introduction. Basic Select SELECT lname, fname, phone FROM employees; Employees Table LNAMEFNAMEPHONE JonesMark SmithSara
1 Querying a Single Table Structured Query Language (SQL) - Part II.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Copyright  Oracle Corporation, All rights reserved. 12 Creating Views.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Grouping These slides are licensed under.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-row Subqueries, Correlated Subqueries.
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.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Advanced SQL. SQL - Nulls Nulls are not equal to anything - Null is not even equal to Null where columna != ‘ABC’ --this will not return records where.
1 ORACLE I 3 – SQL 1 Salim Phone: YM: talim_bansal.
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Aggregating Data Using Group Functions
Chapter 3 Introduction to SQL(3)
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Working with Tables: Join, Functions and Grouping
Using Subqueries to Solve Queries
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
SQL – Entire Select.
Chapter 4 Summary Query.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

Agenda for Class - 03/04/2014 Answer questions about HW#5 and HW#6 Review query syntax. Discuss group functions and summary output with the GROUP BY statement. Introduce sub-queries. 1

2 Structure of the SELECT statement SELECT [all or distinct] FROM (table) WHERE (condition) GROUP BY (grouping fields) HAVING (condition) ORDER BY (sort fields) Order of Actual Execution: 1)FROM 2)WHERE 3)GROUP BY 4)HAVING 5)SELECT 6)ORDER BY Referred to as the “SELECT LIST” When a SELECT statement is executed, the result is referred to as a “result table”. It is a memory-based table.

Table used for examples 3

Different SELECT options 4 SELECT DISTINCT deptno FROMemp1 SELECT TOP 1 salary FROM emp1 ORDER BY salary SELECT TOP 1 salary FROM emp1 ORDER BY salary SELECT TOP 10 PERCENT salary FROMemp1 ORDER BY salary desc

What is a Group Function? A way to summarize data and provide more meaningful and informative output from the database. Sometimes referred to as “summary queries” or “group functions.” Group functions differ from single row SELECT statements: –A SELECT statement processes every row in the underlying table. The result table (unless a WHERE clause is used) contains one row per row in the underlying table. –A group function collects data from multiple rows and produces summarized data in the result table. There should be one row in the result table per group. 5

What do group functions produce? If a group function is run on the whole table, without grouping, it generates a single row result table. If a group function is run with grouping (the GROUP BY statement) then it generates one row per group in the result table. 6

Why are group functions important? They provide information rather than data for the end-users of technology. They help programmers understand large data sets. 7

FunctionDescription of What is Returned AVG Average value of a numeric column; ignores null values COUNT Number of rows. When * is used, all rows are returned (including null values and duplicate rows) MAX Maximum value of a column; ignores null values MIN Minimum value of a column; ignores null values SUM Totals the value of a numeric column; ignores null values 8

Calculating Averages SELECT AVG(salary) FROM emp1; SELECT ROUND(AVG(salary),2) FROM emp1; SELECT ROUND(AVG(salary),2) FROM emp1 WHERE deptno = 10; Use the ROUND function to perform both a mathematical rounding operation and truncate the result to a set number of digits after the decimal point 9

Counting Rows SELECT COUNT(*) FROM emp1; SELECT COUNT(*) FROM emp1 WHERE deptno = 10; SELECT COUNT(*) FROM emp1 WHERE salary > 2000 and deptno = 10; SELECT COUNT(DISTINCT deptno) FROM emp1; 10

Finding Minimum and Maximum Values SELECT MIN(hiredate) FROM emp1; SELECT MAX(hiredate) FROM emp1; SELECT MIN(ename) FROM emp1; SELECT MAX(hiredate) FROM emp1 WHERE deptno = 10; 11

Groups with Calculations/Functions SELECTMAX(salary + ISNULL(comm,0)) FROMemp1; SELECTMAX(DATEDIFF(mm, hiredate, GETDATE()) FROMemp1; 12

Combining group functions SELECT COUNT(salary), SUM(salary), MIN(salary) FROM emp1 WHERE deptno = 10 and salary < 4000; 13

Group function issue… Combining group functions with single row values - doesn't work!! SELECT deptno, COUNT(salary), SUM(salary) FROM emp1 14

Creating summary output by grouping SELECT deptno, SUM(salary) FROM emp1 GROUP BY deptno; SELECT deptno, SUM(salary) FROM emp1 WHERE salary > 2000 GROUP BY deptno; Eliminates rows before the grouping occurs 15

Summary output with conditions SELECT deptno, SUM(salary) FROM emp1 GROUP BY deptno HAVING SUM(salary) > 6000; SELECT deptno, SUM(salary) FROM emp1 GROUP BY deptno HAVING AVG(salary) > 2000; The HAVING statement uses group functions for the condition or grouped attributes 16

Multi-attribute grouping SELECT deptno, job, SUM(salary), AVG(salary) FROM emp1 GROUP BY deptno, job; 17

A group function isn’t a condition!! SELECT MAX(salary) FROMemp1 The statement above does identify the maximum salary from the table. Now try to add the name of the employee to that query: SELECT ename, MAX(salary) FROMemp1

GROUP BY may not work exactly as you expect… Example 1: What if you want to see the employee name of the employee who has the maximum salary? SELECTename, MAX(salary) FROMemp1 GROUP BYename; This code does not accomplish that goal!

Thoughts about fixing the problem… SELECTename, max(salary) FROMemp1 WHEREsalary = MAX(salary) GROUP BYename; SELECTename, max(salary) FROMemp1 GROUP BYename HAVINGmax(salary) = salary; They don’t work!

Could use the TOP 1 SELECT SELECTTOP 1 ename, salary, deptno FROMemp1 ORDER BY salary desc But this works only in the SQL Server environment because TOP 1 is not ANSI-Standard SQL

ANSI–Standard SQL Needs a sub-query to work correctly SELECTename, salary, deptno FROMemp1 WHEREsalary = (SELECT MAX(salary) FROMemp1) Example 1 answer:

What is a sub-query? A sub-query is a query embedded inside another query. The sub-query is executed in the normal operation of the query in which it is embedded. The sub-query will return an “answer” result table to the query in which it is embedded. A sub-query can be placed in the SELECT list, FROM statement, WHERE clause &/or HAVING clause. Two types of sub-queries: non-correlated and correlated.

Types of sub-queries Non-correlated –Inner and outer queries do not exchange data. Correlated –Inner and outer queries do exchange data. 24

Example 2 of a non-correlated sub-query 25 Which employee has been employed with the organization for the longest time? What is the name and salary of that employee? Or another way to look at it: What is the name and salary of the employee who was hired first in our organization Write a query to accomplish that goal!! (Use slide #22 as your example)

Let’s expand our sample data to include a new table for department 26

27 What if we want to see all the employees who are in deptno 20? No problem... SELECT* FROMemp1 WHEREdeptno = 20 Relational operators in a single table

28 What if we want to see all employees who have any department that is in the dept1 table ? SELECT* FROMemp1 WHEREdeptno IN (SELECT deptno FROM dept1) Example 3 of a non-correlated sub-query:

29 What if we want to see all employees who don’t have a department that is in the dept1 table ? SELECT* FROMemp1 WHEREdeptno NOT IN (SELECT deptno FROM dept1) Example 4 of a non-correlated sub-query:

Example of the need for a correlated sub-query Example 1 of a correlated sub-query: which employees have a higher salary than the average salary for their department? SELECT deptno, AVG(salary) FROMemp1 GROUP BY deptno; SELECT empno, ename, deptno, salary FROM emp1 ORDER BY deptno

A Correlated Sub-Query 31 SELECTempno, ename, deptno, salary FROMemp1 outquery WHEREsalary > (SELECTAVG(salary) FROMemp1 inquery WHEREoutquery.deptno = inquery.deptno) Requires that the tables have aliases

What if we also want to see the average salary in the SELECT list? 32 SELECTempno, ename, deptno, salary, (SELECT AVG(salary) FROMemp1 squery WHERE squery.deptno = outquery.deptno) FROMemp1 outquery WHEREsalary > (SELECTAVG(salary) FROMemp1 inquery WHEREoutquery.deptno = inquery.deptno)