Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.

Slides:



Advertisements
Similar presentations
Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Advertisements

DatabaseDatabase cs453 Lab8 1 Ins.Ebtesam AL-Etowi.
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
After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group.
LECTURE 10.  Group functions operate on sets of rows to give one result per group.
Writing Basic SQL SELECT Statements. Capabilities of SQL SELECT Statements A SELECT statement retrieves information from the database. Using a SELECT.
1 Lecture 3: Subqueries DCO11310 Database Systems and Design By Rose Chang.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
Objectives After completing this lesson, you should be able to do the following: Define subqueries Describe the types of problems that the subqueries.
Enhancements to the GROUP BY Clause Fresher Learning Program January, 2012.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
1 Copyright © Oracle Corporation, All rights reserved. Writing Basic SQL SELECT Statements.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
2 Copyright © Oracle Corporation, All rights reserved. Restricting and Sorting Data.
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.
6 Copyright © 2004, Oracle. All rights reserved. Using Subqueries to Solve Queries.
Structured Query Language. Group Functions What are group functions ? Group Functions Group functions operate on sets of rows to give one result per group.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
Nikolay Kostov Telerik Corporation
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
Copyright © 2004, Oracle. All rights reserved. Lecture 4: 1-Retrieving Data Using the SQL SELECT Statement 2-Restricting and Sorting Data Lecture 4: 1-Retrieving.
1 Querying a Single Table Structured Query Language (SQL) - Part II.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
Retrieving Data Using the SQL SELECT Statement. Objectives After completing this lesson, you should be able to do the following: – List the capabilities.
1 SQL SQL (Structured Query Language) : is a database language that is used to create, modify and update database design and data. Good Example of DBMS’s.
15 Copyright © Oracle Corporation, All rights reserved. Using SET Operators.
Copyright © 2004, Oracle. All rights reserved. Using the Set Operators.
6 Copyright © Oracle Corporation, All rights reserved. Subqueries.
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.
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.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
Reporting Aggregated Data Using the Group Functions
Using Subqueries to Solve Queries
Aggregating Data Using Group Functions
Using the Set Operators
Using Subqueries to Solve Queries
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Using the Set Operators
Aggregating Data Using Group Functions
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Using Subqueries to Solve Queries
Reporting Aggregated Data Using the Group Functions
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
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries

Marge Hohly2 Group functions  Group functions operate on sets of rows to give one result per group AVG COUNT MAX MIN SUM STDDEV VARIANCE

Marge Hohly3 AVG function  Returns the average of a set of values – usable only on columns of number type  Syntax: SELECT AVG(salary) FROM employees WHERE job_id LIKE ‘%REP%’;

Marge Hohly4 SUM function  Returns the sum of a set of values – usable only on columns of number type  Syntax: SELECT SUM(salary) FROM employees WHERE job_id LIKE ‘%REP%’;

Marge Hohly5 MIN and MAX functions  Return the minimum and maximum value from a set of values  May be used with columns of NUMBERS, VARCHAR2, and DATE datatype SELECT MIN(department_id) FROM departments; SELECT MAX(last_name) FROM employees; SELECT MIN(hire_date), MAX(hire_date) FROM employees WHERE job_id LIKE ‘%REP%’;

Marge Hohly6 COUNT function  Returns the number of rows counted with non null values for the expression specified SELECT COUNT(commission_pct) FROM employees;

Marge Hohly7 COUNT function cont’d  COUNT(*) returns the number of rows in the table SELECT COUNT(*) FROM employees;

Marge Hohly8 STDDEV function  A statistical function that returns the standard deviation ignoring null values for expressions of NUMBER type SELECT STDDEV(salary) FROM employees;

Marge Hohly9 VARIANCE function  A statistical function that returns the variance ignoring null values for expressions NUMBER type SELECT VARIANCE(salary) FROM employees;

Marge Hohly10 DISTINCT keyword  The DISTINCT keyword can be used with all group functions  In forces the group function to consider only non-duplicate values SELECT COUNT(DISTINCT(last_name)) FROM employees;

Marge Hohly11 Group functions & NULL values  Group functions ignore NULL values SELECT AVG(commission_pct) FROM employees;  The average commission_pct will only be calculated using those rows that have a commission_pct, null rows will be excluded.

Marge Hohly12 NVL function  This is used to replace a NULL with a given value  The value must be of the same datatype as the colunm NO! SELECT commission_pct, NVL(commission_pct, ‘not eligible’) FROM employees; YES! SELECT commission_pct, NVL(commission_pct, 0) FROM employees;

Marge Hohly13 Using NVL with group functions  The NVL function is nested inside the group function When you want to include rows will null values, use NVL function to add a value to the null rows. SELECT AVG(commission_pct), AVG(NVL(commission_pct,0)) FROM employees; Which column will have the lowest value?

