Creating Groups of Data

Entry Level Technology Program

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

Satyam Learning Center

ORACLE

1 of 1

Entry Level Technology Program

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.

Satyam Learning Center

ORACLE

2 of 1

Using the GROUP BY Clause
Entry Level Technology Program

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 ;

Satyam Learning Center

ORACLE

3 of 1

SELECT FROM AVG(SAL) EMP GROUP BY DEPTNO .Using the GROUP BY Clause Entry Level Technology Program The GROUP BY column does not have to be in the SELECT list. Satyam Learning Center ORACLE 4 of 1 .

Satyam Learning Center ORACLE 5 of 1 . grouped by department.Entry Level Technology Program Grouping by More Than One Column EMPLOYEES … “Add up the salaries in the EMPLOYEES table for each job.

JOB. Satyam Learning Center ORACLE 6 of 1 .Entry Level Technology Program Using the GROUP BY Clause on Multiple Columns SELECT FROM DEPTNO. JOB . SUM(SAL) EMP GROUP BY DEPTNO.

Entry Level Technology Program 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 clause. SELECT DEPTNO. SELECT DEPTNO. COUNT(ENAME) FROM EMP. COUNT(ENAME) * ERROR at line 1: ORA-00937: not a single-group group function Satyam Learning Center ORACLE 7 of 1 .

EMP restrict groups.  You AVG(SAL) use the HAVING clause to DEPTNO. AVG(SAL) > 8000  You cannot use group functions in DEPTNO. the WHERE clause. Satyam Learning Center ORACLE 8 of 1 .Entry Level Technology Program Illegal Queries Using Group Functions SELECT FROM WHERE GROUP BY WHERE AVG(SAL) > 8000 * ERROR at line 3: ORA-00934: group function is not allowed here  You cannot use the WHERE clause to restrict groups.

000 Satyam Learning Center ORACLE 9 of 1 .Excluding Group Results Entry Level Technology Program EMPLOYEES … The when it is greater than maximum salary per department $10.

Rows are grouped.Entry Level Technology Program Excluding Group Results: The HAVING Clause Use the HAVING clause to restrict groups: 1. group_function table condition] group_by_expression] group_condition] column]. Groups matching the HAVING clause are displayed. SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY column. Satyam Learning Center ORACLE 10 of 1 . The group function is applied. 2. 3.

MAX(SAL) FROM EMP GROUP BY DEPTNO HAVING MAX(SAL)>10000 . Satyam Learning Center ORACLE 11 of 1 .Using the HAVING Clause Entry Level Technology Program SELECT DEPTNO.

Satyam Learning Center ORACLE 12 of 1 . SUM(SAL) PAYROLL EMP JOB NOT LIKE '%GE%' GROUP BY JOB HAVING SUM(SAL) > 13000 ORDER BY SUM(SAL).Using the HAVING Clause Entry Level Technology Program SELECT FROM WHERE JOB.

Nesting Group Functions Entry Level Technology Program Display the maximum average salary. Satyam Learning Center ORACLE 13 of 1 . SELECT FROM MAX(AVG(SAL)) EMP GROUP BY DEPTNO.

MIN.Summary Entry Level Technology Program In this lesson. MAX. Satyam Learning Center ORACLE 20 of 1 . AVG  Write queries that use the GROUP BY clause  Write queries that use the HAVING clause SELECT FROM [WHERE [GROUP BY [HAVING [ORDER BY column. you should have learned how to:  Use the group functions COUNT. group_function(column) table condition] group_by_expression] group_condition] column].

