group functions cannot be used in the where clause: select type_code from d_songs

79

Upload: armina

Post on 10-Jan-2016

21 views

Category:

Documents


0 download

DESCRIPTION

Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs WHERE SUM (duration) = 100; (this will give an error) ---------------------------------------------------------------------------------------- - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 2: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 3: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 4: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

Group functions cannot be used in the WHERE clause:SELECT type_codeFROM d_songsWHERE SUM (duration) = 100; (this will give an error) ----------------------------------------------------------------------------------------Group functions ignore NULL values. In the example below, the (null) values were not used to find the average overtime rate.

Page 5: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

You can have more than one group function in the SELECT clause, on the same or different columns. You can also restrict the group function to a subset of the table using a WHERE clause.

SELECT MAX(salary), MIN(salary), MIN(employee_id)FROM employeesWHERE department_id = 60;

Page 6: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 7: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 8: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 9: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 10: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 11: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 12: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 13: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 14: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 15: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 16: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 17: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 18: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 19: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 20: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 21: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

19 rows exists in employees table from which only 7 are distinct (not repeated)

Page 22: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 23: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 24: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

Number of rows in employees table 20 rows while the group function returns 4 rows that is not null values

Page 25: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 26: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 27: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 28: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

GROUP BY GuidelinesImportant guidelines to remember when using a GROUP BY clause are:

•If you include a group function (AVG, SUM, COUNT, MAX, MIN, STDDEV, VARIANCE) in a SELECT clause and any other individual columns, each individual column must also appear in the GROUP BY clause. •You cannot use a column alias in the GROUP BY clause.

•The WHERE clause excludes rows before they are divided into groups.

Page 29: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

All column in select clause must be included in group by clause

Page 30: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

NESTING GROUP FUNCTIONSGroup functions can be nested to a depth of twotwo when GROUP BY is used. SELECT max(avg(salary)) FROM employees GROUP by department_id;How many values will be returned by this query? The answer is one – the query will find the average salary for eachdepartment, and then from that list, select the single largest value.

Page 31: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 32: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 33: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 34: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 35: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 36: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 37: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

ROLLUPIn GROUP BY queries you are quite often required to produce subtotals and totals, and the ROLLUP operation can do that for you. The action of ROLLUP is straightforward: it creates subtotals that roll up from the most detailed level to a grand total, following a grouping list specified in the ROLLUP clause. ROLLUP takes as its argument an ordered list of grouping columns. First, it calculates the standard aggregate values specified in the GROUP BY clause. Then, it creates progressively higher-level subtotals, moving from right to left through the list of grouping columns. Finally, it creates a grand total.

Page 38: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

Rollup for department 10

Rollup for department 20

Rollup for department 50

Rollup for department 60

Rollup for department 80

Rollup for total departments

in query

Rollup for department 90

Page 39: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

CUBE

CUBE is an extension to the GROUP BY clause like ROLLUP. It produces cross-tabulation reports.It can be applied to all aggregate functions including AVG, SUM, MIN, MAX and COUNT.CUBEIn the following statement the rows in red are generated by the CUBE operation:SELECT department_id, job_id, SUM(salary)FROM employeesWHERE department_id < 50GROUP BY CUBECUBE (department_id, job_id)

Page 40: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 41: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 42: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

GROUPING SETSThe point of GROUPING SETS is that if you want to see data from the EMPLOYEES table grouped by (department_id, job_id , manager_id), but also by (department_id, manager_id) and also by (job_id, manager_id) then you would normally have to write 3 different select statements with the only difference being the GROUP BY clauses. For the database this means retrieving the same data in this case 3 times, and that can be quite a big overhead. Imagine if your company had 3,000,000 employees. Then you are asking the database to retrieve 9 million rows instead of just 3 million rows, quite a big difference.So GROUPING SETS are much more efficient when writing complex reports.

Page 43: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

GROUPING SETSIn the following statement the rows in red are generated by the GROUPING SETS operation:SELECT department_id, job_id, manager_id, SUM(salary)FROM employeesWHERE department_id < 50GROUP BY GROUPING SETS((job_id, manager_idjob_id, manager_id), (department_id, job_iddepartment_id, job_id), (department_id, manager_iddepartment_id, manager_id))

Page 44: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 45: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 46: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

Subquary can retrieve data from more than one table

Page 47: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 48: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 49: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 50: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 51: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 52: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 53: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 54: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 55: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 56: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 57: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

IN Compares if value is in the list of all values returned by the subquery. 

Page 58: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 59: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 60: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 61: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 62: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 63: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 64: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 65: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

MULTIPLE-COLUMN SUBQUERIES

Subqueries can use one or more columns.A multiple-column subquery can be either pair-wise comparisons or non-pair-wise comparisons.

SELECT employee_id,manager_id,department_idFROM employeesWHERE (manager_id,department_id) IN (SELECT manager_id,department_idFROM employeesWHERE employee_id IN (149,174))AND employee_id NOT IN (149,174)

The query is listing the employeeswhose manager and departments are the same as the manager anddepartment of employees 149 or 174.

pair-wise comparisons

Page 66: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

non-pair-wise multiple-column subquery

