Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-1 COS 346 Day 15.

Similar presentations


Presentation on theme: "Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-1 COS 346 Day 15."— Presentation transcript:

1 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-1 COS 346 Day 15

2 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-2 Agenda Assignment 5 re-graded (based on extra info provided)Assignment 5 re-graded (based on extra info provided) –3 A’s, 1 B, 1 C’s and 1 D –Lots of missing data Assignment 6 PostedAssignment 6 Posted –Due March 22 Quiz 2 will April 13 (Friday the 13 th)Quiz 2 will April 13 (Friday the 13 th) –DP 7 & 8, SQL 2-10 –There will only be 3 exams (3 @ 10% instead of 4 @ 7.5%) Capstone Progress Reports Over DueCapstone Progress Reports Over Due –Missing 3 –SQL Coding Exercises and Question on page 44 Today we will discusToday we will discus – (Chap 5) –Aggregate Row Functions (Chap 5) –Joins (Chap 6) –Lots of Hands-on stuff We will be in the Oracle SQL text for the next 2+ weeksWe will be in the Oracle SQL text for the next 2+ weeks

3 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-3 CHAPTER 5: AGGREGATE ROW FUNCTIONS

4 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-4 AGGREGATE ROW FUNCTIONS Aggregate Row functions give the user the ability to answer business questions such as:Aggregate Row functions give the user the ability to answer business questions such as:  What is the average salary of an employee in the company?  What were the total salaries for a particular year?  What are the maximum and minimum salaries in the Computers Department?

5 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-5 AGGREGATE ROW FUNCTIONS Aggregate functions perform a variety of actions such as counting all the rows in a table, averaging a column's data, and summing numeric data.Aggregate functions perform a variety of actions such as counting all the rows in a table, averaging a column's data, and summing numeric data. Aggregates can also search a table to find the highest "MAX" or lowest "MIN" values in a column.Aggregates can also search a table to find the highest "MAX" or lowest "MIN" values in a column.

6 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-6 AGGREGATE ROW FUNCTIONS List of aggregate functions including their syntax and use.List of aggregate functions including their syntax and use. Function SyntaxFunction Use SUM( [ALL | DISTINCT] expression ) The total of the (distinct) values in a numeric column/expression. AVG( [ALL | DISTINCT] expression ) The average of the (distinct) values in a numeric column/expression. COUNT( [ALL | DISTINCT] expression ) The number of (distinct) non-NULL values in a column/expression. COUNT(*) The number of selected rows. MAX(expression) The highest value in a column/expression. MIN(expression)The lowest value in a column/expression.

7 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-7 AGGREGATE ROW FUNCTIONS There are two rules that you must understand and follow when using aggregates:There are two rules that you must understand and follow when using aggregates: Aggregate functions can be used in both the SELECT and HAVING clauses (the HAVING clause is covered later in this chapter).Aggregate functions can be used in both the SELECT and HAVING clauses (the HAVING clause is covered later in this chapter). Aggregate functions cannot be used in a WHERE clause.Aggregate functions cannot be used in a WHERE clause.

8 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-8 EXAMPLE The following query is wrong and will produce the Oracle ORA-00934 group function is not allowed here error message.The following query is wrong and will produce the Oracle ORA-00934 group function is not allowed here error message. SELECT * FROM employee WHERE AVG(emp_salary) > 40000; ERROR at line 3: ORA-00934: group function is not allowed here.

9 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-9 COUNT( ) If a manager needs to know how many employees work in the organization, COUNT(*) can be used to produce this information.If a manager needs to know how many employees work in the organization, COUNT(*) can be used to produce this information. The COUNT(*) function counts all rows in a table.The COUNT(*) function counts all rows in a table. The wild card asterisk (*) would be used as the parameter in the function.The wild card asterisk (*) would be used as the parameter in the function. SELECT COUNT(*) FROM employee; COUNT(*) COUNT(*) -------- -------- 8

10 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-10 COUNT( ) The result table for the COUNT(*) function is a single scalar value.The result table for the COUNT(*) function is a single scalar value. Notice that the result table has a column heading that corresponds to the name of the aggregate function specified in the SELECT clause.Notice that the result table has a column heading that corresponds to the name of the aggregate function specified in the SELECT clause. The output column can be assigned a more meaningful column name as is shown in the revised query.The output column can be assigned a more meaningful column name as is shown in the revised query.

11 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-11 COUNT( ) This is accomplished by simply listing the desired column name inside double-quotes after the aggregate function specification.This is accomplished by simply listing the desired column name inside double-quotes after the aggregate function specification. SELECT COUNT(*) "Number of Employees" FROM employee; Number of Employees --------------------------- 8

