Upload
damon-dixon
View
221
Download
0
Embed Size (px)
Citation preview
Database Programming
Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL
Cross Join
SELECT last_name, department_nameFROM employees CROSS JOIN departments;
SELECT last_name, department_nameFROM employees, departments;
Marge Hohly 2
Natural Join SELECT e.employee_id, e.last_name,
e.department_id, d.department_nameFROM employees e NATURAL JOIN departments d
SELECT e.employee_id, e.last_name, e.department_id, d.department_nameFROM employees e, departments dWHERE e.department_id = d.department_id;
Marge Hohly 3
Join…On and Join…Using Joins .. 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.
Marge Hohly 4
Join .. Using
SELECT e.employee_id, e.last_name, e.department_id, d.department_nameFROM employees e JOIN departments dUSING (department_id );
Compare with previous and next slide
Marge Hohly 5
Join .. On SELECT e.employee_id, e.last_name,
e.department_id, d.location_idFROM employees e JOIN departments dON (e.department_id = d.department_id );
Marge Hohly 6
Join .. On
SELECT e.employee_id, e.last_name,e.salary,j.grade_idFROM employees e JOIN job_grades jON (e.salary BETWEEN j.lowest_sal AND j.highest_sal);
Marge Hohly 7
Join.. On SELECT e.employee_id,
e.last_name,e.salary, j.grade_levelFROM employees e,job_grades jWHERE e.salary >= j.lowest_salAND e.salary <= j.highest_sal;
SELECT e.employee_id, e.last_name,e.salary, j.grade_levelFROM employees e,job_grades jWHERE e.salary BETWEEN j.lowest_salAND j.highest_sal;
Marge Hohly 8
Self-Joins (Join.. ON) SELECT e.last_name emp, m.last_name mgr
FROM employees e JOIN employees mON(e.manager_id = m.employee_id);
Joining the table to itself Not common join
Marge Hohly 9
Outer Joins (Right Outer Join, Left Outer Join)
SELECT e.employee_id, e.last_name, e.department_id, d.department_nameFROM employees e RIGHT OUTER departments dON( e.department_id = d.department_id);
Marge Hohly 10
Outer Joins (Right Outer Join, Left Outer Join)
SELECT e.employee_id, e.last_name, e.department_id, d.department_nameFROM employees e LEFT OUTER departments dON( e.department_id = d.department_id);
Marge Hohly 11
Full Outer Join
SELECT e.employee_id, e.last_name, e.department_id, d.department_nameFROM employees e FULL OUTER departments dON( e.department_id = d.department_id);
Marge Hohly 12
Group Functions Group functions operate on sets of
rows to give one result per group AVG COUNT MAX MIN SUM STDDEV VARIANCE
Marge Hohly 13
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
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
Marge Hohly 15
Marge Hohly 16
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 employeesWHERE 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 employeesWHERE job_id LIKE ‘%REP%’;
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 employeesWHERE 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_cdsWHERE year < 2001;
SELECT COUNT(DISTINCT year) FROM d_cdsWHERE year < 2001;
Marge Hohly 22
COUNT function cont’d
COUNT(*) returns the number of rows in the table
SELECT COUNT(*)FROM employees;
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;
Marge Hohly 24
VARIANCE function
A statistical function that returns the variance ignoring null values for expressions NUMBER type
SELECT VARIANCE(salary)FROM employees;
Marge Hohly 25
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;
Marge Hohly 26
NVL function This is used to replace a NULL with a given value The value must be of the same datatype as the
colunmNO!SELECT commission_pct, NVL(commission_pct, ‘not eligible’)FROM employees;YES!SELECT commission_pct, NVL(commission_pct, 0)FROM employees;
Marge Hohly 27
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?
Marge Hohly 28