Creating Groups of Data: Employees
Creating Groups of Data: Employees
EMPLOYEES
4400 9500 The 3500 average salary in 6400 EMPLOYEES table for each 10033 department.
ORACLE
1 of 1
Divide rows in a table into smaller groups by using the GROUP BY clause.
ORACLE
2 of 1
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 ;
ORACLE
3 of 1
GROUP BY DEPTNO ;
ORACLE
4 of 1
EMPLOYEES
Add up the salaries in the EMPLOYEES table for each job, grouped by department.
ORACLE
5 of 1
SELECT FROM
ORACLE
6 of 1
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
ORACLE
7 of 1
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.
ORACLE
8 of 1
EMPLOYEES
ORACLE
9 of 1
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];
ORACLE
10 of 1
SELECT
DEPTNO, MAX(SAL)
FROM
EMP
ORACLE
11 of 1
ORDER BY SUM(SAL);
ORACLE
12 of 1
GROUP BY DEPTNO;
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];
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
ORACLE
21 of 1
Subqueries
ORACLE
22 of 1
Main Query:
ORACLE
23 of 1
Subquery Syntax
Entry Level Technology Program
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).
ORACLE
24 of 1
Using a Subquery
Entry Level Technology Program
WHERE
SAL >
(SELECT SAL FROM EMP
WHERE
ENAME = JONES');
ORACLE
25 of 1
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.
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
ORACLE
27 of 1
Single-Row Subqueries
Entry Level Technology Program
>=
< <=
<>
Satyam Learning Center
Not equal to
ORACLE 28 of 1
AND
SAL >
1100
ORACLE
29 of 1
ORACLE
30 of 1
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);
ORACLE
31 of 1
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
ORACLE
32 of 1
SELECT ENAME, JOB FROM EMP WHERE JOB = (SELECT JOB FROM EMP WHERE ENAME = BOND');
no rows selected
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
ORACLE
34 of 1
WHERE
e.EMPNO NOT IN
(SELECT m.MGR FROM EMP M);
no rows selected
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);
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.
ORACLE
37 of 1