You are on page 1of 29

DATABASE LAB

Lab3: Group Functions


and Subquery

Ruba Sultan
GROUP FUNCTIONS

 Group or Aggregate Functions:


SUM
AVG

MIN

MAX

COUNT

STDDEV

VARIANCE

2
QUESTIONS

 Question1
Write a query to display maximum and minimum
salaries for department 10 and 30.
 Question2
Write a query to display hire date for first hired
employee and hire date for the newest hired employee.

3
QUESTIONS

 Question3
Write a query to display total ,average, maximum and
minimum for the paid salaries.

 Question4
Write a query to display total ,average, maximum and
minimum for the paid commissions.

4
GROUP BY

SELECT column,….,aggregate func.


FROM table name
WHERE condition(s)
GROUP BY column1,..,column m
ORDER BY column1,..,column m

 Question5
Write a query to display the total paid salaries for
each department.

5
HAVING
 WHERE clause can only be used with non-aggregate
functions, it couldn’t be used with aggregate functions.
 HAVING clause used when there is a condition on
aggregate values.
SELECT column,….,aggregate func.
FROM table name
WHERE condition(s)
GROUP BY column1,..,column m
HAVING condition(s)
ORDER BY column1,..,column m
6
QUESTIONS

 Question6
Write a query to display the total paid salaries for
each department, exclude any departments that their
total salary less than $10000.
 Question7
State whether the following query valid or not
SELECT deptno,job,SUM(sal)
FROM emp
GROUP BY deptno;

7
QUESTIONS
 Question8
State whether the following query valid or not
SELECT deptno,SUM(sal)
FROM emp
GROUP BY deptno,job;

8
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?
SUBQUERY SYNTAX

SELECT column,….,aggregate func.


FROM table name
WHERE column comparison oper. Main or Outer Query
(SELECT column
FROM table name);

Subquery or Inner Query

10
SUBQUERIES

 Question9
Write a query to display all employee’s names, their
salaries and their job whose jobs same as job of
William Smith.

To solve this query


❑ Find the job of William Smith
❑ Then find all employee’s whose job is same as job found.

11
SUBQUERY SYNTAX
SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM 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 last_name
11000 employees
FROM
WHERE salary >
(SELECT salary
FROM employees
WHERE last_name = 'Abel');
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.
TYPES OF SUBQUERIES

• Single-row subquery
Main query
returns
Subquery ST_CLERK

• Multiple-row subquery
Main query
returns ST_CLERK
Subquery
SA_MAN
SINGLE-ROW SUBQUERIES

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

<> Not equal to


EXECUTING SINGLE-ROW SUBQUERIES
SELECT last_name, job_id, salary
FROM employees
WHERE job_id =ST_CLERK
(SELECT job_id
FROM employees
WHERE employee_id = 141)
AND salary > 2600
(SELECT salary
FROM employees
WHERE employee_id = 143);
USING GROUP FUNCTIONS IN A SUBQUERY
SELECT last_name, job_id, salary
FROM employees2500
WHERE salary =
(SELECT MIN(salary)
FROM employees);
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 department_id, MIN(salary)


FROM employees
GROUP BY department_id
2500
HAVING MIN(salary) >
(SELECT MIN(salary)
FROM employees
WHERE department_id = 50);
WHAT IS WRONG
WITH THIS STATEMENT?

SELECT employee_id, last_name


FROM employees
WHERE salary =
(SELECT MIN(salary)
FROM employees
GROUP BY department_id);

ERROR at line 4:
ORA-01427: single-row subquery returns more than
one row
WILL THIS STATEMENT RETURN ROWS?

SELECT last_name, job_id


FROM employees
WHERE job_id =
(SELECT job_id
FROM employees
WHERE last_name = 'Haas');

no rows selected
MULTIPLE-ROW SUBQUERIES

Return more than one row 


Use multiple-row comparison operators 
Operator Meaning

IN Equal to any member in the list

ANY Compare value to each value returned by


the subquery

Compare value to every value returned by


ALL
the subquery
USING THE ANY OPERATOR
IN MULTIPLE-ROW SUBQUERIES

SELECT employee_id, last_name, job_id, salary


FROM 9000,
employees
6000, 4200
WHERE salary < ANY
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';


USING THE ALL OPERATOR
IN MULTIPLE-ROW SUBQUERIES

SELECT employee_id, last_name, job_id, salary


FROM employees
9000, 6000, 4200
WHERE salary < ALL
(SELECT salary
FROM employees
WHERE job_id = 'IT_PROG')
AND job_id <> 'IT_PROG';
NULL VALUES IN A SUBQUERY

SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM employees mgr);

no rows selected
QUESTIONS
 Question10
Write a query to display all employees names, their
hire dates and their departments numbers that was
hired after Tayler Fox and get more than employee
184

 Question11
Write a query to display all employees information who
have the get maximum paid salary.

26
QUESTIONS
 Question12
Write a query to display departments and their
total paid salaries, exclude any department
that its total salary less than that of
department 30.
 Question13
State weather the following query is valid or not
SELECT empno , ename
FROM emp
WHERE sal = (SELECT MIN(sal)
FROM emp 27
GROUP BY deptno);
MULTIPLE ROW OPERATOR

Operator Description

IN Equal to any value

ALL Compare it to every value

ANY Compare it each value

28
QUESTIONS
 Question14
Write a query to display all employee’s
information whose less than any Stock Clerk
and they are not Stock Clerk.

 Question15
Write a query to display employee’s numbers,
names and their salaries for employees who
earn more than the average salary and who
work in department with any employee with a
letter T in their names. 29

You might also like