Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
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.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Introduction to Oracle9i: SQL1 SQL Group Functions.
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Displaying Data from Multiple Tables. Obtaining Data from Multiple Tables Sometimes you need to use data from more than one table. In the example, the.
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using.
Database Programming Sections 5– GROUP BY, HAVING clauses, Rollup & Cube Operations, Grouping Set, Set Operations 11/2/10.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Sections 3 – Joins & Hierarchical Queries
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
Basisdata Pertanian. After completing this lesson, you should be able to do the following:  Write SELECT statements to access data from more than one.
Database Programming Sections 6 –Subqueries, Single Row Subqueries, Multiple-column subqueries, Multiple-row Subqueries, Correlated Subqueries 11/2/10,
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.
Join, Subqueries and set operators. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Nikolay Kostov Telerik Corporation
Copyright © 2004, Oracle. All rights reserved. D ISPLAYING D ATA FROM M ULTIPLE T ABLES.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Intermediate SQL: Aggregated Data, Joins and Set Operators.
Basic Group Functions (without GROUP BY clause) Week 5 – Chapter 5.
Database Programming Sections 4 – Joins. Marge Hohly2 Overview  Oracle Proprietary Joins (8i and prior): Cartesian Product Equijoin Non-equijoin Outer.
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.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Database Programming Sections 3 – Oracle Joins. Marge Hohly2 Obtaining Data from Multiple Tables: Using Joins.
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.
5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.
4 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Reporting Aggregated Data Using the Group Functions
Aggregating Data Using Group Functions
Oracle Join Syntax.
Displaying Data from Multiple Tables
Displaying Data from Multiple Tables
Using the Set Operators
Displaying Data from Multiple Tables
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Using the Set Operators
Aggregating Data Using Group Functions
Displaying Data from Multiple Tables Using Joins
Oracle Join Syntax.
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
Section 4 - Sorting/Functions
Oracle Join Syntax.
Reporting Aggregated Data Using the Group Functions
Displaying Data from Multiple Tables
Reporting Aggregated Data Using the Group Functions
Displaying Data from Multiple Tables
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL 10/3/06 revised 11/13/07 revised again 11/2/10 DP_4Lesson_Fa11.ppt DP04_Fa11.txt

Cross Join SELECT last_name, department_name FROM employees CROSS JOIN departments; SELECT last_name, department_name FROM employees, departments; The 2nd gets the same results as the first. Without the WHERE clause you get a Cartesian product Both above return 160 rows. Marge Hohly

Natural Join SELECT employee_id, last_name, department_id, department_name FROM employees NATURAL JOIN departments ; SELECT e.employee_id, e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id = d.department_id; SELECT last_name, department_name FROM employees NATURAL JOIN departments; returns 12 rows Marge Hohly

Join…On and Join…Using SELECT employee_id, last_name, department_name FROM employees JOIN departments USING (department_id); Joins by column names and data types that are identical in each table but USING statement limits to one column. Join .. On SELECT e.employee_id, e.last_name, d.department_id, d.location_id FROM employees e JOIN departments d ON (e.department_id = d.department_id); All employees and their work locations. Works when column name are different Marge Hohly

Join .. Using SELECT e.employee_id, e.last_name, e.department_id, d.department_name FROM employees e JOIN departments d USING (department_id ); Compare with previous and next slide Marge Hohly

Join .. On SELECT e.employee_id, e.last_name, e.department_id, d.location_id FROM employees e JOIN departments d ON (e.department_id = d.department_id ); Marge Hohly

Join .. On SELECT e.employee_id, e.last_name,e.salary FROM employees e JOIN job_grades j ON (e.salary BETWEEN j.lowest_sal AND j.highest_sal); Marge Hohly

Join.. On SELECT e.employee_id, e.last_name,e.salary, j.grade_level FROM employees e,job_grades j WHERE e.salary >= j.lowest_sal AND e.salary <= j.highest_sal; SELECT e.employee_id, e.last_name,e.salary, j.grade_level FROM employees e,job_grades j WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal; Note these give the same results Marge Hohly

Self-Joins (Join.. ON) Joining the table to itself Not common join SELECT e.last_name emp, m.last_name mgr FROM employees e JOIN employees m ON(e.manager_id = m.employee_id); Joining the table to itself Not common join Marge Hohly

Outer Joins (Right Outer Join, Left Outer Join) SELECT e.employee_id, e.last_name, e.department_id, d.department_name FROM employees e RIGHT OUTER JOIN departments d ON( e.department_id = d.department_id); These product the same results. Right shows all data from right table (departments) including Nulls, shows departments without employees Left shows all data from Employees. Returns all employees with or without a department Marge Hohly

Outer Joins (Right Outer Join, Left Outer Join) SELECT e.employee_id, e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN departments d ON( e.department_id = d.department_id); These product the same results. Marge Hohly

Full Outer Join SELECT e.employee_id, e.last_name, e.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON(e.department_id = d.department_id); Retrieves all data from employees and departments. Shows employees without departments and departments without employees. Marge Hohly

Group Functions Group functions operate on sets of rows to give one result per group AVG COUNT MAX MIN SUM STDDEV VARIANCE Returns as many lines as there are groups These are "aggregate.“ functions. They require that a group of values be treated together Group functions, unlike single-row functions, operate on sets of rows and give one result back per group. Some of the group functions are listed on this page, including A-V-G for average, COUNT, MAX, which stands for maximum, MIN, which stands for minimum, S-T-D-D-E-V which stands for standard deviation, SUM, and VARIANCE. They can be used in many parts of a SELECT statement, except for the WHERE clause. Marge Hohly

