Вы находитесь на странице: 1из 26

EMPLOYEES

Creating Groups of Data


4400 9500 The 3500 average salary in 6400 EMPLOYEES table for each 10033 department.

Creating Groups of Data: The GROUP BY Clause Syntax


SELECT FROM [WHERE [GROUP BY [ORDER BY column, group_function(column) table condition] group_by_expression] column];

Divide rows in a table into smaller groups by using the GROUP BY clause.

Using the GROUP BY Clause


All columns in the SELECT list that are not in group functions must be in the GROUP BY clause.
SELECT FROM DEPTNO, AVG(SAL) EMP

GROUP BY DEPTNO ;

Using the GROUP BY Clause


The GROUP BY column does not have to be in the SELECT list.
SELECT FROM AVG(SAL) EMP

GROUP BY DEPTNO ;

EMPLOYEES

Grouping by More Than One Column

Addupthe salaries in the EMPLOYEES table for each job, grouped by department.

Using the GROUP BY Clause on Multiple Columns


SELECT DEPTNO, JOB, SUM(SAL)

FROM

EMP

GROUP BY DEPTNO, JOB ;

Illegal Queries Using Group Functions


Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY COUNT(ENAME) SELECT DEPTNO, FROM EMP; clause.
SELECT DEPTNO, COUNT(ENAME) * ERROR at line 1: ORA-00937: not a single-group group function

Column missing in the GROUP BY clause

Illegal Queries Using Group Functions


You cannot use the WHERE clause to restrict groups. SELECT DEPTNO, AVG(SAL) You use the HAVING clause to FROM EMP WHERE AVG(SAL) > 8000 restrict groups. GROUP BY DEPTNO; You cannot use group WHERE AVG(SAL) > 8000 * functions in the WHERE ERROR at line 3: ORA-00934: group function is not allowed here clause.

EMPLOYEES

Excluding Group Results

The when it is greater than maximum salary per department $10,000

1.Rows are grouped. 2.The group function is applied. 3.Groups matching the HAVING clause are displayed.
SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY column, group_function table condition] group_by_expression] group_condition] column];

Excluding Group Results: The HAVING Clause

Using the HAVING Clause


SELECT FROM DEPTNO, MAX(SAL) EMP

GROUP BY DEPTNO
HAVING MAX(SAL)>10000 ;

Using the HAVING Clause


SELECT FROM WHERE JOB, SUM(SAL) PAYROLL EMP JOB NOT LIKE '%GE%'

GROUP BY JOB HAVING SUM(SAL) > 13000

ORDER BY SUM(SAL);

Nesting Group Functions


Display the maximum average salary.
SELECT FROM MAX(AVG(SAL)) EMP

GROUP BY DEPTNO;

Queries

1) In Each year how many employees were join


2) Display similar salaries 3) Display department wise, designation wise how many employees are there. ( Display the department number only one time) 4) Display the output in the following format ( Designation wise, department wise total salaries, grand totals) DEPT10 DEPT20 DEPT30

JOB TOTAL ANALYST 6000 CLERK 13000 MANAGER 20000 PRESIDENT 10000 SALESMAN 13500

6000
4000 5000 3000 5000 6000 10000

10000
3500 3000 7000

Subqueries

Subquery Syntax
SELECT FROM WHERE select_list table expr operator (SELECT FROM

select_list table);

The subquery (inner query) executes once before the main query. The result of the subquery is used by the main query (outer query).

Using a Subquery

SELECT ENAME FROM EMP

WHERE

SAL >

11000

(SELECT SAL FROM WHERE EMP ENAME = JONES');

Guidelines for Using Subqueries


Enclose subqueries in parentheses. Place subqueries on the right side of the comparison condition. The ORDER BY clause in the subquery is not needed unless you are performing Top-N analysis. Use single-row operators with singlerow subqueries and use multiple-row operators with multiple-row subqueries.

Single-Row Subqueries

Return only one row Use single-row comparison Operator Meaning operators
= > >= < <= <> Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to

Executing Single-Row Subqueries


SELECT ENAME, JOB, SAL
FROM WHERE EMP JOB = (SELECT JOB FROM EMP WHERE EMPNO = 7369) AND SAL >
1100 CLERK

(SELECT SAL FROM EMP WHERE EMPNO = 7876);

Using Group Functions in a Subquery


SELECT ENAME, JOB, SAL

FROM

EMP

800

WHERE SAL =

(SELECT MIN(SAL) FROM EMP);

The HAVING Clause with Subqueries


The Oracle server executes subqueries first. The Oracle server returns results into the HAVING clause of the main query. SELECT DEPTNO, MIN(SAL

FROM

EMP
1300

GROUP BY DEPTNO HAVING MIN(SAL) > (SELECT MIN(SAL) FROM EMP WHERE DEPTNO = 30);

What is Wrong with this Statement?


SELECT EMPNO, ENAME FROM EMP WHERE SAL = (SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO);

ERROR at line 4: ORA-01427: single-row subquery returns more than one row

Single-row operator with multiple-row subquery

Will this Statement Return Rows?


SELECT ENAME, JOB FROM EMP WHERE JOB = (SELECT JOB FROM EMP WHERE ENAME = BOND');

no rows selected

Multiple-Row Subqueries

Operator
IN

Return more than one row Use multiple-row Meaning comparison operators Equal to any member in the list

Null Values in a Subquery


SELECT e.ENAME FROM WHERE EMP e e.EMPNO NOT IN (SELECT m.MGR FROM EMP M); no rows selected

Вам также может понравиться