alt
Advertisement
Online Training
Career Series
Exforsys
Exforsys arrow Tutorials arrow Oracle 9i arrow Tutorial 4: SQL functions
Site Search


Tutorial 4: SQL functions
Article Index
Tutorial 4: SQL functions
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7

GROUP BY CLAUSE :   Using a group function one can  group  entire table data and provide a single result,however
you can use the GROUP BY clause to break the result set into logical groupings and get a result for each group.

 

Q. To display no of employees in each dept we can use.

SQL >SELECT DEPTNO,COUNT(*) FROM EMP   GROUP BY DEPTNO;

    DEPTNO   COUNT(*)
---------- ----------
        10          3
        20          5
        30          6

Q. The total salary paid for each department and also when the last recruitment was made in that department.

SQL >SELECT DEPTNO,MAX(HIREDATE),SUM(SAL) FROM EMP    GROUP BY DEPTNO;

    DEPTNO MAX(HIRED   SUM(SAL)
---------- --------- ----------
        10 23-JAN-82       8750
        20 23-MAY-87      10875
        30 03-DEC-81       9400

When grouping using the GROUP BY clause you cannot use a WHERE clause to restrict the groups
that are returned, SQL provides another selection keyword, HAVING, to replace the WHERE clause

Q,To diplay those departments which have more than 5 employees in them we can write.
  SQL >SELECT DEPTNO, COUNT(*) FROM EMP
          GROUP BY DEPTNO
          HAVING COUNT(*) > 5 ;

    DEPTNO   COUNT(*)
---------- ----------
        30          6



 
< Prev   Next >
Exforsys Offers
© 2008 Exforsys.com
Joomla! is Free Software released under the GNU/GPL License.
Page copy protected against web site content infringement by Copyscape