Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL."— Presentation transcript:

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

2 Cross Join  SELECT last_name, department_name FROM employees CROSS JOIN departments;  SELECT last_name, department_name FROM employees, departments; Marge Hohly2

3 Natural Join  SELECT e.employee_id, e.last_name, e.department_id, d.department_name FROM employees e NATURAL JOIN departments d  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; Marge Hohly3

4 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 Hohly4

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 Hohly5

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 Hohly6

7 Join.. On  SELECT e.employee_id, e.last_name,e.salary,j.grade_id FROM employees e JOIN job_grades j ON (e.salary BETWEEN j.lowest_sal AND j.highest_sal); Marge Hohly7

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 ; Marge Hohly8

9 Self-Joins (Join.. ON)  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 Hohly9

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 departments d ON( e.department_id = d.department_id) ; Marge Hohly10

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 departments d ON( e.department_id = d.department_id) ; Marge Hohly11

12 Full Outer Join  SELECT e.employee_id, e.last_name, e.department_id, d.department_name FROM employees e FULL OUTER departments d ON( e.department_id = d.department_id); Marge Hohly12

13 Group Functions  Group functions operate on sets of rows to give one result per group AVG COUNT MAX MIN SUM STDDEV VARIANCE Marge Hohly13

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 Hohly14

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

16 Marge Hohly16

17 Marge Hohly17

18 Marge Hohly18

19 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 Hohly19

20 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%’; Marge Hohly20

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

22 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; Marge Hohly22

23 COUNT function cont’d  COUNT(*) returns the number of rows in the table SELECT COUNT(*) FROM employees; Marge Hohly23

24 STDDEV function  A statistical function that returns the standard deviation ignoring null values for expressions of NUMBER type SELECT STDDEV(salary) FROM employees; Marge Hohly24

25 VARIANCE function  A statistical function that returns the variance ignoring null values for expressions NUMBER type SELECT VARIANCE(salary) FROM employees; Marge Hohly25

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; Marge Hohly26

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; Marge Hohly27

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? Marge Hohly28


Download ppt "Database Programming Sections 4– Review of Joins, Group functions, COUNT, DISTINCT, NVL."

Similar presentations


Ads by Google