5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN.

Slides:



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

Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error)
Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.
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.
Copyright  Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
1Eyad alshareef Enhanced Guide to Oracle 10g Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data.
GROUP FUNCTIONS. Objectives After completing this lesson, you should be able to do the following: Identify the available group functions Describe the.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: Identify the available group.
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.
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.
5 Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables.
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
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.
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.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4-1 Copyright  Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Copyright © 2004, Oracle. All rights reserved. Lecture 6 Displaying Data from Multiple Tables ORACLE.
4 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions.
Copyright س Oracle Corporation, All rights reserved. 5 Aggregating Data Using Group Functions.
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.
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.
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.
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Aggregating Data Using Group Functions. Objectives After completing this lesson, you should be able to do the following: –Identify the available group.
4 Copyright © 2006, Oracle. All rights reserved. Generating Reports by Grouping Related Data.
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.
1 Introduction to Database Systems, CS420 SQL JOIN, Group-by and Sub-query Clauses.
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
Enhanced Guide to Oracle 10g
Working with Tables: Join, Functions and Grouping
Aggregating Data Using Group Functions
(SQL) Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Aggregating Data Using Group Functions
Aggregating Data Using Group Functions
Reporting Aggregated Data Using the Group Functions
Query Functions.
Reporting Aggregated Data Using the Group Functions
Reporting Aggregated Data Using the Group Functions
分组函数 Schedule: Timing Topic 35 minutes Lecture 40 minutes Practice
Aggregating Data Using Group Functions
Presentation transcript:

5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN

5-2 Copyright © 2004, Oracle. All rights reserved. What is the Join? Use a join to query data from more than one table Write the join condition in the WHERE clause SELECTtable1.column, table2.column FROMtable1, table2 WHERE table1.column1=table2.column2

5-3 Copyright © 2004, Oracle. All rights reserved. Types of Joins Joins that are compliant with the SQL include the following: Cross joins Equijoin Natural joins USING clause Self join Non-equijoin Outer join

5-4 Copyright © 2004, Oracle. All rights reserved. SELECTtable1.column, table2.column FROMtable1 [LEFT|RIGHT|FULL OUTER JOIN table2 ON (table1.column_name = table2.column_name)] Outer join

5-5 Copyright © 2004, Oracle. All rights reserved. Outer Joins EMPLOYEESDEPARTMENTS There are no employees in department 190. …

5-6 Copyright © 2004, Oracle. All rights reserved. INNER Versus OUTER Joins In SQL:1999, the join of two tables returning only matched rows is called an inner join. A join between two tables that returns the results of the inner join as well as the unmatched rows from the left (or right) tables is called a left (or right) outer join. A join between two tables that returns the results of an inner join as well as the results of a left and right join is a full outer join.

5-7 Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN departments d ON (e.department_id = d.department_id) ; LEFT OUTER JOIN …

5-8 Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT OUTER JOIN departments d ON (e.department_id = d.department_id) ; RIGHT OUTER JOIN …

5-9 Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, d.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON (e.department_id = d.department_id) ; FULL OUTER JOIN …

5-10 Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, d.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON (e.department_id = d.department_id) ; FULL OUTER JOIN …

5-11 Copyright © 2004, Oracle. All rights reserved. Reporting Aggregated Data Using the Group Functions

5-12 Copyright © 2004, Oracle. All rights reserved. What Are Group Functions? Group functions operate on sets of rows to give one result per group. EMPLOYEES Maximum salary in EMPLOYEES table … Group functions

