Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "5-1 Copyright © 2004, Oracle. All rights reserved. DISPLAYING DATA FROM MULTIPLE TABLES OUTER JOIN."— Presentation transcript:

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

2 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

3 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

4 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-5 Copyright © 2004, Oracle. All rights reserved. Outer Joins EMPLOYEESDEPARTMENTS There are no employees in department 190. …

6 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.

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

8 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 …

9 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 …

10 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 …

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

12 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

13 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)

14 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

15 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.

16 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

17 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

18 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:

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

20 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];

21 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.

22 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

23 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 …

24 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

25 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

26 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

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

28 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

29 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

30 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) > 13000 ORDER BY SUM(salary); Using the HAVING Clause

31 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)


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

Similar presentations


Ads by Google