12 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-12 COUNT( ) COUNT(*) is used to count all the rows in a table.COUNT(*) is used to count all the rows in a table. COUNT(column name) does almost the same thing. The difference is that you may define a specific column to be counted.COUNT(column name) does almost the same thing. The difference is that you may define a specific column to be counted. When column name is specified in the COUNT function, rows containing a NULL value in the specified column are omitted.When column name is specified in the COUNT function, rows containing a NULL value in the specified column are omitted. A NULL value stands for “unknown” or “unknowable” and must not be confused with a blank or zero.A NULL value stands for “unknown” or “unknowable” and must not be confused with a blank or zero.

13 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-13 COUNT ( ) SELECT COUNT(emp_superssn) "Number Supervised Employees" FROM employee; Number Supervised Employees --------------------------- 7 7 In contrast the count(*) will count each row regardless of NULL values.In contrast the count(*) will count each row regardless of NULL values. SELECT COUNT(*) "Number of Employees “ FROM employee; Number of Employees ------------------- 8 8

14 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-14 Using the AVG Function AVG function is used to compute the average value for the emp_salary column in the employee table.AVG function is used to compute the average value for the emp_salary column in the employee table. For example, the following query returns the average of the employee salaries.For example, the following query returns the average of the employee salaries. SELECT AVG(emp_salary) "Average Employee Salary" FROM employee; Average Employee Salary ----------------------- $35,500 $35,500

15 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-15 More Examples What is the average salary offered to employees?What is the average salary offered to employees? This question asks you to incorporate the concept of computing the average of the distinct salaries paid by the organization.This question asks you to incorporate the concept of computing the average of the distinct salaries paid by the organization. The same query with the DISTINCT keyword in the aggregate function returns a different average.The same query with the DISTINCT keyword in the aggregate function returns a different average. SELECT AVG(DISTINCT emp_salary) "Average Employee Salary" FROM employee; Average Employee Salary ----------------------- $38,200 $38,200

16 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-16 Using the SUM Function The SUM function can compute the total of a specified table column.The SUM function can compute the total of a specified table column. The SELECT statement shown here will return the total of the emp_salary column from the employee table.The SELECT statement shown here will return the total of the emp_salary column from the employee table. SELECT SUM(emp_salary) "Total Salary" FROM employee; Total Salary ------------ $284,000 $284,000

17 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-17 More Examples If management is preparing a budget for various departments, you may be asked to write a query to compute the total salary for different departments.If management is preparing a budget for various departments, you may be asked to write a query to compute the total salary for different departments. The query shown here will compute the total emp_salary for employees assigned to department #7.The query shown here will compute the total emp_salary for employees assigned to department #7. SELECT SUM(emp_salary) "Total Salary Dept 7" FROM employee WHERE emp_dpt_number = 7; Total Salary Dept 7 ------------------- $136,000 $136,000

18 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-18 MIN and MAX Functions MIN and MAX Functions The MIN function returns the lowest value stored in a data column.The MIN function returns the lowest value stored in a data column. The MAX function returns the largest value stored in a data column.The MAX function returns the largest value stored in a data column. Unlike SUM and AVG, the MIN and MAX functions work with both numeric and character data columns.Unlike SUM and AVG, the MIN and MAX functions work with both numeric and character data columns.

19 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-19Example A query that uses the MIN function to find the lowest value stored in the emp_last_name column of the employee table.A query that uses the MIN function to find the lowest value stored in the emp_last_name column of the employee table. This is analogous to determine which employee's last name comes first in the alphabet.This is analogous to determine which employee's last name comes first in the alphabet. Conversely, MAX() will return the employee row where last name comes last (highest) in the alphabet.Conversely, MAX() will return the employee row where last name comes last (highest) in the alphabet. SELECT MIN(emp_last_name), MAX(emp_last_name) FROM employee; MIN(EMP_LAST_NAME) MAX(EMP_LAST_NAME) ------------------------- ----------------------- Amin Zhu

20 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-20Example More often the MIN and MAX functions are used to manipulate numeric data columns. More often the MIN and MAX functions are used to manipulate numeric data columns. The query shown here will use the MAX function to display the highest salary paid to an employee in the organization The query shown here will use the MAX function to display the highest salary paid to an employee in the organization SELECT MAX(emp_salary) "Highest Salary" FROM employee; Highest Salary -------------- $55,000 $55,000

21 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-21 Using GROUP BY with Aggregate Functions The power of aggregate functions is greater when combined with the GROUP BY clause.The power of aggregate functions is greater when combined with the GROUP BY clause. In fact, the GROUP BY clause is rarely used without an aggregate function.In fact, the GROUP BY clause is rarely used without an aggregate function. It is possible to use the GROUP BY clause without aggregates, but such a construction has very limited functionality, and could lead to a result table that is confusing or misleading.It is possible to use the GROUP BY clause without aggregates, but such a construction has very limited functionality, and could lead to a result table that is confusing or misleading.