Marge Hohly14 GROUP BY Clause  Use the Group By clause to divide the rows in a table into groups than apply the Group Functions to return summary information about that group  In the example below, the rows are being GROUPed BY department_id. The AVG(group functin) is then applied to each GROUP, or department_id.  SELECT department_id, AVG(salary) FROM employees GROUP BY department_id;

Marge Hohly15 Results of previous query

Marge Hohly16 GROUP BY Clause  With aggregate (group) functions in the SELECT clause, be sure to include individual columns from the SELECT clause in a GROUP BY clause!!!  SELECT department_id, job_id, SUM(salary) FROM employees WHERE hire_date <= ’01-JUN-00’ GROUP BY department_id, job_id;  SELECT d.department_id,d.department_name, MIN(e.hire_date) AS “Min Date” FROM departments d, employees e WHERE e.department_id=d.department_id GROUP BY ?????

Marge Hohly17 GROUP BY Rules...  Use the Group By clause to divide the rows in a table into groups then apply the Group Functions to return summary information about that group.  If the Group By clause is used, all individual columns in the SELECT clause must also appear in the GROUP BY clause.  Columns in the GROUP BY clause do not have to appear in the SELECT clause  No column aliases can be used in the Group By clause.  Use the ORDER BY clause to sort the results other than the default ASC order.  The WHERE clause, if used, can not have any group functions – use it to restrict any columns in the SELECT clause that are not group functions.  Use the HAVING clause to restrict groups not the WHERE clause.

Marge Hohly18 The HAVING Clause  With the HAVING clause the Oracle Server: 1.Groups rows. 2.Applies group function to the group(s). 3.Displays the groups that match the criteria in the HAVING clause. SELECT department_id, job_id, SUM(salary) FROM employees WHERE hire_date 50;

Marge Hohly19 The HAVING clause example SELECT department_id, job_id, SUM(salary) FROM employees WHERE hire_date 50;

Marge Hohly20 WHERE or HAVING ??  The WHERE clause is used to restrict rows. SELECT department_id, MAX(salary) FROM employees WHERE department_id>=20 GROUP BY department_id;  The HAVING clause is used to restrict groups returned by a GROUP BY clause. SELECT department_id, MAX(salary) FROM employees GROUP BY department_id HAVING MAX(salary)> 1000;

Marge Hohly21 SUBQUERY  SELECT statement that is embedded in a clause of another SELECT statement  Can be placed in the WHERE clause, the HAVING clause, and the FROM clause.  Executes first and its output is used to complete the query condition for the main or outer query.  SELECT last_name FROM employees WHERE salary > (SELECT salary FROM employees WHERE employee_id = 104);

Marge Hohly22 SUBQUERIES  Guidelines for using subqueries are: The subquery is enclosed in parentheses. The subquery is placed on the right side of the comparison condition. The outer and inner queries can get data from different tables. Only one ORDER BY clause can be used for a SELECT statement; and, if specified, it must be the last clause in the main SELECT statement. The only limit on the number of subqueries is the buffer size the query uses.

Marge Hohly23 SUBQUERIES  There are two types of subqueries: Single-row subqueries that use single- row operators (>,=.>=,,<=) and return only one row from the inner query. Multiple-row subqueries that use multiple-row operators (IN, ANY, ALL) and return more than one row from the inner query.

Marge Hohly24 SINGLE-ROW SUBQUERY  SELECT last_name FROM employees WHERE salary > (SELECT salary FROM employees WHERE last_name = ‘Abel’); Subquery returns 11000, thus the main SELECT statement returns all employees with a salary greater than What is there is more than one Abel in the company??

Marge Hohly25 Example  SELECT department_id, department_name FROM departments WHERE department_id = (SELECT department_id FROM employees WHERE salary < 4000);  What will result?

Marge Hohly26 SUBQUERIES FROM DIFFERENT TABLES  Subqueries are not limited to just one inner query. As the example illustrates, there can be more than one subquery returning information to the outer query. Also, the outer and inner queries can get data from different tables.  SELECT last_name,job_id,salary,department_id FROM employees WHERE job_id= (SELECT job_id FROM employees WHERE employee_id = 141) AND department_id = (SELECT department_id FROM departments WHERE location_id = 1500);  Subquery returns job_id = ST_CLERK and department_id = 50

Marge Hohly27 Results of previous query

Marge Hohly28 SUBQUERIES  Gropu functions can be used in single-row subqueries. The inner query returns a single row to the outer query. SELECT last_name,first_name,salary FROM f_staffs WHERE salary < (SELECT MAX(salary) FROM f_staffs);

Marge Hohly29 Example results  Subquery returns MAX(salary) = 60

Marge Hohly30 GROUP Functions in HAVING Clause  HAVING clause is used to restrict gropus and always has a group condition (such as MIN, MAX, AVG) stated  See next page for example

Marge Hohly31 GROUP Functions in HAVING Clause  SELECT department_id, MIN(salary) FROM employees GROUP BY department_id HAVING MIN(salary) > (SELECT MIN(salary) FROM employees WHERE department_id = 50);  Inner query returns 7500 for minimum salary