28
Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

Embed Size (px)

Citation preview

Page 1: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

Database Programming

Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

Page 2: 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

Page 3: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 4: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 5: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 6: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 7: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 8: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 9: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 10: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 11: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 12: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 13: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 14: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 15: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 16: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

Marge Hohly 16

Page 17: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

Marge Hohly 17

Page 18: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

Marge Hohly 18

Page 19: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 20: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 21: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 22: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 23: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

COUNT function cont’d

COUNT(*) returns the number of rows in the table

SELECT COUNT(*)FROM employees;

Marge Hohly 23

Page 24: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 25: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

VARIANCE function

A statistical function that returns the variance ignoring null values for expressions NUMBER type

SELECT VARIANCE(salary)FROM employees;

Marge Hohly 25

Page 26: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 27: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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

Page 28: Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL

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