0% found this document useful (0 votes)
24 views31 pages

Creating Groups of Data: Employees

The document provides information on using subqueries in SQL. Key points covered include: - A subquery is used to solve a problem where the value needed is unknown, such as finding employees whose salary is greater than a particular employee's salary. - Subqueries are enclosed in parentheses and placed on the right side of a comparison operator. - Subqueries can return single rows or multiple rows, requiring different comparison operators. - The HAVING clause and group functions can be used with subqueries. - Null values in a subquery's results require special treatment.

Uploaded by

Rajesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views31 pages

Creating Groups of Data: Employees

The document provides information on using subqueries in SQL. Key points covered include: - A subquery is used to solve a problem where the value needed is unknown, such as finding employees whose salary is greater than a particular employee's salary. - Subqueries are enclosed in parentheses and placed on the right side of a comparison operator. - Subqueries can return single rows or multiple rows, requiring different comparison operators. - The HAVING clause and group functions can be used with subqueries. - Null values in a subquery's results require special treatment.

Uploaded by

Rajesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

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

Using the GROUP BY Clause


Entry Level Technology Program

The GROUP BY column does not have to be in the SELECT list.


SELECT FROM AVG(SAL) EMP

GROUP BY DEPTNO ;

Satyam Learning Center

ORACLE

4 of 1

Entry Level Technology Program

Grouping by More Than One Column

EMPLOYEES

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

Satyam Learning Center

ORACLE

5 of 1

Entry Level Technology Program

Using the GROUP BY Clause on Multiple Columns

SELECT FROM

DEPTNO, JOB, SUM(SAL) EMP

GROUP BY DEPTNO, JOB ;

Satyam Learning Center

ORACLE

6 of 1

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, COUNT(ENAME) FROM EMP;

SELECT DEPTNO, COUNT(ENAME) * ERROR at line 1: ORA-00937: not a single-group group function

Satyam Learning Center

ORACLE

7 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. You AVG(SAL) use the HAVING clause to DEPTNO, EMP restrict groups. AVG(SAL) > 8000 You cannot use group functions in DEPTNO; the WHERE clause.

Satyam Learning Center

ORACLE

8 of 1

Excluding Group Results


Entry Level Technology Program

EMPLOYEES

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

Satyam Learning Center

ORACLE

9 of 1

Entry Level Technology Program

Excluding Group Results: The HAVING Clause

Use the HAVING clause to restrict groups: 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];

Satyam Learning Center

ORACLE

10 of 1

Using the HAVING Clause


Entry Level Technology Program

SELECT

DEPTNO, 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 FROM WHERE

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

GROUP BY JOB HAVING SUM(SAL) > 13000

ORDER BY SUM(SAL);

Satyam Learning Center

ORACLE

12 of 1

Nesting Group Functions


Entry Level Technology Program

Display the maximum average salary.


SELECT FROM MAX(AVG(SAL)) EMP

GROUP BY DEPTNO;

Satyam Learning Center

ORACLE

13 of 1

Summary
Entry Level Technology Program

In this lesson, you should have learned how to: Use the group functions COUNT, MAX, MIN, 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, group_function(column) table condition] group_by_expression] group_condition] column];

Satyam Learning Center

ORACLE

20 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. ( Display the department number only one time) 4) Display the output in the following format ( Designation wise, department wise total salaries, 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

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 Abels?

Main Query:

Which employees have salaries greater than Abels salary?


Subquery

What is Abels 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. The result of the subquery is used by the main query (outer query).

Satyam Learning Center

ORACLE

24 of 1

Using a Subquery
Entry Level Technology Program

SELECT ENAME FROM EMP


11000

WHERE

SAL >
(SELECT SAL FROM EMP

WHERE

ENAME = JONES');

Satyam Learning Center

ORACLE

25 of 1

Entry Level Technology Program

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 single-row subqueries and use multiple-row operators with multiple-row subqueries.

Satyam Learning Center

ORACLE

26 of 1

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

Entry Level Technology Program

Executing Single-Row Subqueries


EMP JOB = (SELECT JOB FROM EMP WHERE EMPNO = 7369)

SELECT ENAME, JOB, 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

Using Group Functions in a Subquery

SELECT ENAME, JOB, SAL FROM EMP


800

WHERE SAL = (SELECT MIN(SAL) FROM EMP);

Satyam Learning Center

ORACLE

30 of 1

Entry Level Technology Program

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 FROM DEPTNO, MIN(SAL EMP

GROUP BY DEPTNO
1300

HAVING

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

Satyam Learning Center

ORACLE

31 of 1

Entry Level Technology Program

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

Satyam Learning Center

ORACLE

32 of 1

Entry Level Technology Program

Will this Statement Return Rows?

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

no rows selected

Satyam Learning Center

ORACLE

33 of 1

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

Null Values in a Subquery


Entry Level Technology Program

SELECT e.ENAME FROM EMP e

WHERE

e.EMPNO NOT IN
(SELECT m.MGR FROM EMP M);

no rows selected

Satyam Learning Center

ORACLE

35 of 1

Summary
Entry Level Technology Program

In this lesson, 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

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.

Query-II from exe-I document

Satyam Learning Center

ORACLE

37 of 1

You might also like