grand totals) DEPT10 4000 5000 3500 DEPT20 6000 3000 5000 10000 3000 DEPT30 6000 10000 7000 TOTAL 6000 13000 20000 10000 13500  JOB ANALYST CLERK MANAGER PRESIDENT SALESMAN Satyam Learning Center ORACLE 21 of 1 .Queries Entry Level Technology Program    1) In Each year how many employees were join 2) Display similar salaries 3) Display department wise. designation wise how many employees are there. department wise total salaries. ( Display the department number only one time) 4) Display the output in the following format ( Designation wise.

Entry Level Technology Program Subqueries Satyam Learning Center ORACLE 22 of 1 .

Entry Level Technology Program Using a Subquery to Solve a Problem Who has a salary greater than Abel’s? Main Query: ? Which employees have salaries greater than Abel’s salary? Subquery ? What is Abel’s salary? Satyam Learning Center ORACLE 23 of 1 .

Subquery Syntax Entry Level Technology Program SELECT FROM WHERE select_list table expr operator (SELECT FROM select_list table).  The subquery (inner query) executes once before the main query. Satyam Learning Center ORACLE 24 of 1 .  The result of the subquery is used by the main query (outer query).

Satyam Learning Center ORACLE 25 of 1 .Using a Subquery Entry Level Technology Program SELECT ENAME FROM EMP 11000 WHERE SAL > (SELECT SAL FROM EMP WHERE ENAME = ‘JONES').

Entry Level Technology Program Guidelines for Using Subqueries  Enclose subqueries in parentheses.  The ORDER BY clause in the subquery is not needed unless you are performing Top-N analysis. Satyam Learning Center ORACLE 26 of 1 .  Use single-row operators with single-row subqueries and use multiple-row operators with multiple-row subqueries.  Place subqueries on the right side of the comparison condition.

Types of Subqueries Entry Level Technology Program • Single-row subquery Main query returns Subquery ST_CLERK • Multiple-row subquery Main query returns Subquery ST_CLERK SA_MAN Satyam Learning Center ORACLE 27 of 1 .

Single-Row Subqueries Entry Level Technology Program  Return only one row  Use single-row comparison operators Operator = > Meaning Equal to Greater than >= < <= Greater than or equal to Less than Less than or equal to <> Satyam Learning Center Not equal to ORACLE 28 of 1 .

SAL FROM WHERE CLERK AND SAL > 1100 (SELECT SAL FROM EMP WHERE EMPNO = 7876). Satyam Learning Center ORACLE 29 of 1 .Entry Level Technology Program Executing Single-Row Subqueries EMP JOB = (SELECT JOB FROM EMP WHERE EMPNO = 7369) SELECT ENAME. JOB.

SAL FROM EMP 800 WHERE SAL = (SELECT MIN(SAL) FROM EMP). JOB. Satyam Learning Center ORACLE 30 of 1 .Entry Level Technology Program Using Group Functions in a Subquery SELECT ENAME.

Entry Level Technology Program The HAVING Clause with Subqueries  The Oracle server executes subqueries first. Satyam Learning Center ORACLE 31 of 1 . SELECT FROM DEPTNO.  The Oracle server returns results into the HAVING clause of the main query. MIN(SAL EMP GROUP BY DEPTNO 1300 HAVING MIN(SAL) > (SELECT MIN(SAL) FROM EMP WHERE DEPTNO = 30).

ERROR at line 4: ORA-01427: single-row subquery returns more than one row Satyam Learning Center ORACLE 32 of 1 . ENAME FROM EMP WHERE SAL = (SELECT MIN(SAL) FROM EMP GROUP BY DEPTNO).Entry Level Technology Program What is Wrong with this Statement? SELECT EMPNO.

no rows selected Satyam Learning Center ORACLE 33 of 1 . JOB FROM EMP WHERE JOB = (SELECT JOB FROM EMP WHERE ENAME = ‘BOND').Entry Level Technology Program Will this Statement Return Rows? SELECT ENAME.

Multiple-Row Subqueries Entry Level Technology Program Operator  Return more than one row  Use multiple-row comparison Meaning operators Equal to any member in the list IN Satyam Learning Center ORACLE 34 of 1 .

ENAME FROM EMP e WHERE e.MGR FROM EMP M).Null Values in a Subquery Entry Level Technology Program SELECT e.EMPNO NOT IN (SELECT m. no rows selected Satyam Learning Center ORACLE 35 of 1 .

you should have learned how to:  Identify when a subquery can help solve a question  Write subqueries when a query is based on unknown values SELECT FROM WHERE FROM select_list table expr operator (SELECT select_list table). Satyam Learning Center ORACLE 36 of 1 .Summary Entry Level Technology Program In this lesson.

 Query-II from exe-I document Satyam Learning Center ORACLE 37 of 1 .Exercise Entry Level Technology Program  Display the employees from other departments whose designation match with the designation of an employee working for department 20 and drawing least salary.