ENO Emp - NM Salary DEP
ENO Emp - NM Salary DEP
3) Copy emp table to the table which declare our own attribute names..
14 rows selected.
-1-
Select
Syntax:
Distinct
Syntax:
In above query first rows are selected on DEPTNO then it will go to the job.
SELECT DISTINCT deptno,job from emp
DEPTNO JOB
10CLERK
10MANAGER
-2-
10PRESIDENT
20ANALYST
20CLERK
20MANAGER
30CLERK
30MANAGER
30SALESMAN
9 rows selected.
JOB DEPTNO
ANALYST 20
CLERK 10
CLERK 20
CLERK 30
MANAGER 10
MANAGER 20
MANAGER 30
PRESIDENT 10
SALESMAN 30
9 rows selected.
-3-
Sorting:
Syntax:
9). Display details of all employees the DECENDING order of there employees salary.
14 rows selected.
11). Display the deportment number job, emp no, name & salary on the ASCENDING order of
deptno and DECENDING order of job.
(Or)
SELECT deptno,job,empno,ename,sal from emp order by deptno asc,job desc;
-4-
10CLERK 7934MILLER 1300
20MANAGER 7566JONES 2975
20CLERK 7369SMITH 800
20CLERK 7876ADAMS 1100
20ANALYST 7788SCOTT 3000
20ANALYST 7902FORD 3000
30SALESMAN 7499ALLEN 1600
30SALESMAN 7654MARTIN 1250
30SALESMAN 7521WARD 1250
30SALESMAN 7844TURNER 1500
30MANAGER 7698BLAKE 2850
30CLERK 7900JAMES 950
14 rows selected.
COMM
1400
500
300
0
14 rows selected.
In above result all null values are came first that is because in oracle during data sorting NULL
VALUES will be consider as BIGGEST value.
Hear oracle using SINGLE BYTE OVERHEARD mechanism during data sorting ware
aver NULL exist.
-5-
In this mechanism oracle first sort all the not null values first, to biggest not null values
memory size one byte is added and this memory size is assigned to the null value, considering
null as biggest value.
COMM
0
300
500
1400
DEPTNO
10
20
30
-6-
Where:
Syntax:
is to define the restrictions on data selection i.e retrieving specific rows of data based on some
condition.
OPERATORS SQL:-
Arithmetic operations:
+ . - , * , / .
Relational operator:-
< , >, <=, >=, =, IN, BETWEEN, LIKE, IS NULL.
Negation operations:
<> , !=, ^= not equal to.
NOT IN, NOT BETWEEN, NOT LIKE, IS NOT NULL.
Logical operator:
AND, OR, NOT.
Set operators:
UNION, UNION ALL, INTERSECT, MINUS.
-7-
EXAMPLES:
15). Display name, salary, daily salary, annual salary and experience of all the
employees.
14 rows selected.
Note:
Date-date is allowed.(it returns days)
Date +date is allowed. (it returns days)
Date-number is allowed(it returns date).
-8-
17).display all the employees working from deportment 30.
6 rows selected.
18) List all the emp’s working for the manager 7698.
-9-
7839KING PRESIDENT 17-NOV-81 5000 10
7844TURNER SALESMAN 769808-SEP-81 1500 0 30
7876ADAMS CLERK 778823-MAY-87 1100 20
7900JAMES CLERK 769803-DEC-81 950 30
7902FORD ANALYST 756603-DEC-81 3000 20
7934MILLER CLERK 778223-JAN-82 1300 10
13 rows selected.
20) list all the emp’s have an experience more then 27 years.
11 rows selected.
- 10 -
8 rows selected.
10 rows selected.
25).List all the employees joined in beginning of the third quarter of 1981 still the end of
the second quarter of 1982.
- 11 -
7654MARTIN SALESMAN 769828-SEP-81 1250 1400 30
7839KING PRESIDENT 17-NOV-81 5000 10
7844TURNER SALESMAN 769808-SEP-81 1500 0 30
7900JAMES CLERK 769803-DEC-81 950 30
7902FORD ANALYST 756603-DEC-81 3000 20
7934MILLER CLERK 778223-JAN-82 1300 10
26).Display all the employees how’s annual salary is ranging from 18000 to 42000.
13 rows selected.
Like:
The delimiter is used is used like character are:
‘_’ represent a single charactor.
% to represent a group of characters.
- 12 -
IS NULL to identify the null values.
IS NOT NULL to identify not null values.
31).display all the emp’s joined in month which secound character is ‘E’.
Note:
_% = %_ = %
Key:
Sql> C/J/E means Change where J replace with E.
- 13 -
Select * from emp where job=’MANAGER’ and mgr is NOT NULL;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566JONES MANAGER 783902-APR-81 2975 20
7698BLAKE MANAGER 783901-MAY-81 2850 30
7782CLARK MANAGER 783909-JUN-81 2450
34).list all employees of department no.20 and receiving annual salary ranging
from 2200 and 40000.
Select * from emp where deptno=20 and sal*12 between 22000 and 40000;
35).display all the emp’s having experience more then 20 years and less then
30,not receiving any commission in the ascending order of salary.
36).display details of all employees joined in 1981 but not belongs to the starting
month character ‘A’, working with not registering president and analyst.
- 14 -
where
hiredate between '01-jan-1981' and '31-dec-1981' and
hiredate not like '0%' and
hiredate not like '%_A%' and
job not in('PRESIDENT','CLARK');
- 15 -
Functions:
Function is a predefined function perform a predefined program.
Mostly function for calculation or manipulation purpose(not DML).
EVERY FUNCTION IS RETURN A VALUE.
For few function inputs are mandate or optional or not required.
(Depending upon the process capabilities )
ARITHEMATIC FUNCTION:
b).ROUND():-
- 16 -
ROUND(50,-2)
100
Select round(49,-2) from dual;
ROUND(49,-2)
0
TRUNC():-
CEIL():-
It rounding only decimal points.
It always round to upper value.
FLOOR():-
- 17 -
SQRT():-
MOD():-
POWER():-
SIGN():-
It returns –1 if it is negative otherwise it returns 0.
GREATEST(),LEAST() :-
Greatest(),least() functions are also called as functions these two function
accept any no.of arguments these two function works with any data type but all
parameter of these function belongs to same data type.
NVL(<col>,<val>):-
Null value conversion:- this function used to convert a null value into specified not
null value.
This function required two parameters.
- 18 -
This function works on any parameters belongs to same data type.
Example:
select nvl(comm,0) from emp;
36).display the employee number, name, sal, annual salary rounded to nearest 100, daily
salary rounded the rupee and experience with 2 decimal places of all the emp’s
Select empno,ename,sal,
round(sal*12,-2) asal,
round(sal/30,0) dsal,
trunc((sysdate-HIREDATE)/365,2) exp
from emp;
EMPNO ENAME SAL ASAL DSAL EXP
7369SMITH 800 9600 27 28.08
7499ALLEN 1600 19200 53 27.9
7521WARD 1250 15000 42 27.9
7566JONES 2975 35700 99 27.79
7654MARTIN 1250 15000 42 27.3
7698BLAKE 2850 34200 95 27.71
7782CLARK 2450 29400 82 27.6
7788SCOTT 3000 36000 100 21.74
7839KING 5000 60000 167 27.16
7844TURNER 1500 18000 50 27.35
7876ADAMS 1100 13200 37 21.65
7900JAMES 950 11400 32 27.12
7902FORD 3000 36000 100 27.12
7934MILLER 1300 15600 43 26.98
- 19 -
CHARACTER FUNTION:
UPPER():-
LOWER():
INITCAP():
The first letter of the word convert into upper case.
ASCII():
CHR():
SUBSTR():
It require three parameters
1).String
2).which position u want..
3).how many characters u want..
- 20 -
Select substr(‘ameerkpet’,6) from dual;
SUBS
kpet
INSTR():
It require four parameters
1).String
2).searching char
3).from which position
4).number of occurred position of that latter(ie.1 st ,2nd,3rd ).
Select instr('ameerpet','e',2,3) from dual;
INSTR('AMEERPET','E',2,3)
7
Display al the emp’s names having a character ‘L’ at least 2 times after the 2 nd
possion.
select * from emp where instr(ename,'L',3,2)>0;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7934MILLER CLERK 778223-JAN-82 1300 10
LENGTH():
LPAD():
It pads from left side.
It require three parameters
string or number.
- 21 -
1) Max length of string u want.
2) Character to be filled.
Select lpad(‘5000’,6,’*’) from dual;
LPAD('
**5000
RPAD():
It pads from right.
Select rpad('5000',6,'*') from dual;
RPAD('
5000**
LTRIM():
It removes from extreme left.
Default character which is trimmed is space character.
Select ltrim(‘AMEERPER’,’A’) from dul;
LTRIM('
MEERPER
RTRIM():
It trimmed from extreme right.
Select rtrim('AAAAAAMEERPER','R') from dual;
RTRIM('AAAAA
AAAAAAMEERPE
Select rtrim('AAAAAAMEERPERRRRR','ER') from dual;
RTRIM('AAAA
AAAAAAMEERP
CONCAT():
It joins two strings.
We also use pipe simbole(||).
Select concat('this is ','my home…') from dual;
CONCAT('THISIS'
- 22 -
this is myhome…
TRANSLATE():
It decode the given string.
In this function first we give the string and then we specify the coding.
Select translate ('AMEERPET','ABCDEFGHIJKLM','123456789abcd')from dual;
TRANSLAT
1d55RP5T
REPLACE():
This function find the string and replace with encoding string.
It require three parameters
1).the main string.
2).string which is to be replaced with encoding.
3).encoding string or characters.
Select replace('AMEERPET','P','12345') from dual;
REPLACE('AME
AMEER12345ET
DECODE():
It is like a NESTED IF-ELSE statement in languages.
It decode different string with different coding.
Select job,decode(job,’CLERK’,’C’,
‘MANAGER’,’M’,
‘SALESMAN’,’S’,
‘UNKNOUN JOB’) from emp;
JOB DECODE(JOB,
CLERK C
SALESMAN S
SALESMAN S
MANAGER M
SALESMAN S
MANAGER M
MANAGER M
ANALYST UNKNOUN JOB
PRESIDENT UNKNOUN JOB
SALESMAN S
- 23 -
CLERK C
CLERK C
ANALYST UNKNOUN JOB
CLERK C
14 rows selected
14 rows selected.
- 24 -
39).display the all emp’s who’s joined in first 5 days of any month.
Select * from emp where substr(hiredate,1,2) between 1 and 5;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566JONES MANAGER 783902-APR-81 2975 20
7698BLAKE MANAGER 783901-MAY-81 2850 30
7900JAMES CLERK 769803-DEC-81 950 30
7902FORD ANALYST 756603-DEC-81 3000 20
8 rows selected
- 25 -
Mr.Jonesis working for the dept deptno for the manager 7839 as MANAGER with a
salary Rs. 2975 since 81
Mr.Martinis working for the dept deptno for the manager 7698 as SALESMAN with a
salary Rs. 1250 since 81
Mr.Blakeis working for the dept deptno for the manager 7839 as MANAGER with a
salary Rs. 2850 since 81
Mr.Clarkis worcking for the dept deptno for the manager 7839 as MANAGER with a
salary Rs. 2450 since 81
Mr.Scottis worcking for the dept deptno for the manager 7566 as ANALYST with a
salary Rs. 3000 since 87
Mr.Kingis worcking for the dept deptno for the manager as PRESIDENT with a salary
Rs. 5000 since 81
Mr.Turneris worcking for the dept deptno for the manager 7698 as SALESMAN with a
salary Rs. 1500 since 81
Mr.Adamsis worcking for the dept deptno for the manager 7788 as CLERK with a salary
Rs. 1100 since 87
Mr.Jamesis worcking for the dept deptno for the manager 7698 as CLERK with a salary
Rs. 950 since 81
Mr.Fordis worcking for the dept deptno for the manager 7566 as ANALYST with a
salary Rs. 3000 since 81
Mr.Milleris worcking for the dept deptno for the manager 7782 as CLERK with a salary
Rs. 1300 since 82
14 rows selected.
- 26 -
This function accept any data type required sing paramitor.
To number():-
Select to_number(‘123’) from dual;
TO_NUMBER('123')
123
To_date:-
Date function:-
Months _between(date,date)
- 27 -
Select months_between(sysdate,’09-jan-2000’) from dual;
MONTHS_BETWEEN(SYSDATE,'09-JAN-2000')
108.045612
Add_months(date,number)
Last_day(date)
It returns the last day of month.
Select last_day('09-feb-2000') from dual;
LAST_DAY(
29-FEB-00
Next_day(date,’day’)
It returns first day(sun,mon….) after sys_date of the next week
This function returns the date which specified from sysdate.
Select next_day(sysdate,’tue’) from dual;
NEXT_DAY(
13-JAN-09
Greatest():
- 28 -
Select greatest(’09-feb-3000’,sysdate,’09-jan-1200’) from dual;
GREATEST('0
10-JAN-09
Display the empno , name ,sal, daily sal of all the salesman (as per days of
month)
Select empno,ename,sal
Sal/(to_char(last_day(add_months(sysdate,-1),’dd’))) d_sal from emp;
EMPNO ENAME SAL DSAL
7369SMITH 800 25.8064516
7499ALLEN 1600 51.6129032
7521WARD 1250 40.3225806
7566JONES 2975 95.9677419
7654MARTIN 1250 40.3225806
7698BLAKE 2850 91.9354839
7782CLARK 2450 79.0322581
7788SCOTT 3000 96.7741935
7839KING 5000 161.290323
7844TURNER 1500 48.3870968
7876ADAMS 1100 35.483871
7900JAMES 950 30.6451613
7902FORD 3000 96.7741935
- 29 -
7934MILLER 1300 41.9354839
14 rows selected
44) display total no.of remaining day in the month after current date.
Select
to_char(last_day(sysdate),'dd')total_days,
To_char(sysdate,'dd') no_of_days_ex,
To_char(last_day(sysdate),'dd') - to_char(sysdate,'dd') no_of_days_re
from dual;
TO NO NO_OF_DAYS_RE
31 10 21
Least():-
DATE FORMATES:
- 30 -
day - it week day in character from (Monday,Tuesday,…)
MONTH FORMATES:
YEAR FORMATES:
select to_char(sysdate,'y yy yyy yyyy') from dual;
TO_CHAR(SYSDA
9 09 009 2009
select to_char(sysdate,'year') from dual;
TO_CHAR(SYSDATE,'YEAR')
two thousand nine
TIME FORMATES:
select to_char(sysdate,'hh hh12 hh24 mi ss sssss') from dual;
TO_CHAR(SYSDATE,'HHH
11 11 11 27 04 41224
Select to_char(sysdate,'am pm') from dual;
TO_CH
am am
Select to_char(‘09-jan-2100’) from dual;
ORA-01722: invalid number
Note: we not able to convert int char to character.
- 31 -
SP: spelling
Select to_char(sysdate,'ddsp') from dual;
TO_CHAR(SYSD
ten
TH:
Select to_char(sysdate,'ddth') from dual;
TO_C
10th
Select to_char(sysdate,'ddthsp') from dual;
(or)
Select to_char(sysdate,'ddspth') from dual;
TO_CHAR(SYSDAT
tenth
Week format;
select to_char(sysdate,'day')||
to_char(sysdate,' ddth ')||
to_char(sysdate,' month ')||
to_char(sysdate,' yyyy bc ') from dual;
TO_CHAR(SYSDATE,'DAY')||TO_CHAR(SYS
- 32 -
saturday 10th january 2009 ad
select to_char(sysdate,'day')||'the'||
to_char(sysdate,' ddth ')||' of '||
to_char(sysdate,' month yyyy hh24:mi::ss')
from dual;
TO_CHAR(SYSDATE,'DAY')||'THE'||TO_CHAR(SYSDATE,
saturday the 10th of january 2009 12:32::42
49)display all the emp’s 1 st and 4th quarter of 1981 and 1983.
S
no rows selected
Note: hear ‘dy’ not take as a ‘SUNDAY’…it takes as ‘SUNDAY---‘.
In week strings ‘Wednesday’ have the highest length so other week string are
trimmed. So we do like this…
- 33 -
7788SCOTT ANALYST 756619-APR-87 3000 20
);
insert into dept1 values(30,'fin','ban');
DEPTNO DNAME LOC
10acc hyd
20acc hyd
30fin ban
- 34 -
create table emp2
(
empno number(2) primary key,
ename varchar2(10) not null,
sal number(7) not null,
job varchar2(10) not null,
deptno number(2) references dept on delete cascade
);
insert into emp2 values(2,'raJU',4000,'CLEARK',20);
EMPNO ENAME SAL JOB DEPTNO
1ravi 3000CLEARK 10
2raJU 4000CLEARK 20
- 35 -
TABLES
Emp:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369SMITH CLERK 790217-DEC-80 800 20
7499ALLEN SALESMAN 769820-FEB-81 1600 300 30
7521WARD SALESMAN 769822-FEB-81 1250 500 30
7566JONES MANAGER 783902-APR-81 2975 20
7654MARTIN SALESMAN 769828-SEP-81 1250 1400 30
7698BLAKE MANAGER 783901-MAY-81 2850 30
7782CLARK MANAGER 783909-JUN-81 2450 10
7788SCOTT ANALYST 756619-APR-87 3000 20
7839KING PRESIDENT 17-NOV-81 5000 10
7844TURNER SALESMAN 769808-SEP-81 1500 0 30
7876ADAMS CLERK 778823-MAY-87 1100 20
7900JAMES CLERK 769803-DEC-81 950 30
7902FORD ANALYST 756603-DEC-81 3000 20
7934MILLER CLERK 778223-JAN-82 1300 10
14 rows selected.
Dept :
DEPTNO DNAME LOC
10ACCOUNTING NEW YORK
20RESEARCH DALLAS
30SALES CHICAGO
40OPERATIONS BOSTON
Salgrad:
GRADE LOSAL HISAL
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
- 36 -
JOINS
Display the details of the jems.
Elias:
Select d.loc from emp e,dept d where e.ename=’JEMS’ and d.deptno=e.deptno;
- 37 -
d.deptno=e.deptno;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7499ALLEN SALESMAN 769820-FEB-81 1600 300 30
7521WARD SALESMAN 769822-FEB-81 1250 500 30
7654MARTIN SALESMAN 769828-SEP-81 1250 1400 30
7844TURNER SALESMAN 769808-SEP-81 1500 0 30
53)display the all the employees along with location and experience of all
employees.
Select e.*,d.loc,(sysdate-e.hiredate)/12 exp from emp e,dept d
where
d.deptno=e.deptno;
EMPN HIREDAT COM DEPTN
ENAME JOB MGR SAL LOC EXP
O E M O
17-DEC- 854.29778
7369SMITH CLERK 7902 800 20DALLAS
80 9
SALESMA 20-FEB- CHICAG 848.88112
7499ALLEN 7698 1600 300 30
N 81 O 3
SALESMA 22-FEB- CHICAG 848.71445
7521WARD 7698 1250 500 30
N 81 O 6
02-APR- 845.46445
7566JONES MANAGER 7839 2975 20DALLAS
81 6
SALESMA 28-SEP- CHICAG 830.54778
7654MARTIN 7698 1250 1400 30
N 81 O 9
01-MAY- CHICAG 843.04778
7698BLAKE MANAGER 7839 2850 30
81 O 9
09-JUN- NEW 839.79778
7782CLARK MANAGER 7839 2450 10
81 YORK 9
19-APR- 661.46445
7788SCOTT ANALYST 7566 3000 20DALLAS
87 6
PRESIDEN 17-NOV- NEW 826.38112
7839KING 5000 10
T 81 YORK 3
TURNE SALESMA 08-SEP- CHICAG 832.21445
7844 7698 1500 0 30
R N 81 O 6
23-MAY- 658.63112
7876ADAMS CLERK 7788 1100 20DALLAS
87 3
03-DEC- CHICAG 825.04778
7900JAMES CLERK 7698 950 30
81 O 9
03-DEC- 825.04778
7902FORD ANALYST 7566 3000 20DALLAS
81 9
NEW 820.79778
7934MILLER CLERK 778223-JAN-821300 10
YORK 9
14 rows selected.
53)display the managers of all the location in the descending order of their
salaries.
- 38 -
where
e.job='MANAGER'and
d.deptno=e.deptno order by e.sal;
ENAM HIREDAT DEPTN
EMPNO JOB MGR SAL COMM LOC EXP
E E O
MANAGE NEW 839.79886
7782CLARK 783909-JUN-81 2450 10
R YORK 2
MANAGE 01-MAY- CHICAG 843.04886
7698BLAKE 7839 2850 30
R 81 O 2
MANAGE 845.46552
7566JONES 783902-APR-812975 20DALLAS
R 9
54) Display all managers and analyst of both accounting and research
deportment having a experience when it from 27 to 29 years, not receiving any
commission but working for some manager.
55) display the details of all the employees along with their grades.
select e.*,s.grade from emp e, salgrade s where e.sal between s.losal and
s.hisal;
- 39 -
56)display all the grade 3 and 4 managers.
select e.*,s.grade from emp e,salgrade s where e.job='MANAGER'and s.grade in(3,4) and
e.sal between s.losal and s.hisal;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO GRADE
7566JONES MANAGER 783902-APR-81 2975 20 4
7698BLAKE MANAGER 783901-MAY-81 2850 30 4
7782CLARK MANAGER 783909-JUN-81 2450 10 4
58) display employes number and name salary,daily salary grade in terms of *’s
location of all the employees.
select e.empno,e.ename,e.sal,decode(s.grade,1,'*',2,'**',3,'***',4,'****',5,'*****')
grade
from emp e,dept d,salgrade s
where e.deptno=d.deptno and
e.sal between s.losal and s.hisal;
EMPNO ENAME SAL GRADE
7369SMITH 800*
7876ADAMS 1100*
7900JAMES 950*
7521WARD 1250**
7654MARTIN 1250**
7934MILLER 1300**
7499ALLEN 1600***
7844TURNER 1500***
7566JONES 2975****
7698BLAKE 2850****
7782CLARK 2450****
7788SCOTT 3000****
7902FORD 3000****
7839KING
- 40 -
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7782CLARK MANAGER 783909-JUN-81 2450 10
7839KING PRESIDENT 17-NOV-81 5000 10
)
--condition 2--
(
(s.grade in (5,4) and d.loc='NEW YORK')
e.sal*12>3000 and (e.comm is null or e.comm=0)
e.mgr is not null and
(
mod (substr(e.mgr,3,1),2)<>0 or
mod (substr(e.mgr,3,1),4)<>0
)
) and
(e.deptno=d.deptno and e.sal between s.losal and s.hisal);
EMPNO ENAME SAL GRADE DEPTNO
7499ALLEN 1600 1 30
7521WARD 1250 1 30
7654MARTIN 1250 1 30
7844TURNER 1500 1 30
7499ALLEN 1600 2 30
7521WARD 1250 2 30
7654MARTIN 1250 2 30
7844TURNER 1500 2 30
7499ALLEN 1600 3 30
7521WARD 1250 3 30
7654MARTIN 1250 3 30
7844TURNER 1500 3 30
7782CLARK 2450 4 10
13 rows selected.
- 41 -
SELF J0IN:-
It is for join objects of same table.
This we will use when we have to retrieve row’s from column where
checking condition on same column.
Join condition must be on comparing column.
Select data table and the table checking condition must be different.
61) display details of the employees hose salary is more then JEMS salary.
12 rows selected.
62) display the details of all the employees hose job is same as BLACK.
64) Display the details of all the emp’s how are senior to to president.
Select b.* from emp a,emp b where a.job=’PRESIDENT’ and b.hiredate <a.hiredate;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369SMITH CLERK 790217-DEC-80 800 20
7499ALLEN SALESMAN 769820-FEB-81 1600 300 30
7521WARD SALESMAN 769822-FEB-81 1250 500 30
7566JONES MANAGER 783902-APR-81 2975 20
7654MARTIN SALESMAN 769828-SEP-81 1250 1400 30
- 42 -
7698BLAKE MANAGER 783901-MAY-81 2850 30
7782CLARK MANAGER 783909-JUN-81 2450 10
7844TURNER SALESMAN 769808-SEP-81 1500 0 30
8 rows selected
65) Display the details of all the emp’s who are senior of their own MANAGER’S .
select b.* from emp a,emp b where a.mgr=b.empno and b.job=’MANAGER’;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698BLAKE MANAGER 783901-MAY-81 2850 30
7698BLAKE MANAGER 783901-MAY-81 2850 30
7698BLAKE MANAGER 783901-MAY-81 2850 30
7566JONES MANAGER 783902-APR-81 2975 20
7698BLAKE MANAGER 783901-MAY-81 2850 30
7698BLAKE MANAGER 783901-MAY-81 2850 30
7566JONES MANAGER 783902-APR-81 2975 20
7782CLARK MANAGER 783909-JUN-81 2450 10
8 rows selected.
Note:
Hear default Is inner join.
Natural join automatically define join condition on the common column of
the table.
No separate join condition is allowed.
The table name qualifier is not allowed for joining column.
Select deptno from emp e join dept.d using (deptno);
Using clause is useful to put you’r subjection to oracle to indicate to which
column the join to be defind.
Display the enum,ename,sal,grade of all the emp’s’
- 43 -
select e.empno,e.ename,e.sal,s.grade from emp e inner join salgrade s on (e.sal between
s.losal and s.hisal);
EMPNO ENAME SAL GRADE
7369SMITH 800 1
7876ADAMS 1100 1
7900JAMES 950 1
7521WARD 1250 2
7654MARTIN 1250 2
7934MILLER 1300 2
7499ALLEN 1600 3
7844TURNER 1500 3
7566JONES 2975 4
7698BLAKE 2850 4
7782CLARK 2450 4
7788SCOTT 3000 4
7902FORD 3000 4
7839KING 5000 5
Display the salesmans of the sales dept in the desc order of there salaries.
- 44 -
7782CLARK 2450 4NEW YORK
7788SCOTT 3000 4DALLAS
7902FORD 3000 4DALLAS
7839KING 5000 5NEW YORK
SUB QUERY’S:-
Select * from emp where sal>(select sal from emp where ename=’JAMES’) ;
Select * from emp where job=(select job from emp where ename=’BLAKE’);
66) Display details of all the emp’s of employees of ACCOUNTING dept table
whose job is name as JONES job.
- 45 -
YOR
K R 981 0 NG
K
67) display the details of employees whose jobs are same as the jobs of
RESEARCH deportment and restrict the details of the employees of research
deportment from output.
Select * from emp
Where job in
(
select distinct e.job from emp e,dept d where d.dname='RESEARCH' and
e.deptno=d.deptno
)
and
deptno<>(select deptno from dept where dname='RESEARCH');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7934MILLER CLERK 778223-JAN-82 1300 10
7900JAMES CLERK 769803-DEC-81 950 30
7782CLARK MANAGER 783909-JUN-81 2450 10
7698BLAKE MANAGER 783901-MAY-81 2850 30
68) display details of all emp’s are those who belongs to deportment where
ADAMS,MANGARS MANAGERS MANAGER is working.
- 46 -
where empno=(
select mgr from emp
where empno=
(
select mgr from emp
where empno= (select mgr from emp where
ename='ADAMS')
)
)
);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7782CLARK MANAGER 783909-JUN-81 2450 10
7839KING PRESIDENT 17-NOV-81 5000 10
7934MILLER CLERK 778223-JAN-82 1300 10
69) diplay the details of all the employees whose salary is less then PRESIDENT
salary and it should be more then salary of the employees 7900 employees and
belongs to any department except FORD’s department and join after SMITH but
before ADAMS.
>ANY >1300
<ANY <5000
select sal from emp where deptno=10 and sal> any(select sal from emp where
deptno=20);
- 47 -
70) select details of deportment 10 and 30 whose salaries are more then the
salary of all the employees of deportment 20.
Select * from emp where deptno in (10,30) and sal> all(select sal from emp where
deptno=20);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7839KING PRESIDENT 17-NOV-81 5000 10
71) display the details of all the employees if there is any employees with a name
‘JEMES’ exist in database.
Select * from emp where exists (select * from emp where ename='JAMES');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369SMITH CLERK 790217-DEC-80 800 20
7499ALLEN SALESMAN 769820-FEB-81 1600 300 30
7521WARD SALESMAN 769822-FEB-81 1250 500 30
7566JONES MANAGER 783902-APR-81 2975 20
7654MARTIN SALESMAN 769828-SEP-81 1250 1400 30
7698BLAKE MANAGER 783901-MAY-81 2850 30
7782CLARK MANAGER 783909-JUN-81 2450 10
7788SCOTT ANALYST 756619-APR-87 3000 20
7839KING PRESIDENT 17-NOV-81 5000 10
7844TURNER SALESMAN 769808-SEP-81 1500 0 30
7876ADAMS CLERK 778823-MAY-87 1100 20
7900JAMES CLERK 769803-DEC-81 950 30
7902FORD ANALYST 756603-DEC-81 3000 20
7934MILLER CLERK 778223-JAN-82 1300 10
14 rows selected.
GROUP FUNCTION: -
A function which process a set of rows at a time and returns a single
value.
1.max()
2.min()
3.sum()
4.avg()
5.count()
note:-
all the above functions required a single paramitor.
All group functions are ignored null values.
- 48 -
Max,min and count function work on any data type but sum and avg work
on only number data type.
1.max(<col name>)
73) Display the junior most employee and display junior most employee hire
date.
Select max (hiredate) from emp;
MAX(HIRED
23-MAY-87
- 49 -
) and
Mgr= (select empno from emp where ename =’BLAKE’);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7900JAMES CLERK 769803-DEC-81 950 30
78) Find the total experiences of all the employees’ year how managers to others
are.
Select (months_between(sysdate,hiredate)/12) total_exp from emp
Where empno in (select distinct mgr from emp);
TOTAL_EXP
27.7887991
27.7081539
27.6033152
21.7431001
27.1651432
27.1194442
In above query we are trying to convert the number into words.for this,it is not
possible bcz we not convert number to char.so hear we first convert number into
date(hear also we not convert number into date it only available in junior
level(‘j’),so using this we done above.)
Round is round the number bcz decimal number we cannot change into date.
4) Avg(<col name>)
is it find the avg of all column values
81) Display the details of the all the employees hose salary is more then the avg
sal of all the emp’s.
- 50 -
Select * from emp where sal>(select avg(sal) from emp);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566JONES MANAGER 783902-APR-81 2975 20
7698BLAKE MANAGER 783901-MAY-81 2850 30
7782CLARK MANAGER 783909-JUN-81 2450 10
7788SCOTT ANALYST 756619-APR-87 3000 20
7839KING PRESIDENT 17-NOV-81 5000 10
7902FORD ANALYST 756603-DEC-81 3000 20
6 rows selected.
82) display the details of all the employees of accounting department,hose salary
is more then the avg salary of accounting department employees.
Select e.* from emp e,dept d where d.dname=’ACCOUNTING’and e.sal>(select
avg(e.sal) from emp e);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7566JONES MANAGER 783902-APR-81 2975 20
7698BLAKE MANAGER 783901-MAY-81 2850 30
7782CLARK MANAGER 783909-JUN-81 2450 10
7788SCOTT ANALYST 756619-APR-87 3000 20
7839KING PRESIDENT 17-NOV-81 5000 10
7902FORD ANALYST 756603-DEC-81 3000 20
6 rows selected.
Select count(*) from emp e,salgrade s where s.grade=2 and e.sal between s.losal and
s.hisal;
COUNT(*)
3
- 51 -
GORUP BY CLAUSE:
This clause tell oracle to group rows based on distinct values that exist for
specified columns i.e creats a data set, containing several sets of record
sets,and not on individual records.
Restrictions:
1) the columns those are specified is the group clause are only allowed in the
select statement .
2) columns other then the columns specified in the group by can also be
used in the select but along with group functions (for summery result).
ROLLUP: it is used to give total number of columns.
CUBE : it is used to give total number of rows.
- 52 -
CHICAGO 2 1250 1250
CHICAGO 3 1600 1500
CHICAGO 4 2850 2850
NEW YORK 2 1300 1300
NEW YORK 4 2450 2450
NEW YORK 5 5000 5000
9 rows selected.
16 rows selected.
HAVING CLAUSE: -
- 53 -
Select [distinct] <col>, <col>,……----5
From <tab>,<tab>,…-----------1
[where <condiction>]-----------2
group by <condiction>---------3
HAVING <condiction>---------4
[order by <col> [asc/desc]]---6
92) display the count of the employees belongs to grade In each deportment
where it is more then 2.
Select e.deptno,s.grade,count(*)
From emp e,slagrade s
Where e.sal between s.losal and s.hisal group by e.deptno,s.grade
Having count(*)>2
DEPTNO GRADE COUNT(*)
20 4 3
- 54 -
Group by deptno
Having count(deptno)=(select max(count(deptno)) from emp group by deptno);
DEPTNO COUNT(*)
30 6
94)display the details of managers fro whom max employees rae working .
select * from emp
where empno=( select mgr from emp group by mgr having count(*)=(select
max(count(*)) from emp group by mgr));
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698BLAKE MANAGER 783901-MAY-81 2850 30
98) if the number of emp’s dept 30 are more the 5 displays all those emp’s other
than don’t display.
Select * from emp
Where deptno=30 and exists (select deptno from emp where deptno=30 group
by deptno having count(*)>6);
no rows selected
Select * from emp
Where deptno=30 and not exists (select deptno from emp where deptno=30 group by
deptno having count(*)>6);
- 55 -
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7499ALLEN SALESMAN 769820-FEB-81 1600 300 30
7521WARD SALESMAN 769822-FEB-81 1250 500 30
7654MARTIN SALESMAN 769828-SEP-81 1250 1400 30
7698BLAKE MANAGER 783901-MAY-81 2850 30
7844TURNER SALESMAN 769808-SEP-81 1500 0 30
7900JAMES CLERK 769803-DEC-81 950 30
6 rows selected.
CO RELATED SUBQUERY:
- 56 -
102)display details of employees hose salary is more then avg sal of there won
deportment.
Select * from emp x
Where sal>(select avg(sal) from emp y where y.deptno=x.deptno)
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7499ALLEN SALESMAN 769820-FEB-81 1600 300 30
7566JONES MANAGER 783902-APR-81 2975 20
7698BLAKE MANAGER 783901-MAY-81 2850 30
7788SCOTT ANALYST 756619-APR-87 3000 20
7839KING PRESIDENT 17-NOV-81 5000 10
7902FORD ANALYST 756603-DEC-81 3000 20
6 rows selected.
103)Display the details of all the employees hose salary is less the avg salary of
there job
Select * from emp x where sal=(select max(sal) from emp y where y.deptno=x.deptno);
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7698BLAKE MANAGER 783901-MAY-81 2850 30
7788SCOTT ANALYST 756619-APR-87 3000 20
7839KING PRESIDENT 17-NOV-81 5000 10
7902FORD ANALYST 756603-DEC-81 3000 20
105) display the details of employees whose experience is more then the
employees of their won grade.
select e.* from emp e,salgrade s where
months_between(sysdate,e.hiredate)/12 >=
(select max(months_between(sysdate,hiredate)/12) from emp e,salgrade g
where g.grade=s.grade and e.sal between s.losal and g.hisal)
and
e.sal between s.losal and s.hisal;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369SMITH CLERK 790217-DEC-80 800 20
7521WARD SALESMAN 769822-FEB-81 1250 500 30
7499ALLEN SALESMAN 769820-FEB-81 1600 300 30
- 57 -
7566JONES MANAGER 783902-APR-81 2975 20
7839KING PRESIDENT 17-NOV-81 5000 10
106) display the details all the employees where all senior to there own
managers.
Select e.* from emp e, emp m where e.mgr = m.empno and e.hiredate<m.hiredate;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369SMITH CLERK 790217-DEC-80 800 20
7499ALLEN SALESMAN 769820-FEB-81 1600 300 30
7521WARD SALESMAN 769822-FEB-81 1250 500 30
7566JONES MANAGER 783902-APR-81 2975 20
7698BLAKE MANAGER 783901-MAY-81 2850 30
7782CLARK MANAGER 783909-JUN-81 2450 10
6 rows selected.
Select * from emp where (select max(sal) from emp e where sal<(select max(sal)
from emp));
Select * from (select sal,dense_rank() (order by sal desc) rank from emp) where
rank=&rank;
Select * from (select rownum rank,sal from(select distinct sal from emp order by
1 desc) where rank=&rank)
- 58 -
………………………Multiple updates………………….
INDEX
- 59 -
ii)unique index.
Constants is a part of definition of table index is external object.
By default primary key column have unique indexes created by orcle.
Creation of unique indexes on primary key and unique key not allowed.
INDEX:
Syntax:
CREATE [unique] INDEX <index name>ON <table name>
(<col>, <col>……);
So we do like this:
create index hiredate_ind on emp(hiredate desc);
7). Create reverse key index on grade column of sal grade table.
8)Create a reverse key index on the department column number of dept table on
descending order
- 60 -
create index raj on dept(deptno desc) reveres ;
. ERROR at line 1:
ORA-02158: invalid CREATE INDEX option
Note: index can’t use both desc and reverse.
CLUSTER
Syntax:
VIEW
It is a logical representation.
Register into disk.(it store in compilation version).
It not use any memory to store the data, that’s way it is a virtual table.
It is vary versatile in nature because parametric mehanisum.
Security.
Syntax:
Create view <view name>
[<col>,<cpl>,..] as select statement.
- 61 -
i)Create a view to list all the clarks.
View created.
Desc cl_view
Name Null? Type
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SALARY NUMBER(7,2)
ii) Create a view to display the employee number, name, salary and deptno of all
the emp’s on the descending order of deptno employees.
iii). Create a view to list the employee no,ename,and experience of all the emp’s
Note:All the columns of the views those are derived from arethematic
expressions, psedo columns and function-based columns must required, column
name aliases.
- 62 -
as
select * from emp e where deptno=20 with check option;
Oracle 8i interduced view trigger concept which allowes the view to accept
the DML operations to view. Through view is violating the above rules.
We not built index on view.
The above commend is use when the view is created on a table if the table Is
deleted and again I created table with same name and definition by this
command we retrieve the view.
- 63 -
SYNONYM
Types of synonym:
i)private synonym:
private synonym is a synonym created by any valid user of
database as well as by the DBS.
ii)public synonym
it is created only by the DBS.
Syntax:
Create [public] synonym <syn name> for <object name >;
- 64 -
- 65 -