Group functions Used in the SELECT clause Never used in the WHERE clause Group functions ignore NULL values You can have more than one group function in the SELECT clause on same or different columns Some group functions work on numeric data types only Marge Hohly

The MIN and MAX functions are used to return the minimum or maximum value from a set of values. These functions may be used with columns that have NUMBER, VARCHAR2, and DATE data types. When MIN and MAX are used with numeric data types, they return the smallest or the largest values. When used with DATE, MIN returns the earliest date and MAX returns the latest or most recent date. With character values, MIN returns the character closest to upper case A, MAX returns the character at the other end of the alphabetic scale, including the numbers and other special characters. When used with character values, MIN and MAX will search beginning with the first character. If the first characters are the same in all values, it will move on to the second value, and so forth. In the example to the right, the syntax SELECT MAX(salary) FROM employees, returns the highest number in the salary column of the employees table. The SUM function returns the sum of a set of values. It is usable only on numeric data types. The AVG function returns the average of a set of values; it is also usable only on columns with a number type. Marge Hohly

The COUNT function returns the number of rows counted with not null values for the expression specified. COUNT star returns the number of rows in a table. This is used to ensure all rows are counted, in case particular columns have null values. VARIANCE is a statistical function. This returns the variance and, again, ignores null values. It can also only be used with number data types. S-T-D-D-E-V is the statistical function that returns the standard deviation. It ignores null values and it can only be used with numeric data types. A standard deviation is a measure of the spread or dispersion of a set of data. In the example on the right, SELECT STDDEV (salary) FROM employees measures the standard deviation across all employee salaries. Marge Hohly

Marge Hohly

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 Hohly

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%’; SELECT SUM(salary) FROM employees WHERE department_id = 80; Without GROUP BY clause returns 1 line. SELECT SUM(salary) FROM employees GROUP BY department_id; SELECT department_id, SUM(salary) FROM employees GROUP BY department_id; SELECT type_code FROM d_songs WHERE SUM(duration) = 100; Marge Hohly

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 datatypes 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 Hohly

COUNT function Returns the number of rows counted with non-null values for the expression specified SELECT COUNT(commission_pct) FROM employees; SELECT COUNT(year) FROM d_cds WHERE year < 2001; SELECT COUNT(DISTINCT year) FROM d_cds WHERE year < 2001; Null values are ignored if the Null are not specified with another value. Note example above counts the 4 non-null values. SELECT COUNT(commission_pct),SUM(commission_pct),AVG(commission_pct), Count(NVL(commission_pct,0)), AVG(NVL(commission_pct,0)) FROM employees; Marge Hohly

COUNT function cont’d COUNT(*) returns the number of rows in the table SELECT COUNT(*) FROM employees; Shows that there are 20 records in the Employees table Marge Hohly

STDDEV function A statistical function that returns the standard deviation ignoring null values for expressions of NUMBER type SELECT STDDEV(salary) FROM employees; SELECT COUNT(salary),ROUND(STDDEV(salary),1) FROM employees; Marge Hohly

VARIANCE function A statistical function that returns the variance ignoring null values for expressions NUMBER type SELECT VARIANCE(salary) FROM employees; Run command. SELECT ROUND(VARIANCE(salary),1) FROM employees; Marge Hohly

Multiple group functions Can use more than one group function in the SELECT clause Same or different columns SELECT MAX(salary), MIN(salary), MIN(employee_id FROM employees WHERE department_id = 60; Let’s review a few facts about using group functions. Group functions are written in the SELECT clause and cannot be used in the WHERE clause. They ignore NULL values. You can use more than one group function in the SELECT clause and even on different columns. Consider adding a WHERE condition to restrict the number of rows returned as in the example on this slide. SUM, AVG, STDDEV, VARIANCE – numeric data type only Marge Hohly

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; SELECT COUNT(DISTINCT(department_id)) FROM employees; Results 7 rows rather than 19 rows without DISTINCT SELECT COUNT(Year) FROM d_cds WHERE year < 2001; SELECT COUNT(DISTINCT Year) We’ve seen the use of the DISTINCT keyword in the SELECT statement to eliminate duplicate values. You can also use the DISTINCT keyword with group functions. It forces the group function to consider only non-duplicate values. The keyword DISTINCT, when used in a query selecting more than one column, will return non-duplicate combinations of the columns. Place the DISTINCT keyword inside the GROUP function. Take time to execute several queries using the DISTINCT keyword in Application Express. Note that if DISTINCT is placed outside of the COUNT function, it will have no effect on the result of the query. Marge Hohly

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; Unlike single-row functions, group functions ignore null values. Remember, if a null value is encountered with a single-row function, it returns null as an answer. The null value function, which we encountered before, can be used inside of group functions. Look at the SELECT clause below. The null value is used to change the values of customer_orders from null values to zero. Take time to use the null value function in Application Express and be sure to note the differences when executing the same query with and without the null value function SELECT AVG(NVL(customer_order, 0)) FROM Marge Hohly

Using NVL with group functions The NVL function is nested inside the group function When you want to include rows with 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? AVG(commission_pct) AVG(NVL(commission_pct,0)) .2125 .0425 (this one averaged all values and where there was no value a 0 was put in.) Marge Hohly