22 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-22Example The following query displays how many employees work for each department:The following query displays how many employees work for each department: SELECT emp_dpt_number "Department", COUNT(*) "Department Count" FROM employee GROUP BY emp_dpt_number; Department Department Count ---------- ---------------- 1 1 1 1 3 3 3 3 7 4 7 4

23 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-23 GROUP BY Clause Some RDBM provides considerable flexibility in specifying the GROUP BY clause.Some RDBM provides considerable flexibility in specifying the GROUP BY clause. The column name used in a GROUP BY does not have to be listed in the SELECT clause; however, it must be a column name from one of the tables listed in the FROM clause.The column name used in a GROUP BY does not have to be listed in the SELECT clause; however, it must be a column name from one of the tables listed in the FROM clause.

24 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-24Example We could rewrite the last query without specifying the emp_dpt_number column as part of the result table, but as you can see below, the results are rather cryptic without the emp_dpt_number column to identify the meaning of the aggregate count.We could rewrite the last query without specifying the emp_dpt_number column as part of the result table, but as you can see below, the results are rather cryptic without the emp_dpt_number column to identify the meaning of the aggregate count. SELECT COUNT(*) "Department Count" FROM employee GROUP BY emp_dpt_number; Department Count Department Count---------------- 1 3 4

25 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-25 Using GROUP BY with Expressions In addition to column names, any expression listed in a SELECT clause can be used with a GROUP BY clause.In addition to column names, any expression listed in a SELECT clause can be used with a GROUP BY clause. SELECT AVG(emp_salary) "Current Average Salary", AVG(emp_salary * 1.25) "New Average Salary" AVG(emp_salary * 1.25) "New Average Salary" FROM employee GROUP BY emp_salary * 1.25; Current Average Salary New Average Salary ---------------------- ------------------ $25,000 $31,250 $25,000 $31,250 $30,000 $37,500 $30,000 $37,500 $38,000 $47,500 $38,000 $47,500 $43,000 $53,750 $43,000 $53,750 $55,000 $68,750 $55,000 $68,750

26 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-26 Nesting Aggregate Functions: MAX(AVG( )) A result table to determine which department pays employees the highest average salary would be difficult.A result table to determine which department pays employees the highest average salary would be difficult. This can be accomplished by nesting the AVG(salary) inside the MAX() function and removing the emp_dpt_number column from the SELECT list.This can be accomplished by nesting the AVG(salary) inside the MAX() function and removing the emp_dpt_number column from the SELECT list. SELECT MAX(AVG(emp_salary)) "Largest Average Salary" FROM employee GROUP BY emp_dpt_number; Largest Average Salary ---------------------- $55,000 $55,000

27 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-27 GROUP BY and NULL Values When a query with a GROUP BY clause processes data columns that contain NULL values, all data rows containing a NULL value are placed into a distinct group.When a query with a GROUP BY clause processes data columns that contain NULL values, all data rows containing a NULL value are placed into a distinct group.

28 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-28 GROUP BY and NULL Values SELECT work_emp_ssn "SSN", SUM(work_hours) "Total Hours Worked" SUM(work_hours) "Total Hours Worked" FROM assignment GROUP BY work_emp_ssn; SSN Total Hours Worked --------- ------------------ 999111111 39.9 999222222 39.6 999333333 42.1 999444444 44.6 999555555 34 999666666 999887777 41 999888888 43

29 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-29 Using GROUP BY with a WHERE Clause The WHERE clause works to eliminate data table rows from consideration before any grouping takes place.The WHERE clause works to eliminate data table rows from consideration before any grouping takes place. The query shown here produces an average hours worked result table for employees with a social security number that is larger than 999-66-0000.The query shown here produces an average hours worked result table for employees with a social security number that is larger than 999-66-0000. SELECT work_emp_ssn SSN, AVG(work_hours) "Average Hours Worked" FROM assignment WHERE work_emp_ssn > 999660000 GROUP BY work_emp_ssn; SSN Average Hours Worked --------- -------------------- 999666666 999887777 20.5 999888888 21.5

30 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-30 Using GROUP BY with an ORDER BY Clause The ORDER BY clause allows you to specify how rows in a result table are sorted.The ORDER BY clause allows you to specify how rows in a result table are sorted. The default ordering is from smallest to largest value.The default ordering is from smallest to largest value. A GROUP BY clause in a SELECT statement will determine the sort order of rows in a result table.A GROUP BY clause in a SELECT statement will determine the sort order of rows in a result table. The sort order can be changed by specifying an ORDER BY clause after the GROUP BY clause.The sort order can be changed by specifying an ORDER BY clause after the GROUP BY clause.

