Professional Documents
Culture Documents
LECTURE WEEK 6
E-mail: kamran.munir@gmail.com
(2) kamran@niit.edu.pk
Concept of Sub-query
Sub-query Examples
Sub-query Practice
Use of GROUP BY clause
GROUP BY Examples
Date Arithmetic
Date Functions
To count the
employees who are
getting salary.
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.
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.
Use of GROUP BY
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
AVG(SAL)
---------625
800
200
HAVING clause
HAVING clause
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
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
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
HIREDATE
--------17-DEC-80
20-FEB-81
22-FEB-81
02-APR-81
(SYSDATE-HIREDATE)/365
--------------------21.9493285
21.7712463
21.7657669
21.6589176
12
ADD_MONTHS Function
Also try
ADD_MONTHS(hiredate, 4)
ADD_MONTHS(hiredate, 12)
.
.
13
14
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
contd..
MONTHS_BETWEEN contd..
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 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