Vous êtes sur la page 1sur 20

RDBMS Using Oracle

LECTURE WEEK 6

E-mail: kamran.munir@gmail.com
(2) kamran@niit.edu.pk

This Week Lecture Includes

Concept of Sub-query
Sub-query Examples
Sub-query Practice
Use of GROUP BY clause
GROUP BY Examples
Date Arithmetic
Date Functions

The COUNT Function


The count function is used to count the rows
selected by the query.

To count the number of rows in emp table enter:


SQL> select count(*) from emp;
COUNT(*)
---------14

To count the
employees who are
getting salary.

SQL> select count(sal) from emp;


COUNT(SAL)
---------14

SQL> select count (DISTINCT JOB) from


emp where deptno = 10;
COUNT (DISTINCT JOB)
-----------------2

To count the number


of different jobs held
by employees in
department 10.

Group functions Vs Individual


Functions

A command
Select ename, avg(sal) from emp
is invalid. Because select ename will return multiple
records and avg(sal) will return one average result of all
salaries.
IF we will enter this kind of command SQL*plus will
display a message saying that ENAME is not a singlegroup group function.

SQL> select ename, avg(sal) from emp;


select ename, max(sal) from emp
*
ERROR at line 1:
ORA-00937: not a single-group group function
SQL> select ename, max(sal) from emp;
select ename, max(sal) from emp
*
ERROR at line 1:
ORA-00937: not a single-group group function

Group function in a sub-query

Suppose we want to find out who makes the


highest salary in emp table. Since we cannot
write this SELECT ename, max(sal) from emp

In order to get this result we can put MAX(SAL)


function in sub query

Group function in a sub-query

To list the employee and job with the highest


salary enter:
SQL> select ENAME, JOB, SAL from emp
where sal = (select MAX(sal) from emp);
ENAME
--------------Alan

JOB
-------------------CLERK

SAL
---------900

Sub Query
Sub Query can be used with any of the following
Operators includes = , != , <> , <= , >= , NOT IN and
IN, <operator> ANY , <operator> ALL etc
= != <> < > <= >= all are used with single
value output of a sub query, where as IN, NOT IN,
<operator> ANY , <operator> ALL keywords can be
used for single as well as for multi value output of a sub
query.

SUB QUERY PRACTICE:


Note:You have understood the concept of SUBQUERY if you can
solve following queries by your own.

List the employee name and job with the lowest


salary?

List the employee name who has salary more


then the average salary of all Managers from
EMP table?

How many of employees have salary more then


the SUM of Salaries of All CLERKS?

Select ename from emp where


sal>(select min(sal) from emp)

