Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some.

Similar presentations


Presentation on theme: "SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some."— Presentation transcript:

1 SQL-5 (Group By.. Having)

2 Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some attribute values. For example, we may want to find the average salary of employees in each department or the number of employees who work on each project.  In these cases, we need to partition the relation into nonoverlapping subsets (or groups) of tuples.  Each group (partition) will consist of the tuples that have the same value of some attribute(s), called the grouping attribute(s).

3 Group By.. Having Act on record sets rather than on individual records based on grouping attribute(s). Group By Clause:  Another section of select statement.  This optional clause tells oracle to group rows based on distinct values that exist for specified columns.  The Group By clause creates a data set, containing several sets of records grouped together based on a condition.

4 Syntax Select,, ………, Aggregate_function ( ) From table_name where group by,, ;

5 Group By.. Having  The GROUP BY clause specifies the grouping attributes, which should also appear in the SELECT clause, so that the value resulting from applying each aggregate function to a group of tuples appears along with the value of the grouping attribute(s).  If NULLs exist in the grouping attribute, then a separate group is created for all tuples with a NULL value in the grouping attribute. Query. For each department, retrieve the department number, the number of employees in the department, and their average salary. Q: SELECT Dno, COUNT (*), AVG (Salary) FROM EMPLOYEE GROUP BY Dno;

6

7 Questions Find how many employees are there in each branch? Sol :--- Select branch_no “Branch_no”, count (Emp_no) “No. of employees” from employee group by branch_no; Output: Branch_no No. of employees B14 B25

8 Questions If the objective is to find out the maximum salary of each department then the query will be SELECT deptno, MAX (sal) FROM emp GROUP BY deptno; DEPTNO MAX(SAL) ---------------------- 10 8000 20 6000 2 rows selected.

9 Questions For each project, retrieve the project number, the project name, and the number of employees who work on that project. Given the schemas as: PROJECT(Pnumber); WORKS_ON(Pno, Pname) SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON WHERE Pnumber=Pno GROUP BY Pnumber, Pname;

10 Having Having clause can be used in conjunction with group by clause. Imposes condition on Group By clause which further filters the group created by the Group By clause. HAVING clause is very much similar to WHERE clause except that having clause is applicable only for grouped data. Here, the rows are further restricted according to the having clause.

11 HAVING Each column specification specified in the having clause must occur within a statistical function or must occur in the list of columns named in the group by clause. e.g. If we want to find out the maximum salary for all departments having more than three employees, then the query will be-

12 Example SELECT deptno, MAX (sal) FROM emp GROUP BY deptno HAVING COUNT(*) >3; DEPTNO MAX(SAL) ---------- ------------ 10 8000 11 11000 rows selected.

13 Example For each project on which more than two employees work, retrieve the project number, the project name, and the number of employees who work on the project. PROJECT(Pnumber, Proj_complet_date) WORKS_ON (Pno, Pname) SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON WHERE Pnumber=Pno GROUP BY Pnumber, Pname HAVING COUNT (*) > 2;

14 Example For each project, retrieve the project number, the project name, and the number of employees from department 5 who work on the project. SELECT Pnumber, Pname, COUNT (*) FROM PROJECT, WORKS_ON, EMPLOYEE WHERE Pnumber=Pno AND Ssn=Essn AND Dno=5 GROUP BY Pnumber, Pname;

15 Example We can also use the order by clause with group by & HAVING clauses to arrange the records in any specific order. SELECT deptno, MAX (sal) FROM emp GROUP BY deptno HAVING COUNT (*) >2 ORDER BY MAX(sal); DEPTNO MAX(SAL) ---------- ------------ 20 6000 10 8000 2 rows selected.

16 Example Consider Relation BOOK(BOOK_ID,title, price), Compute total price of books belonging to each combination of BOOK_ID and title comprising the character ‘n’ in the title. select title, BOOK_ID, sum(price) from book group by title, BOOK_ID having title like '%n%';

17 Example (Cont.)

18 Example Not Recommended: select sum(price) from book group by title, BOOK_ID having title like '%n%';

19 Questions Ques: Which of the following STATEMENT is true? (A)select title, BOOK_ID, sum(price) from book group by title, BOOK_ID having sum(price)>555; (B) select title, BOOK_ID, sum(price) from book group by title, BOOK_ID having price > 555; Ans: (A)


Download ppt "SQL-5 (Group By.. Having). Group By  Need: To apply the aggregate functions to subgroups of tuples in a relation, where the subgroups are based on some."

Similar presentations


Ads by Google