31 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-31 Using GROUP BY with an ORDER BY Clause SELECT emp_dpt_number "Department", AVG(emp_salary) "Average Salary" FROM employee GROUP BY emp_dpt_number ORDER BY AVG(emp_salary); Department Average Salary ---------- -------------- 3 $31,000 3 $31,000 7 $34,000 7 $34,000 1 $55,000 1 $55,000

32 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-32 GROUP BY with a HAVING Clause The HAVING clause is used for aggregate functions in the same way that a WHERE clause is used for column names and expressions.The HAVING clause is used for aggregate functions in the same way that a WHERE clause is used for column names and expressions. The HAVING and WHERE clauses do the same thing, that is, filter rows from inclusion in a result table based on a condition.The HAVING and WHERE clauses do the same thing, that is, filter rows from inclusion in a result table based on a condition. A WHERE clause is used to filter rows BEFORE the GROUPING action.A WHERE clause is used to filter rows BEFORE the GROUPING action. A HAVING clause filters rows AFTER the GROUPING action.A HAVING clause filters rows AFTER the GROUPING action.

33 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-33 GROUP BY with a HAVING Clause SELECT emp_dpt_number "Department", AVG(emp_salary) "Average Salary" FROM employee GROUP BY emp_dpt_number HAVING AVG(emp_salary) > 33000; Department Average Salary ---------- -------------- 1 $55,000 1 $55,000 7 $34,000 7 $34,000

34 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-34 Combining HAVING Clause with WHERE Clause SELECT emp_dpt_number "Department", AVG(emp_salary) "Average Salary" FROM employee WHERE emp_dpt_number <> 1 GROUP BY emp_dpt_number HAVING AVG(emp_salary) > 33000; Department Average Salary ---------- -------------- 7 $34,000 7 $34,000

35 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-35 GROUP BY with a HAVING Clause Conceptually, SQL performs the following steps in the query given above. Conceptually, SQL performs the following steps in the query given above. 1. The WHERE clause filters rows that do not meet the condition emp_dpt_number <> 1. 2. The GROUP BY clause collects the surviving rows into one or more groups for each unique emp_dpt_number. 3. The aggregate function calculates the average salary for each emp_dpt_number grouping. 4. The HAVING clause filters out the rows from the result table that do not meet the condition average salary greater than $33,000.

36 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-36 Using GROUP BY with an ORDER BY Clause Standard SQL Rules The most common use of a HAVING clause is to create result tables containing one row per group, with one or more summary values in the row. To do this your query must meet the following conditions:

37 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-37 Using GROUP BY with an ORDER BY Clause Columns listed in a SELECT clause must also be listed in the GROUP BY expression or they must be arguments of aggregate functions.Columns listed in a SELECT clause must also be listed in the GROUP BY expression or they must be arguments of aggregate functions. A GROUP BY expression can only contain column names that are in the SELECT clause listing.A GROUP BY expression can only contain column names that are in the SELECT clause listing. Columns in a HAVING expression must be either:Columns in a HAVING expression must be either:  single-valued—arguments of an aggregate function, for instance, or  listed in the SELECT clause listing or GROUP BY clause.

38 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-38 Example SELECT emp_dpt_number "Department", COUNT(*) "Department Count" FROM employee GROUP BY emp_dpt_number HAVING COUNT(*) >= 3; Department Department Count ---------- ---------------- 3 3 3 3 7 4 7 4

39 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-39 More Examples SELECT emp_dpt_number "Department", COUNT(*) "Department Count", COUNT(*) "Department Count", MAX(emp_salary) "Top Salary", MAX(emp_salary) "Top Salary", MIN(emp_salary) "Low Salary" MIN(emp_salary) "Low Salary" FROM employee GROUP BY emp_dpt_number HAVING COUNT(*) >= 3; Department Department Count Top Salary Low Salary ---------- ---------------- ---------- ---------- 3 3 $43,000 $25,000 3 3 $43,000 $25,000 7 4 $43,000 $25,000 7 4 $43,000 $25,000

40 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-40 SQL Extensions to GROUP BY and HAVING Clauses Various software vendors, such as Oracle, provide extensions to the standard Structured Query Language.Various software vendors, such as Oracle, provide extensions to the standard Structured Query Language. A SELECT clause listing that includes aggregate functions can also include columns that are not arguments of aggregate functions, or that are not included in the GROUP BY clause.A SELECT clause listing that includes aggregate functions can also include columns that are not arguments of aggregate functions, or that are not included in the GROUP BY clause. The GROUP BY clause can include column names or expressions that are not included in the SELECT clause listing.The GROUP BY clause can include column names or expressions that are not included in the SELECT clause listing.

41 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-41 SQL Extensions to GROUP BY and HAVING Clauses The GROUP BY ALL clause displays all groups, even those excluded from calculations by a WHERE clause.The GROUP BY ALL clause displays all groups, even those excluded from calculations by a WHERE clause. The HAVING clause can include columns or expressions that are not listed in the SELECT clause or in a GROUP BY clause.The HAVING clause can include columns or expressions that are not listed in the SELECT clause or in a GROUP BY clause.