Select ename from emp where sal > (select avg(sal)


from emp where job = MANAGER;
Select count (empno) from emp where sal> (select
sum(sal) from emp where job = CLERK);

Use of GROUP BY

SQL> select ename, max(sal) from emp


*
ERROR at line 1:
ORA-00937: not a single-group group function

Use of GROUP BY
Suppose you want to find sum of salaries for each
department.
select deptno, sum(sal) from emp group by deptno;
DEPTNO SUM(SAL)
-----------------10
2700
20
800

Use of GROUP BY

SQL> select job, avg(sal) from emp group by job;


JOB
-------------------CLERK
MANAGER
Manager

AVG(SAL)
---------625
800
200

HAVING clause

As we can select specific rows with a WHERE


clause, similarly we can select specfic groups
with a HEAVING clause.

HAVING clause will come after the GROUP


BY clause.

HAVING clause

As an example, suppose we want to list the average


salary for all job groups with more then two employees.

To list the average salary for all job groups with more
then two employees
SQL> select job , count (*) , avg (sal) from emp

group by JOB
HAVING count(*) > 2;
JOB
COUNT(*) AVG(SAL)
-------------------- ------------------CLERK
4
625

Can you solve this (A difficult One)


LIST all departments from emp table with
at least two clerks?
SQL> select DEPTNO from EMP
where JOB = 'CLERK'
GROUP BY DEPTNO
HAVING count(*) >= 2;
DEPTNO
---------10

Labeling GROUP Columns


SQL> select job, sum(sal) as "TOTAL SALARY"
from emp group by job;
JOB
-------------------CLERK
MANAGER
Manager

TOTAL SALARY
-----------2500
800
200

DATE ARITHMETIC
We can perform arithmetic operations on date
fields, the operations you may use are
Date + Number
Date Number
Date Date

10

We can perform arithmetic operations on date fields, the


operations you may use are
Date + Number
(add a number of days in a date, producing a date)
Date Number
(Subtracts a number of days in a date, producing a date)
Date Date
(Subtract one date from another, producing a number)

Date + Number
Add a number of days in a date,
producing a date.
SQL> Select sal, hiredate, hiredate + 365
as "New Hiredate" from emp
SAL
---------800
1600
1250
446.25
1250

HIREDATE
--------17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81
28-SEP-81

New Hiredate
--------17-DEC-81
20-FEB-82
22-FEB-82
02-APR-82
28-SEP-82

Try for
e.g.(hiredate 31)
Also try for
Date - Number
e.g.(hiredate 365)
And
e.g.(hiredate 180)

11

Date Date
Subtract one date from another, producing a
number.
Date Date returns result in number of days.
SQL> select sysdate, hiredate, (sysdate - hiredate) from emp;
SYSDATE
--------23-NOV-02
23-NOV-02
23-NOV-02
23-NOV-02

HIREDATE
--------17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81

(SYSDATE-HIREDATE)
-----------------8011.50647
7946.50647
7944.50647
7905.50647

SQL> select sysdate, hiredate, (sysdate - hiredate) / 365 from emp;


SYSDATE
--------23-NOV-02
23-NOV-02
23-NOV-02
23-NOV-02

HIREDATE
--------17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81

(SYSDATE-HIREDATE)/365
--------------------21.9493285
21.7712463
21.7657669
21.6589176

Here you can also use


Number Functions
like ROUND, CEIL,
FLOOR etc

12

ADD_MONTHS Function

ADD_MONTHS function is used to add any


number of months in a date.

ADD_MONTHS takes two arguments


a date
and a integer representing a number of month.
It returns a date after adding that number in months.

SQL>select hiredate, ADD_MONTHS (hiredate, 1)


from emp;
HIREDATE ADD_MONTH
----------------17-DEC-80
17-JAN-81
20-FEB-81
20-MAR-81
22-FEB-81
22-MAR-81
02-APR-81
02-MAY-81
28-SEP-81
28-OCT-81
01-MAY-81
01-JUN-81

Also try
ADD_MONTHS(hiredate, 4)
ADD_MONTHS(hiredate, 12)
.
.

13

GREATEST & LEAST


GREATEST Function is used to Find the Later
date from two given dates
LEAST Function is used to Find the Earlier date
from two given dates
For Example

SQL> select greatest('12-JAN-2002' , sysdate) from dual


GREATEST
----------14-NOV-03

SQL> select greatest('12-JAN-2002' , 11-DEC-2003) from dual


GREATEST
----------11-DEC-2003
Similarly you can use LEAST Function
SQL> select LEAST('12-JAN-2002' , 23-NOV-01) from dual
LEAST
----------23-NOV-01

We can also use GREATEST & LEAST functions For


two date columns e.g.
select GREATEST (Hiredate' ,any_date_column)
from mytable;

14

COUNT the employees (From


EMP table) which were hired in
each year.

YEAR
1980
1981
1987

Employees
1
10
2

Time
5 Mins

Select
TO_CHAR(HIREDATE, 'YYYY'), COUNT(*)

from emp
group by
TO_CHAR(HIREDATE, 'YYYY');

15

DATE Functions Contd


MONTHS_BETWEEN
This function is used to find the difference in
number of months between two date columns
or two given dates.
For Example

contd..

MONTHS_BETWEEN contd..

SQL> select months_between('12-JAN2002','12-JAN-2001') from dual;


MONTHS_BETWEEN('12-JAN-2002','12-JAN-2001')
------------------------------------------12

16

MONTHS_BETWEEN
SQL> select sysdate, hiredate,
months_between(sysdate, hiredate) as Diff From emp;
SYSDATE
--------01-Nov-03
01-Nov-03
01-Nov-03
01-Nov-03

HIREDATE
--------17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81

DIFF
-------------------------------276.50282
264.406045
264.341529
262.986691

LAST_DAY
This function is used to find out the last date of the month
for any given date.
LAST_DAY (HIREDATE)
SQL> select hiredate, LAST_DAY(HIREDATE) as "LAST
DATE" from emp

HIREDATE
--------17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81

LAST DATE
--------31-DEC-80
28-FEB-81
28-FEB-81
30-APR-81

17

LAST_DAY
SQL> Select LAST_DAY('20-DEC-2002') as Result
from dual;
RESULT
--------31-DEC-02

NEXT_DAY
This function is used to find the NEXT day
(given) of any date (given).
NEXT_DAY (HIREDATE, FRIDAY)

contd..

18

NEXT_DAY contd..
SQL> Select hiredate,
NEXT_DAY(HIREDATE, 'FRIDAY')
as "NEXT DAY" from emp;
HIREDATE
--------17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81

NEXT DAY
--------19-DEC-80
27-FEB-81
27-FEB-81
03-APR-81

NEXT DAY column


is showing the First
FRIDAY coming after
Given (hiredate) date.

NEXT_DAY contd..
SQL> select hiredate,
to_char(
NEXT_DAY(HIREDATE,'FRIDAY'),
'DY DD MM YY') as "NEXT DAY" from emp
HIREDATE
--------17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81

NEXT DAY
-----------FRI 19 12 80
FRI 27 02 81
FRI 27 02 81
FRI 03 04 81

Finding NEXT_DAY
and also displaying
DAY by using TO_CHAR
function.

19

NEXT_DAY
SQL> select
NEXT_DAY(15-Nov-2004', THUSDAY')
as EID DAY from dual;
EID DAY
--------16-NOV-04

Thanks

20