5-13 Copyright © 2004, Oracle. All rights reserved. Types of Group Functions FunctionDescriptionExample COUNT )*|Distinct exp) Number of rows (*,including duplicate rows with null ) COUNT(*) COUNT(First_name) COUNT(Distinct salary) COUNT(salary+100) COUNT(commission_pct) SUM ( Distinct exp( Sum values of exp, ignoring null values sum(salary) sum(Distinct salary) sum(salary+100) sum(Distinct salary+100) MAX( Distinct exp( Maximum value of exp, ignoring null values max( distinct salary/100) MIN ( Distinct exp( Minimum value of exp, ignoring null values min( distinct salary/100) AVG ( Distinct exp( Average value of exp, ignoring null values Avg(salary)

5-14 Copyright © 2004, Oracle. All rights reserved. SELECT [column,] group_function(column),... FROM table [WHERE condition] [GROUP BY column] [ORDER BY column]; Group Functions: Syntax

5-15 Copyright © 2004, Oracle. All rights reserved. SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary) FROM employees WHERE job_id =‘IT_PROG'; Using the AVG and SUM Functions You can use AVG and SUM for numeric data. SELECT MIN(hire_date), MAX(hire_date),MIN(First_name),MAX(First_name ) FROM employees; Using the MIN and MAX Functions You can use MIN and MAX for numeric, character, and date data types.

5-16 Copyright © 2004, Oracle. All rights reserved. Example Display the last one get a job in employees Display the name of the employee who get the max salary ERROR  cann’t display any column with a group function Display the total of salary for all employees who work in department number 60? select max(hire_date) from employees select sum(salary) from employees where department_id =60 select last_name,max(salary) from employees

5-17 Copyright © 2004, Oracle. All rights reserved. Group functions ignore null values in the column: The NVL function forces group functions to include null values: SELECT AVG(commission_pct) FROM employees SELECT AVG(NVL(commission_pct, 0)) FROM employees Group Functions and Null Values 1 2

5-18 Copyright © 2004, Oracle. All rights reserved. SELECT MAX(AVG(salary)) FROM employees GROUP BY department_id; Nesting Group Functions Display the maximum average salary:

5-19 Copyright © 2004, Oracle. All rights reserved. Creating Groups of Data EMPLOYEES … Average salary in EMPLOYEES table for each department

5-20 Copyright © 2004, Oracle. All rights reserved. You can divide rows in a table into smaller groups by using the GROUP BY clause. Creating Groups of Data: GROUP BY Clause Syntax SELECT column, group_function(column) FROM table [WHERE condition] [GROUP BY group_by_expression] [ORDER BY column];

5-21 Copyright © 2004, Oracle. All rights reserved. SELECT department_id, AVG(salary) FROM employees GROUP BY department_id Using the GROUP BY Clause All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.

5-22 Copyright © 2004, Oracle. All rights reserved. Using the GROUP BY Clause The GROUP BY column does not have to be in the SELECT list. SELECT AVG(salary) FROM employees GROUP BY department_id

5-23 Copyright © 2004, Oracle. All rights reserved. Grouping by More Than One Column EMPLOYEES Add the salaries in the EMPLOYEES table for each job, grouped by department …

5-24 Copyright © 2004, Oracle. All rights reserved. SELECT department_id dept_id, job_id, SUM(salary) FROM employees GROUP BY department_id, job_id Using the GROUP BY Clause on Multiple Columns

5-25 Copyright © 2004, Oracle. All rights reserved. Illegal Queries Using Group Functions You cannot use the WHERE clause to restrict groups. You use the HAVING clause to restrict groups. You cannot use group functions in the WHERE clause. SELECT department_id, AVG(salary) FROM employees WHERE AVG(salary) > 8000 GROUP BY department_id ERROR  group function is not allowed here Note:Cannot use the WHERE clause to restrict groups

5-26 Copyright © 2004, Oracle. All rights reserved. Illegal Queries Using Group Functions Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause: SELECT department_id, COUNT(last_name) FROM employees  Column missing in the GROUP BY clause

5-27 Copyright © 2004, Oracle. All rights reserved. Restricting Group Results EMPLOYEES … The maximum salary per department when it is greater than $10,000

5-28 Copyright © 2004, Oracle. All rights reserved. SELECT column, group_function FROM table [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY column]; Restricting Group Results with the HAVING Clause When you use the HAVING clause, the Oracle server restricts groups as follows: 1.Rows are grouped. 2.The group function is applied. 3.Groups matching the HAVING clause are displayed. Having : deal with results of Group function Where: deal with real value in table

5-29 Copyright © 2004, Oracle. All rights reserved. SELECT department_id, MAX(salary) FROM employees GROUP BY department_id HAVING MAX(salary)>10000 ; Using the HAVING Clause

5-30 Copyright © 2004, Oracle. All rights reserved. SELECT job_id, SUM(salary) PAYROLL FROM employees WHERE job_id NOT LIKE '%REP%' GROUP BY job_id HAVING SUM(salary) > ORDER BY SUM(salary); Using the HAVING Clause

5-31 Copyright © 2004, Oracle. All rights reserved. TRY 1. SELECT department_id, MAX(salary) FROM employees GROUP BY department_id HAVING MAX(salary)>10000 ORDER BY department_id 2. SELECT department_id, MAX(salary) FROM employees HAVING MAX(salary)>10000 GROUP BY department_id ORDER BY MAX(salary)