42 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-42 GROUP BY with a HAVING Clause The HAVING clause is a conditional option that is directly related to the GROUP BY clause option because a HAVING clause eliminates rows from a result table based on the result of a GROUP BY clause.The HAVING clause is a conditional option that is directly related to the GROUP BY clause option because a HAVING clause eliminates rows from a result table based on the result of a GROUP BY clause. In Oracle, a HAVING clause will not work without a GROUP BY clause.In Oracle, a HAVING clause will not work without a GROUP BY clause.

43 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-43 GROUP BY with a HAVING Clause SELECT emp_dpt_number, AVG(emp_salary) FROM employee HAVING AVG(emp_salary) > 33000; ERROR at line 1: ORA-00937: not a single-group group function

44 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-44 Oracle SQL Format SELECT [ALL | DISTINCT] select-list FROM table-reference-list table-reference-list [WHERE search-condition] search-condition [GROUP BY column-name [, column-name]... ] [HAVING search-condition] search-condition [[UNION | UNION ALL |INTERSECT | MINUS] select-statement]... select-statement [ORDER BY {unsigned integer | column-name} [ASC|DESC]]

45 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-45 Hands-on LOG on to your database using SQL*PLUS worksheetLOG on to your database using SQL*PLUS worksheet Page 107, SQL Coding Exercises and QuestionsPage 107, SQL Coding Exercises and Questions –Let’s do some of these

46 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-46 CHAPTER 6: JOINS

47 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-47 A TYPICAL JOIN OPERATION The following figure displays the employee and department tables.The following figure displays the employee and department tables. The figure illustrates the concept of a JOIN operation by connecting columns within each table with a line.The figure illustrates the concept of a JOIN operation by connecting columns within each table with a line. One line connects the employee table's emp_dpt_number column with the department table's dpt_no column.One line connects the employee table's emp_dpt_number column with the department table's dpt_no column. A second line connects the employee table's emp_ssn column to the department table's dep_mgrssn column.A second line connects the employee table's emp_ssn column to the department table's dep_mgrssn column.

48 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-48 A TYPICAL JOIN OPERATION

49 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-49 JOINS We will begin our study of JOIN operations by focusing on the relationship between the employee and department tables represented by the common department number values.We will begin our study of JOIN operations by focusing on the relationship between the employee and department tables represented by the common department number values. Our first query lists employee names and department numbers. This query only retrieves data from the single employee table.Our first query lists employee names and department numbers. This query only retrieves data from the single employee table.

50 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-50 JOINS SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_dpt_number "Department" emp_dpt_number "Department" FROM employee; Last Name First Name Department ------------- -------------- -------------- Bordoloi Bijoy 1 Joyner Suzanne 3 Zhu Waiman 7 more rows will be displayed...

51 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-51 JOINS A large organization can have dozens or even hundreds of departments. Thus, the numbers displayed in the department column shown above may not be very meaningful.A large organization can have dozens or even hundreds of departments. Thus, the numbers displayed in the department column shown above may not be very meaningful. Suppose you want the department names instead of the department numbers to be listed.Suppose you want the department names instead of the department numbers to be listed. The department names are mentioned in the “Department” table.The department names are mentioned in the “Department” table. Hence we need to join the “Employee” and the “Department” tables to get the required results.Hence we need to join the “Employee” and the “Department” tables to get the required results.

52 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-52 JOINS SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_first_name "First Name", dpt_name "Department Name" dpt_name "Department Name" FROM employee, department WHERE employee.emp_dpt_number = department.dpt_no; Last Name First Name Department Name ------------- ------------- ---------------------- Bordoloi Bijoy Headquarters Joyner Suzanne Admin and Records Zhu Waiman Production more rows will be displayed...

53 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-53 JOINS The following table shows two tables simply named Table_1 and Table_2.The following table shows two tables simply named Table_1 and Table_2. Each table has a single column named Col_1. Each table also has three rows with simple alphabetic values stored in the Col_1 column.Each table has a single column named Col_1. Each table also has three rows with simple alphabetic values stored in the Col_1 column. Table_1 Table_2 Table_1 Table_2 COL_1 COL_1 a b c a b c

54 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-54JOINS SELECT * FROM table_1, table_2; COL_1 COL_1 -------- --------- a a b a c a a b b b c b a c b c cc

55 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-55JOINS The first row of the “table_1” table was joined with every row in the “table_2” table.The first row of the “table_1” table was joined with every row in the “table_2” table. A Cartesian product may not be useful and could be misleading.A Cartesian product may not be useful and could be misleading. Always include a WHERE clause in your JOIN statements.Always include a WHERE clause in your JOIN statements. SELECT * FROM table_1, table_2; WHERE table_1.col_1 = table_2.col_1; col_1 col_1 col_1 col_1 -------- -------- -------- -------- a a a a b b b b c c c c

