Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries.

Similar presentations


Presentation on theme: "Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries."— Presentation transcript:

1 Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries

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

3 Marge Hohly3 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%’;

4 Marge Hohly4 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%’;

5 Marge Hohly5 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 datatype 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%’;

6 Marge Hohly6 COUNT function  Returns the number of rows counted with non null values for the expression specified SELECT COUNT(commission_pct) FROM employees;

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

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

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

10 Marge Hohly10 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;

11 Marge Hohly11 Group functions & NULL values  Group functions ignore NULL values SELECT AVG(commission_pct) FROM employees;  The average commission_pct will only be calculated using those rows that have a commission_pct, null rows will be excluded.

12 Marge Hohly12 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;

13 Marge Hohly13 Using NVL with group functions  The NVL function is nested inside the group function When you want to include rows will 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?

14 Marge Hohly14 GROUP BY Clause  Use the Group By clause to divide the rows in a table into groups than apply the Group Functions to return summary information about that group  In the example below, the rows are being GROUPed BY department_id. The AVG(group functin) is then applied to each GROUP, or department_id.  SELECT department_id, AVG(salary) FROM employees GROUP BY department_id;

15 Marge Hohly15 Results of previous query

16 Marge Hohly16 GROUP BY Clause  With aggregate (group) functions in the SELECT clause, be sure to include individual columns from the SELECT clause in a GROUP BY clause!!!  SELECT department_id, job_id, SUM(salary) FROM employees WHERE hire_date <= ’01-JUN-00’ GROUP BY department_id, job_id;  SELECT d.department_id,d.department_name, MIN(e.hire_date) AS “Min Date” FROM departments d, employees e WHERE e.department_id=d.department_id GROUP BY ?????

17 Marge Hohly17 GROUP BY Rules...  Use the Group By clause to divide the rows in a table into groups then apply the Group Functions to return summary information about that group.  If the Group By clause is used, all individual columns in the SELECT clause must also appear in the GROUP BY clause.  Columns in the GROUP BY clause do not have to appear in the SELECT clause  No column aliases can be used in the Group By clause.  Use the ORDER BY clause to sort the results other than the default ASC order.  The WHERE clause, if used, can not have any group functions – use it to restrict any columns in the SELECT clause that are not group functions.  Use the HAVING clause to restrict groups not the WHERE clause.

18 Marge Hohly18 The HAVING Clause  With the HAVING clause the Oracle Server: 1.Groups rows. 2.Applies group function to the group(s). 3.Displays the groups that match the criteria in the HAVING clause. SELECT department_id, job_id, SUM(salary) FROM employees WHERE hire_date 50;

19 Marge Hohly19 The HAVING clause example SELECT department_id, job_id, SUM(salary) FROM employees WHERE hire_date 50;

20 Marge Hohly20 WHERE or HAVING ??  The WHERE clause is used to restrict rows. SELECT department_id, MAX(salary) FROM employees WHERE department_id>=20 GROUP BY department_id;  The HAVING clause is used to restrict groups returned by a GROUP BY clause. SELECT department_id, MAX(salary) FROM employees GROUP BY department_id HAVING MAX(salary)> 1000;

21 Marge Hohly21 SUBQUERY  SELECT statement that is embedded in a clause of another SELECT statement  Can be placed in the WHERE clause, the HAVING clause, and the FROM clause.  Executes first and its output is used to complete the query condition for the main or outer query.  SELECT last_name FROM employees WHERE salary > (SELECT salary FROM employees WHERE employee_id = 104);

22 Marge Hohly22 SUBQUERIES  Guidelines for using subqueries are: The subquery is enclosed in parentheses. The subquery is placed on the right side of the comparison condition. The outer and inner queries can get data from different tables. Only one ORDER BY clause can be used for a SELECT statement; and, if specified, it must be the last clause in the main SELECT statement. The only limit on the number of subqueries is the buffer size the query uses.

23 Marge Hohly23 SUBQUERIES  There are two types of subqueries: Single-row subqueries that use single- row operators (>,=.>=,,<=) and return only one row from the inner query. Multiple-row subqueries that use multiple-row operators (IN, ANY, ALL) and return more than one row from the inner query.

24 Marge Hohly24 SINGLE-ROW SUBQUERY  SELECT last_name FROM employees WHERE salary > (SELECT salary FROM employees WHERE last_name = ‘Abel’); Subquery returns 11000, thus the main SELECT statement returns all employees with a salary greater than 11000. What is there is more than one Abel in the company??

25 Marge Hohly25 Example  SELECT department_id, department_name FROM departments WHERE department_id = (SELECT department_id FROM employees WHERE salary < 4000);  What will result?

26 Marge Hohly26 SUBQUERIES FROM DIFFERENT TABLES  Subqueries are not limited to just one inner query. As the example illustrates, there can be more than one subquery returning information to the outer query. Also, the outer and inner queries can get data from different tables.  SELECT last_name,job_id,salary,department_id FROM employees WHERE job_id= (SELECT job_id FROM employees WHERE employee_id = 141) AND department_id = (SELECT department_id FROM departments WHERE location_id = 1500);  Subquery returns job_id = ST_CLERK and department_id = 50

27 Marge Hohly27 Results of previous query

28 Marge Hohly28 SUBQUERIES  Gropu functions can be used in single-row subqueries. The inner query returns a single row to the outer query. SELECT last_name,first_name,salary FROM f_staffs WHERE salary < (SELECT MAX(salary) FROM f_staffs);

29 Marge Hohly29 Example results  Subquery returns MAX(salary) = 60

30 Marge Hohly30 GROUP Functions in HAVING Clause  HAVING clause is used to restrict gropus and always has a group condition (such as MIN, MAX, AVG) stated  See next page for example

31 Marge Hohly31 GROUP Functions in HAVING Clause  SELECT department_id, MIN(salary) FROM employees GROUP BY department_id HAVING MIN(salary) > (SELECT MIN(salary) FROM employees WHERE department_id = 50);  Inner query returns 7500 for minimum salary


Download ppt "Database Programming Sections 5 & 6 – Group functions, COUNT, DISTINCT, NVL, GROUP BY, HAVING clauses, Subqueries."

Similar presentations


Ads by Google