Download presentation
Presentation is loading. Please wait.
Published byΝικόμαχος Μανιάκης Modified over 6 years ago
1
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
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
Marge Hohly
18
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
19
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
20
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
21
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
22
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
23
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
24
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
25
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
26
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
27
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
28
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)) (this one averaged all values and where there was no value a 0 was put in.) Marge Hohly
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.