56 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-56 JOIN OPERATION RULES JOINS and the SELECT Clause A JOIN query always begins with a SELECT clause.A JOIN query always begins with a SELECT clause. List the columns to be displayed in the result table after the SELECT keyword.List the columns to be displayed in the result table after the SELECT keyword. The result table column order reflects the order in which column names are listed in the SELECT clause.The result table column order reflects the order in which column names are listed in the SELECT clause. To modify the order in which column names are listed, simply rearrange the order of the column listing in the SELECT clause.To modify the order in which column names are listed, simply rearrange the order of the column listing in the SELECT clause.

57 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-57Example SELECT dpt_name "Department Name", emp_last_name "Last Name", emp_last_name "Last Name", emp_first_name "First Name" emp_first_name "First Name" FROM employee e, department d WHERE e.emp_dpt_number = d.dpt_no AND e.emp_dpt_number = 7; Department Name Last Name First Name ---------------------- -------------- --------------- Production Zhu Waiman Production Bock Douglas Production Joshi Dinesh Production Prescott Sherri

58 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-58 JOIN OPERATION RULES JOIN operations also support the specification of all columns by the use of a simple asterisk (*) in a SELECT clause.JOIN operations also support the specification of all columns by the use of a simple asterisk (*) in a SELECT clause. The result table for the query will contain all columns from both of the tables.The result table for the query will contain all columns from both of the tables. When the asterisk (*) is used, the column order of the result table is based on the order in which tables are listed in the FROM clause.When the asterisk (*) is used, the column order of the result table is based on the order in which tables are listed in the FROM clause.

59 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-59 JOINS and the FROM Clause Any SELECT statement that has two or more table names listed in a FROM clause is a JOIN query.Any SELECT statement that has two or more table names listed in a FROM clause is a JOIN query. By definition, a JOIN operation retrieves rows from two or more tables.By definition, a JOIN operation retrieves rows from two or more tables. The FROM clause is always used to list the tables from which columns are to be retrieved by a JOIN query.The FROM clause is always used to list the tables from which columns are to be retrieved by a JOIN query. The FROM clause listing has a limit of 16 table names.The FROM clause listing has a limit of 16 table names. The order of table name listings is irrelevant to the production of the result table with one exception – that is, if you use an asterisk (*) in the SELECT clause, then the column order in the result table reflects the order in which tables are listed in the FROM clause.The order of table name listings is irrelevant to the production of the result table with one exception – that is, if you use an asterisk (*) in the SELECT clause, then the column order in the result table reflects the order in which tables are listed in the FROM clause.

60 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-60 JOINS and the WHERE Clause The WHERE clause specifies the relationship between tables listed in the FROM clause.The WHERE clause specifies the relationship between tables listed in the FROM clause. It also restricts the rows displayed in the result table.It also restricts the rows displayed in the result table. The most commonly used JOIN operator is the "equal" (=) sign.The most commonly used JOIN operator is the "equal" (=) sign.

61 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-61 QUALIFYING COLUMN NAMES When column names are ambiguous (the column names used are from more than one table) you must qualify them.When column names are ambiguous (the column names used are from more than one table) you must qualify them. SELECT * FROM table_1, table_2 WHERE col_1 = col_1; This query is WRONG. Oracle Server would reject this query and generate the error message "ambiguous object.“This query is WRONG. Oracle Server would reject this query and generate the error message "ambiguous object.“ Error at line 3: Error at line 3: ORA - 00918 : column ambiguously defined ORA - 00918 : column ambiguously defined

62 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-62 QUALIFYING COLUMN NAMES This error message tells you that you have included a column name somewhere in the query that exists in more than one table listed in the FROM clause.This error message tells you that you have included a column name somewhere in the query that exists in more than one table listed in the FROM clause. Here the error is in the WHERE clause; however, it is also possible to make a similar error in the SELECT clause.Here the error is in the WHERE clause; however, it is also possible to make a similar error in the SELECT clause. The SELECT statement shown below fails to qualify the col_1 name in the SELECT clause, and Oracle again produces the ORA-00918 error message.The SELECT statement shown below fails to qualify the col_1 name in the SELECT clause, and Oracle again produces the ORA-00918 error message. SELECT col_1 FROM table_1, table_2 WHERE table_1.col_1 = table_2.col_1; ERROR at line 1: ORA-00918: column ambiguously defined