A non-pair-wise multiple-column subquery also uses more than one column in the subquery, but it compares them one at a time, so the comparisons take place in different subqueries. You will need to write one subquery per column you want to compare against when performing non-pair-wise multiple column subqueries.

SELECT employee_id,manager_id,department_idFROM employeesWHERE manager_id IN(SELECT manager_idFROM employeesWHERE employee_id IN(174,199))AND department_id IN(SELECT department_idFROM employeesWHERE employee_id IN(174,199))AND employee_id NOT IN(174,199);

Page 67: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

Correlated SubqueriesThe Oracle server performs a correlated subquery when the subquery references a column from a table referred to in the parent statement.A correlated subquery is evaluated once for each row processed by the parent statement.The parent statement can be a SELECT, UPDATE or DELETE statement.

SELECT o.first_name,o.last_name, o.salaryFROM employees oWHERE o.salary >(SELECT AVG(i.salary)FROM employees iWHERE i.department_id =o.department_id);

A correlated subquery, however, executes once for each candidate row considered by the outer query. In other words, the inner query is driven by the outer query. The part that makes thisexample a correlated subquery is marked in red.

Page 68: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs
Page 69: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

WITH clauseThe syntax for the WITH clause is as follows:------------------------------------------------------------WITH subquery-name AS (subquery),subquery-name AS (subquery)SELECT column-listFROM {table | subquery-name | view}WHERE condition is true;

Correlated subquery and WITH clause

•The WITH clause retrieves the results of one or more query blocks and stores those results for the user who runs the query.•The WITH clause improves performance. •The WITH clause makes the query easier to read.•The with clause is used to write a very complex query with joins and aggregations used many times

Page 70: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

WITHdept_costs AS (SELECT d.department_name, SUM(e.salary) AS dept_totalFROM employees e JOIN departments d ON e.department_id = d.department_idGROUP BY d.department_name),avg_cost AS (SELECT SUM(dept_total)/COUNT(*) AS dept_avgFROM dept_costs)SELECT *FROM dept_costsWHERE dept_total >(SELECT dept_avgFROM avg_cost)ORDER BY department_name;

Display the department name and total salaries for those departments whose total salary is greater than the average salary across departments.

Page 71: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

SET OperatorsSet operators are used to combine the results

from different SELECT statements into one single result output.

SET operators can return the rows found in both statements, the rows that are in one table and not the other, or the rows common to both statements.

Page 72: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

Guidelines for set operatorsGuidelines for set operators

•The number of columns and the data types of the columns being selected by the SELECT statements in the queries, must be identical in all the SELECT statements used in the query.

•The names of the columns need not be identical.

•Column names in the output are taken from the column names in the first SELECT statement. So any column aliases should be entered in the first statement as you would want to see them in the finished report.

Page 73: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

the following two lists will be used throughout this lesson:A = {1, 2, 3, 4, 5}B = {4, 5, 6, 7, 8}Or in reality: two tables, one called A and one called B.

UNION

The UNION operator returns all rows from both tables, after eliminating duplicates.

SELECT a_idFROM aUNIONSELECT b_idFROM b;

The result of listing all elements in A and B

eliminating duplicates is {1, 2, 3, 4, 54, 5, 6, 7, 8}.If you joined A and B you would get only {4, 5}.You would have to perform a full outer join to getthe same list as above.(similar to full outer join)

Page 74: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

UNION ALLThe UNION ALL operator returns all rows from bothtables, without eliminating duplicates.

SELECT a_idFROM aUNION ALLSELECT b_idFROM b;The result of listing all elements in A and B without

eliminating duplicates is {1, 2, 3, 4, 5, 4, 54, 5, 4, 5, 6, 7, 8}.Observe the difference between

UNION and UNION ALL

Page 75: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

INTERSECTThe INTERSECT operator returns all rows

common to both tables.SELECT a_idFROM aINTERSECTSELECT b_idFROM b;

The result of listing all elements found in both Aand B is {4, 5}.

Page 76: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

MINUSThe MINUS operator returns all rows found in one

table but not the other.

SELECT a_id

FROM a

MINUS

SELECT b_id

FROM b;

The result of listing all elements found in

A but not B is {1, 2, 3},

and B MINUS A would give {6, 7, 8}.

Page 77: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

TO_CHAR(null) – matching the select listSELECT location_id, department_name "Department",

TO_CHAR(NULL) "Warehouse"FROM departmentsUNIONSELECT location_id, TO_CHAR(NULL) "Department",

warehouse_nameFROM warehouses;---------------------------------------------------------------------------------------

------------------This query will SELECT the location_id and the department_name from thedepartments table and it includes a reference to a NULL value as a “stand-in”for a column in the warehouses table. The column in the warehouses table isa varchar2 column, to a call to the TO_CHAR function is used to ensure thedatatypes of all columns in the two statements match. A similar reference hasbeen made to a column in the SELECT from warehouses clause. The resultcan be seen on the next slide.

Page 78: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

SELECT location_id, department_name "Department", TO_CHAR(NULL) "Warehouse"FROM departmentsUNIONSELECT location_id, TO_CHAR(NULL) "Department", warehouse_nameFROM warehouses;

Page 79: Group functions cannot be used in the WHERE clause: SELECT type_code FROM d_songs

GOOD LUCK

SEE YOU NEXT MEETING

Raafat [email protected]

[email protected]