63 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-63 QUALIFYING COLUMN NAMES An ambiguous column name is qualified by using the DOT (.) connector to connect the table name and column name.An ambiguous column name is qualified by using the DOT (.) connector to connect the table name and column name. Sometimes it is easier to qualify column names by using table alias names.Sometimes it is easier to qualify column names by using table alias names. Often, a single letter is used as an identifier to reduce keystroke requirements.Often, a single letter is used as an identifier to reduce keystroke requirements. SELECT dpt_name "Department Name", emp_last_name "Last Name", emp_last_name "Last Name", emp_first_name "First Name" emp_first_name "First Name" FROM employee e, department d WHERE e.emp_dpt_number = d.dpt_no AND e.emp_dpt_number = 7;

64 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-64 QUALIFYING COLUMN NAMES The use of the letters "e" and "d" is completely arbitrary; "t1" and "t2" or any other unique aliases could have been used.The use of the letters "e" and "d" is completely arbitrary; "t1" and "t2" or any other unique aliases could have been used. The important points to learn are:The important points to learn are: –The alias must follow a table name. –Use a space to separate a table name and its alias. –The alias must be unique within the SELECT statement. If the column names are not identical you are not required to qualify them, although you still might want to for documentation purposesIf the column names are not identical you are not required to qualify them, although you still might want to for documentation purposes

65 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-65 JOIN Operations Using Inequality Operators ( ) You may use any relational operator in a JOIN query. The next query uses an inequality operator, the greater than (>) relational operator.You may use any relational operator in a JOIN query. The next query uses an inequality operator, the greater than (>) relational operator. SELECT emp_first_name "First Name", dep_name "Dependent Name", dep_name "Dependent Name", dep_relationship "Relation" dep_relationship "Relation" FROM employee e, dependent d WHERE e.emp_ssn = d.dep_emp_ssn AND e.emp_first_name > d.dep_name; e.emp_first_name > d.dep_name; First Name Dependent Name Relation --------------- --------------------- ---------- Waiman Jo Ellen DAUGHTER Waiman Andrew SON Waiman Susan SPOUSE Suzanne Allen SPOUSE Douglas Deanna DAUGHTER

66 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-66 Joining More Than Two Tables While the examples given thus far have joined rows from two tables, you can specify up to 16 tables in a JOIN operation.While the examples given thus far have joined rows from two tables, you can specify up to 16 tables in a JOIN operation. The more tables that are included in a JOIN operation, the longer the query will take to process, especially when the tables are large with millions of rows per table.The more tables that are included in a JOIN operation, the longer the query will take to process, especially when the tables are large with millions of rows per table.

67 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-67 Joining More Than Two Tables The example shown in the following figure joins three tables to produce a result table based on two different relationships.The example shown in the following figure joins three tables to produce a result table based on two different relationships.

68 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-68 Joining More Than Two Tables The SELECT statement to join the tables depicted in the figure is shown here.The SELECT statement to join the tables depicted in the figure is shown here. SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_first_name "First Name", 1.10*emp_salary "Raised Salary", p.pro_name "Project" 1.10*emp_salary "Raised Salary", p.pro_name "Project" FROM employee e, assignment a, project p WHERE e.emp_ssn = a.work_emp_ssn AND a.work_pro_number = p.pro_number AND a.work_pro_number = p.pro_number AND p.pro_name = 'Inventory'; p.pro_name = 'Inventory'; Last Name First Name Raised Salary Project --------------- --------------- ---------------- ----------- Zhu Waiman $47,300 Inventory Markis Marcia $27,500 Inventory Amin Hyder $27,500 Inventory

69 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-69 Joining Tables by Using Two Columns The diagram depicts the relationship at a university where students enroll in course sections.The diagram depicts the relationship at a university where students enroll in course sections.

70 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-70 Joining Tables by Using Two Columns The SELECT statement that accomplishes the JOIN based on two columns is shown below.The SELECT statement that accomplishes the JOIN based on two columns is shown below. This situation arises when the related tables have composite primary key columns.This situation arises when the related tables have composite primary key columns. SELECT s.course_title "Course Title", e.student_ssn "Student SSN" e.student_ssn "Student SSN" FROM enrollment e, section s WHERE e.course_number = s.course_number AND e.section_number = s.section_number; e.section_number = s.section_number;

71 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-71 OUTER JOIN Operations Oracle also supports what is called an outer- join. This means that a row will appear in the joined table even though there is no matching value in the table to be joined.Oracle also supports what is called an outer- join. This means that a row will appear in the joined table even though there is no matching value in the table to be joined. Suppose you want to know the names of the employees regardless of whether they have dependents or not. You can use the outer join as follows.Suppose you want to know the names of the employees regardless of whether they have dependents or not. You can use the outer join as follows.

72 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-72 OUTER JOIN Operations The plus sign in parentheses (+) tells Oracle to execute an OUTER JOIN operation.The plus sign in parentheses (+) tells Oracle to execute an OUTER JOIN operation. Further, it is the dependent table that is being outer-joined to the employee table because some employees will not have dependents.Further, it is the dependent table that is being outer-joined to the employee table because some employees will not have dependents. SELECT emp_last_name "Last Name", emp_first_name "First Name", dep_name "Dependent", dep_name "Dependent", dep_relationship "Relationship" dep_relationship "Relationship" FROM employee e, dependent d WHERE e.emp_ssn = d.dep_emp_ssn(+);

73 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-73 SELECT emp_last_name "Last Name", emp_first_name "First Name", emp_first_name "First Name", dep_name "Dependent", dep_name "Dependent", dep_relationship "Relationship" dep_relationship "Relationship" FROM employee e, dependent d WHERE e.emp_ssn = d.dep_emp_ssn(+); Last Name First Name Dependent Relationship --------------- --------------- -------------- ------------ Bordoloi Bijoy Joyner Suzanne Allen SPOUSE Zhu Waiman Andrew SON Zhu Waiman Jo Ellen DAUGHTER Zhu Waiman Susan SPOUSE Markis Marcia Amin Hyder Bock Douglas Deanna DAUGHTER Bock Douglas Jeffery SON Bock Douglas Mary Ellen SPOUSE Joshi Dinesh Prescott Sherri 12 rows selected.

74 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-74 OUTER JOINS and NULL values Management might desire a listing of employees with no dependents in order to satisfy some governmental reporting requirement.Management might desire a listing of employees with no dependents in order to satisfy some governmental reporting requirement. We can take advantage of the fact that the dep_name column will be NULL for employees with no dependents, and simply add a criteria to the WHERE clause to include employees where the dep_name column is NULL.We can take advantage of the fact that the dep_name column will be NULL for employees with no dependents, and simply add a criteria to the WHERE clause to include employees where the dep_name column is NULL.

75 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-75 OUTER JOINS and NULL values SELECT emp_last_name "Last Name", emp_first_name "First Name" FROM employee e, dependent d WHERE e.emp_ssn = d.dep_emp_ssn(+) AND d.dep_name IS NULL; d.dep_name IS NULL; Last Name First Name --------------- --------------- Bordoloi Bijoy Markis Marcia Amin Hyder Joshi Dinesh Prescott Sherri

76 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-76 LEFT and RIGHT OUTER JOIN Operations If the employee table is on the left and it is outer-joined with the dependent table depicted on the right, then this is sometimes referred to as a RIGHT OUTER JOIN.If the employee table is on the left and it is outer-joined with the dependent table depicted on the right, then this is sometimes referred to as a RIGHT OUTER JOIN. Now if you think about it, the entity- relationship diagram could just as easily have been drawn with the dependent table on the left and the employee table on the right. The same JOIN operation would now be termed a LEFT OUTER JOIN.Now if you think about it, the entity- relationship diagram could just as easily have been drawn with the dependent table on the left and the employee table on the right. The same JOIN operation would now be termed a LEFT OUTER JOIN.

77 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-77 LEFT JOIN SELECT emp_last_name "Last Name", emp_first_name "First Name", dep_name "Dependent", dep_relationship "Relationship" dep_name "Dependent", dep_relationship "Relationship" FROM employee e, dependent d WHERE d.dep_emp_ssn(+) = e.emp_ssn; Last Name First Name Dependent Relationship --------------- --------------- -------------- ------------ Bordoloi Bijoy Joyner Suzanne Allen SPOUSE Zhu Waiman Andrew SON Zhu Waiman Jo Ellen DAUGHTER Zhu Waiman Susan SPOUSE Markis Marcia Amin Hyder Bock Douglas Deanna DAUGHTER Bock Douglas Jeffery SON Bock Douglas Mary Ellen SPOUSE Joshi Dinesh Prescott Sherri 12 rows selected.

78 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-78 SELF-JOIN Operations A SELF JOIN operation is used to produce a result table when the relationship of interest exists among rows that are stored within a single table.A SELF JOIN operation is used to produce a result table when the relationship of interest exists among rows that are stored within a single table.

79 Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-79 SELF-JOIN Operations SELECT e1.emp_last_name || ', ' || e1.emp_first_name "Supervisor", e2.emp_last_name || ', ' || e2.emp_first_name "Employee" e2.emp_last_name || ', ' || e2.emp_first_name "Employee" FROM employee e1, employee e2 WHERE e1.emp_ssn = e2.emp_superssn; Supervisor Employee ---------------------------- ------------------- Bordoloi, Bijoy Joyner, Suzanne Bordoloi, Bijoy Zhu, Waiman Joyner, Suzanne Markis, Marcia Joyner, Suzanne Amin, Hyder Zhu, Waiman Bock, Douglas Zhu, Waiman Joshi, Dinesh Zhu, Waiman Prescott, Sherri


Download ppt "Bordoloi and BockCopyright 2004 Prentice Hall, Inc.5-1 COS 346 Day 15."

Similar presentations


Ads by Google