SQL

80
1) To copy the contents of the table EMP to RAJ1… CREATE table raj1 as select * from emp; 2) Create a table which contain structure is same as EMP… CREATE table raj2 as SELECT * from emp where 2=200; 3) Copy emp table to the table which declare our own attribute names.. CREATE table raj1(eno,emp_nm,salary,dep) as SELECT empno,ename,sal,deptno from emp; ENO EMP_NM SALARY DEP 7369SMITH 800 20 7499ALLEN 1600 30 7521WARD 1250 30 7566JONES 2975 20 7654MARTIN 1250 30 7698BLAKE 2850 30 7782CLARK 2450 10 7788SCOTT 3000 20 7839KING 5000 10 7844TURNER 1500 30 7876ADAMS 1100 20 7900JAMES 950 30 7902FORD 3000 20 7934MILLER 1300 10 14 rows selected. 4) To improve performance we use NOLOGGING.. CREATE table raj2(eno,emp_nm,salary,dep) NOLOGGING as SELECT empno,ename,sal,deptno from emp; - 1 -

Transcript of SQL

Page 1: SQL

1) To copy the contents of the table EMP to RAJ1…

CREATE table raj1 as select * from emp;

2) Create a table which contain structure is same as EMP…

CREATE table raj2 as SELECT * from emp where 2=200;

3) Copy emp table to the table which declare our own attribute names..

CREATE table raj1(eno,emp_nm,salary,dep) as SELECT empno,ename,sal,deptno from emp;

ENO EMP_NM SALARY DEP

7369 SMITH 800 20

7499 ALLEN 1600 30

7521 WARD 1250 30

7566 JONES 2975 20

7654 MARTIN 1250 30

7698 BLAKE 2850 30

7782 CLARK 2450 10

7788 SCOTT 3000 20

7839 KING 5000 10

7844 TURNER 1500 30

7876 ADAMS 1100 20

7900 JAMES 950 30

7902 FORD 3000 20

7934 MILLER 1300 10

14 rows selected.

4) To improve performance we use NOLOGGING..

CREATE table raj2(eno,emp_nm,salary,dep) NOLOGGING as SELECT empno,ename,sal,deptno from emp;

- 1 -

Page 2: SQL

SelectSyntax:

SELECT<col>,<col>,<col> ….FROM <tabname><tabname><tabname>…..;

It is a data query language or data retrieval language. * Represent only columns.

We also give like this…

SELECT all * from emp;

5) giving Elias names to coloms..

SELECT empno enumber,ename Name from emp;

Note:- Elias name must not in oracle verbs..

DistinctSyntax:

SELECT DISTINCT <col>,<col>,<col> ….FROM <tabname><tabname><tabname>…..;

To retrieve distinct rows from table. It is anci standard keyword.

We will also use UNIQUE .

6).Display all the unique departments of the employees.

SELECT UNIQUE deptno from emp;

7).Display all unique commissions of employees.

SELECT DISTINCT comm from emp;

8) Display all the unique DEP and JOB combinations

SELECT DISTINCT deptno,job from emp;

In above query first rows are selected on DEPTNO then it will go to the job. SELECT DISTINCT deptno,job from emp

DEPTNO JOB

10 CLERK

10 MANAGER

- 2 -

Page 3: SQL

10 PRESIDENT

20 ANALYST

20 CLERK

20 MANAGER

30 CLERK

30 MANAGER

30 SALESMAN

9 rows selected.

select distinct job,deptno from emp;

JOB DEPTNO

ANALYST 20

CLERK 10

CLERK 20

CLERK 30

MANAGER 10

MANAGER 20

MANAGER 30

PRESIDENT 10

SALESMAN 30

9 rows selected.

- 3 -

Page 4: SQL

Sorting:Syntax:

SELECT [distinct] <col><col><col>,…. FROM <table><table>….ORDER BY <col> [ASC/DESC],

<col> [ASC/DESC]…..;note:- default sort order is ASCENDING order.

9). Display details of all employees the DECENDING order of there employees salary.

SELECT * from emp order by sal desc;

10). Display the descending order of their experience.

SELECT * from emp ORDER BY hiredate;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7839 KING PRESIDENT  17-NOV-81 5000   10

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7934 MILLER CLERK 7782 23-JAN-82 1300   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7876 ADAMS CLERK 7788 23-MAY-87 1100   20

14 rows selected.

11). Display the deportment number job, emp no, name & salary on the ASCENDING order of deptno and DECENDING order of job.

SELECT deptno,job,empno,ename,sal from emp order by 1 asc,2 desc; (Or)SELECT deptno,job,empno,ename,sal from emp order by deptno asc,job desc;

DEPTNO JOB EMPNO ENAME SAL

10 PRESIDENT 7839 KING 5000

10 MANAGER 7782 CLARK 2450

- 4 -

Page 5: SQL

10 CLERK 7934 MILLER 1300

20 MANAGER 7566 JONES 2975

20 CLERK 7369 SMITH 800

20 CLERK 7876 ADAMS 1100

20 ANALYST 7788 SCOTT 3000

20 ANALYST 7902 FORD 3000

30 SALESMAN 7499 ALLEN 1600

30 SALESMAN 7654 MARTIN 1250

30 SALESMAN 7521 WARD 1250

30 SALESMAN 7844 TURNER 1500

30 MANAGER 7698 BLAKE 2850

30 CLERK 7900 JAMES 950

14 rows selected.

12). Display all the commissions in DESCENDING order.

SELECT comm From emp order by comm desc;

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.

SINGLE BYTE OVERHEADE:-

- 5 -

Page 6: SQL

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.

To diplay null values last…

SELECT comm From emp order by comm desc NULLS FIRST;

COMM

 

 

 

 

 

 

 

 

 

 

0

300

500

1400

13). Display all the unique deportment number in ascending order.

SELECT DISTINCT deptno from emp order by 1;

DEPTNO

10

20

30

- 6 -

Page 7: SQL

Where:

Syntax:

SELECT [distinct] <col><col><col>,…. FROM <table><table>….WHERE <condition>[ORDER BY <col> [ASC/DESC],

<col> [ASC/DESC]…..];

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 -

Page 8: SQL

EXAMPLES: 15). Display name, salary, daily salary, annual salary and experience of all the employees.

SELECT ename,sal,sal/30 dsal,sal*12 asal,(sysdate-hiredate)/365 exp from emp;ENAME SAL DSAL ASAL EXP

SMITH 800 26.6666667 9600 28.0832178

ALLEN 1600 53.3333333 19200 27.9051356

WARD 1250 41.6666667 15000 27.8996562

JONES 2975 99.1666667 35700 27.7928069

MARTIN 1250 41.6666667 15000 27.3023959

BLAKE 2850 95 34200 27.7133548

CLARK 2450 81.6666667 29400 27.6065055

SCOTT 3000 100 36000 21.7434918

KING 5000 166.666667 60000 27.1654096

TURNER 1500 50 18000 27.3571904

ADAMS 1100 36.6666667 13200 21.6503411

JAMES 950 31.6666667 11400 27.121574

FORD 3000 100 36000 27.121574

MILLER 1300 43.3333333 15600 26.981848

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).

16)display the details of all the managers

SELECT * from emp where job=’MANAGER’;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7566 JONES MANAGER 7839 02-APR-81 2975   20

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

sql > i (it use to add one more line to exist)

- 8 -

Page 9: SQL

17).display all the employees working from deportment 30.

SELECT * from emp where deptno=30;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7900 JAMES CLERK 7698 03-DEC-81 950   30

6 rows selected.

18) List all the emp’s working for the manager 7698.

SELECT * from emp where mgr=7698;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7900 JAMES CLERK 7698 03-DEC-81 950   30

19).list all the emp’s who join after 1980.

SELECT * from emp where hiredate > ’31-dec-1980’;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

- 9 -

Page 10: SQL

7839 KING PRESIDENT  17-NOV-81 5000   10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100   20

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7934 MILLER CLERK 7782 23-JAN-82 1300   10

13 rows selected.

20) list all the emp’s have an experience more then 27 years.

SELECT * from emp where (sysdate-hiredate)/365 >27;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7839 KING PRESIDENT  17-NOV-81 5000   10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

11 rows selected.

21) display all the Clarks and salesman.

SELECT * from emp where job=’CLERK’ OR job=’SALESMAN’; ORSELECT * from emp where job IN (’CLERK’, ’SALESMAN’);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100   20

7900 JAMES CLERK 7698 03-DEC-81 950   30

7934 MILLER CLERK 7782 23-JAN-82 1300   10

- 10 -

Page 11: SQL

8 rows selected.

22).Display a detail’s of ‘FORD’,’JAMES’, ‘SMITHA’,’DAMS KING’ on descending order of salary.

SELECT * from emp where ename in (‘FORD’,’JAMES’, ‘SMITHA’,’DAMS KING’) order by ename desc;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

23).Display all the employees join in the year 1980.

SELECT * from emp where hiredate between ’01-jan-1980’ and ’31-dec-1980’;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

24).Display the employees receiving sal from 1250 up to 3000. SELECT * from emp where sal between 1250 and 3000;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7934 MILLER CLERK 7782 23-JAN-82 1300   10

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.

SELECT * from emp where hiredate between '01-jul-1981' and '30-jun-1982';

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

- 11 -

Page 12: SQL

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7839 KING PRESIDENT  17-NOV-81 5000   10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7934 MILLER CLERK 7782 23-JAN-82 1300   10

26).Display all the employees how’s annual salary is ranging from 18000 to 42000.

SELECT * from emp where sal*12 between 18000 and 24000;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

27).Display all the emp’s working for manager.

SELECT * from emp where mgr is NOT NULL;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100   20

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7934 MILLER CLERK 7782 23-JAN-82 1300   10

13 rows selected.

28) Display all the five characters name.

SELECT ename from emp where ename LIKE‘-----‘;

Like:The delimiter is used is used like character are: ‘_’ represent a single charactor. % to represent a group of characters.

- 12 -

Page 13: SQL

IS NULL to identify the null values. IS NOT NULL to identify not null values.

29).select the names starting with chars ‘S’.

select ename from emp where ename like ‘A%’;

ENAME

ALLEN

ADAMS

30).list all the names joining a January.

select * from emp where hiredate like '%JAN%';

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7934 MILLER CLERK 7782 23-JAN-82 1300   10

31).display all the emp’s joined in month which secound character is ‘E’.

select * from emp where hiredate like '%_E_%';

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

Note:

_% = %_ = %

Key:Sql> C/J/E means Change where J replace with E.

32).List all the emp’s of 1980.select * from emp where hiredate like '%80';

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800  

33).display all the managers working for some other managers.

- 13 -

Page 14: SQL

Select * from emp where job=’MANAGER’ and mgr is NOT NULL;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7566 JONES MANAGER 7839 02-APR-81 2975   20

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-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;

ENAME SAL DEPTNO JOB ASAL

SMITH 800 20 CLERK 9600

JONES 2975 20 MANAGER 35700

SCOTT 3000 20 ANALYST 36000

ADAMS 1100 20 CLERK 13200

FORD 3000 20 ANALYST 36000

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.

select * from emp where(sysdate-hiredate)/365 >20 and(sysdate-hiredate)/365 <30 and comm is null order by sal;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

7900 JAMES CLERK 7698 03-DEC-81 950   30

7876 ADAMS CLERK 7788 23-MAY-87 1100   20

7934 MILLER CLERK 7782 23-JAN-82 1300   10

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7839 KING PRESIDENT  17-NOV-81 5000   10

Note: hear we not use BETWEEN for selecting experience bcz it was not including 20 and 30.Between is for including 20 and 30.

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.

select * from emp

- 14 -

Page 15: SQL

where hiredate between '01-jan-1981' and '31-dec-1981' andhiredate not like '0%' andhiredate not like '%_A%' andjob not in('PRESIDENT','CLARK');

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

- 15 -

Page 16: SQL

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 )

function are two types:1)single function:- a function witch process 1 row at a time and return a value .2)group function :- a function witch process a set of rows at a time and return a single values .

these functions based an the data types divided into 4 types :1) number function or airthematic or scientific.2) character function or string manipulation function.\3)date function.4)data convert function or TYPE COST function.

ARITHEMATIC FUNCTION:

a).ABC(number) :- it returns unsigned value.

Select abs(-34343) from dual;

ABS(-34343)

34343

b).ROUND():-

select round(345.126,2) from dual;

ROUND(345.126,2)

345.13

In above 345.126 is rounded up to 2 decimals it was 345.13 but if we give 345.124 it will round as 345.12.

select round(345.124,2) from dual;

ROUND(345.124,2)

345.12

We also round the integers..

Select round(51,-2) from dual;

- 16 -

Page 17: SQL

ROUND(50,-2)

100

Select round(49,-2) from dual;

ROUND(49,-2)

0

TRUNC():-

Select trunc(1234.5678,3) from dual;

TRUNC(1234.5678,3)

1234.567

Select trunc(1234,-3) from dual;

TRUNC(1234,-3)

1000

Select trunc(1999,-3) from dual;

TRUNC(1999,-3)

1000

CEIL():- It rounding only decimal points. It always round to upper value.

Select ceil(1234.0000001) from dual;CEIL(1234.0000001)

1235

Select CEIL(1234.99999) from dual;CEIL(1234.99999)

1235

FLOOR():-

Select floor(1234.00001) from dual;

FLOOR(1234.00001)

1234

- 17 -

Page 18: SQL

SQRT():-

Select sqrt(25) from dual;

SQRT(25)

5

MOD():-

Select mod(45,10) from dual;

MOD(45,10)

5

POWER():-

Select power(5,2) from dual;

POWER(5,2)

25

SIGN():- It returns –1 if it is negative otherwise it returns 0. Select sign(100),sign(0),sign(-100) from dual;

SIGN(100) SIGN(0) SIGN(-100)

1 0 -1

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.

Select greatest(1,12,35) from dual;

GREATEST(1,12,35)

35

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 -

Page 19: SQL

This function works on any parameters belongs to same data type.Example:select nvl(comm,0) from emp;

and we also have function LOG(),LN(),EXP(),SIN(),COS(),TAN()

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) expfrom emp;

EMPNO ENAME SAL ASAL DSAL EXP

7369 SMITH 800 9600 27 28.08

7499 ALLEN 1600 19200 53 27.9

7521 WARD 1250 15000 42 27.9

7566 JONES 2975 35700 99 27.79

7654 MARTIN 1250 15000 42 27.3

7698 BLAKE 2850 34200 95 27.71

7782 CLARK 2450 29400 82 27.6

7788 SCOTT 3000 36000 100 21.74

7839 KING 5000 60000 167 27.16

7844 TURNER 1500 18000 50 27.35

7876 ADAMS 1100 13200 37 21.65

7900 JAMES 950 11400 32 27.12

7902 FORD 3000 36000 100 27.12

7934 MILLER 1300 15600 43 26.98

37).display all salaries divided by 100.

Select * from emp where mod(sal,100)=0;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7839 KING PRESIDENT  17-NOV-81 5000   10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100   20

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7934 MILLER CLERK 7782 23-JAN-82 1300   10

- 19 -

Page 20: SQL

CHARACTER FUNTION:

UPPER():-

Select upper('rajender') from dual;UPPER('R

RAJENDER

LOWER():

Select lower(‘RAJENDER’) from dual;LOWER('R

rajender

INITCAP(): The first letter of the word convert into upper case.

Select initcap('rajender mca') from dual;INITCAP('RAJ

Rajender Mca

ASCII():

Select ascii(‘A’) from dual;ASCII('A')

65

Select ascii('ABCDEF') from dual;ASCII('A')

65

So it converts only first char as ASCII.

CHR():

It convert ASCII as character.Select chr(65) from dual;

C

A

SUBSTR(): It require three parameters

1).String 2).which position u want..3).how many characters u want..

- 20 -

Page 21: SQL

Select substr(‘ameerkpet’,6) from dual;SUBS

kpet

Select substr(‘ameerpet’,2,4) from dual;SUBS

meer

Select substr('ameerpet',-5) from dual;SUBST

erpet

It counts chars from reverse order.Select substr('ameerpet',-5,2) from dual;

SU

er

It counts chars from reverse order and select 2 chars from starting.

INSTR(): It require four parameters

1).String 2).searching char

3).from which position 4).number of occurred position of that latter(ie.1st ,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 2nd possion.select * from emp where instr(ename,'L',3,2)>0;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7934 MILLER CLERK 7782 23-JAN-82 1300   10

LENGTH():

Select length(‘ORACLE’) from dual;LENGTH('ORACLE')

6

LPAD(): It pads from left side. It require three parameters string or number.

- 21 -

Page 22: SQL

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

Select ltrim('AAAAAAMEERPER','A') from dual;LTRIM('

MEERPER

Select ltrim('AAAAAAMEERPER','AME') from dual;LTRI

RPER

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 -

Page 23: SQL

this is myhome…

Select 'this is '||'orcle class' from dual;'THISIS'||'ORCLECLA

this is orcle class

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 parameters1).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 -

Page 24: SQL

CLERK C

CLERK C

ANALYST UNKNOUN JOB

CLERK C

14 rows selected

We also use CASE statement.

Select job,case jobWhen ’CLERK’ then ’C’, when ‘MANAGER’ then ’M’, when ‘SALESMAN’ then ’S’, else ‘UNKNOUN JOB’end job_codefrom emp;

JOB JOB_CODE

CLERK C

SALESMAN S

SALESMAN S

MANAGER M

SALESMAN S

MANAGER M

MANAGER M

ANALYST UNKNOUN JOB

PRESIDENT UNKNOUN JOB

SALESMAN S

CLERK C

CLERK C

ANALYST UNKNOUN JOB

CLERK C

14 rows selected.

Note: hear job-code is alias name.

38).display all six character named employees.Select ename from emp where length(ename)=6;

ENAME

MARTIN

TURNER

MILLER

- 24 -

Page 25: SQL

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

7566 JONES MANAGER 7839 02-APR-81 2975   20

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

40).display enames having charact AEIOU IN SECOND position.Select ename from emp where substr(ename,2,1) in(‘A’,’E’,’I’,’O’,’U’);

ENAME

WARD

JONES

MARTIN

KING

TURNER

JAMES

FORD

MILLER

8 rows selected

41)Display details of all emp’s of following formate.Mr.smith is working with the dept 10 for the manager(mgrno) with a salary Rs.1250 since 81. select concat('Mr.', concat(initcap(ename), concat('is worcking for the dept ', concat('deptno', concat(' for the manager ', concat(mgr, concat(' as ', concat(upper(job), concat(' with a salary Rs. ', concat(sal, concat(' since ',substr(hiredate,8))))))))))) ) emp_details from em;

EMP_DETAILS

Mr.Smithis working for the dept deptno for the manager 7902 as CLERK with a salary Rs. 800 since 80

Mr.Allenis worcking for the dept deptno for the manager 7698 as SALESMAN with a salary Rs. 1600 since 81

Mr.Wardis worcking for the dept deptno for the manager 7698 as SALESMAN with a salary Rs. 1250 since 81

- 25 -

Page 26: SQL

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.

42)encrypt all the employees names with special chares.

Data conversion function1) IMPLECIT CONVERCTION:

when oracle convert the data in one format to other then it is called as implicit converction 2) EXPLICIT CONVERCTION:-

these are three types.. To_char() To_ number() To_date()

To_char():- To convert number into character. To covert date into character.

Formate:To_char(number[,’FORMATE]) To_char(date[,’format’])

Select to-char(1234) from dual;Vsize():- virtual memory function

This function is to maser the memory in bytes.

- 26 -

Page 27: SQL

This function accept any data type required sing paramitor.

Select vsize(123456),vsize(to_char(123456)) from dual;VSIZE(123456) VSIZE(TO_CHAR(123456))

4 6

Select to_char(123456,’9999999.99’) from dual;TO_CHAR(1234

123456.00

Select to_char(sysdate) from dual;TO_CHAR(S

10-JAN-09

Select to_char(sysdate,’mm/dd/yyyy’) from dual;TO_CHAR(SY

01/10/2009

To number():-Select to_number(‘123’) from dual;

TO_NUMBER('123')

123

Select to_number(‘123456.00’,999999.99) from dual;TO_NUMBER('123456.00',999999.99)

123456

Select to_number(‘123456’,999999.99) from dual;ORA-00939: too many arguments for function(it gives error).

To_date:-

Select to_date(‘10123495’,’mm/dd/yyyy’) from dual;TO_DATE('

12-OCT-95

Date function:-

Months _between(date,date)

It gives the difference between the months.

- 27 -

Page 28: SQL

Select months_between(sysdate,’09-jan-2000’) from dual;MONTHS_BETWEEN(SYSDATE,'09-JAN-2000')

108.045612

Select abs(months_between('09-jan-2000','09-mar-2008')) from dual;ABS(MONTHS_BETWEEN('09-JAN-2000','09-MAR-2008'))

98

Add_months(date,number)

Add no.of months to given date. Year must be -4713 and +9999 butnot ‘0’(zero).

Select add_months(sysdate,12) from dual;ADD_MONTH

10-JAN-10

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():

Select greatest(’09-jan-2000’,’30-mar-1200’,’23-apr-1980’) from dual;GREATEST('0

30-mar-1200

The above query(v(’09-jan-2000’) is not date it is characters.The first one parameter data type is the measure of other data type.

Select greatest(sysdate,'09-feb-3000',sysdate,'09-jan-1200') from dual;GREATEST(

09-FEB-00

- 28 -

Page 29: SQL

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)

42) Select empno,ename,sal,sal/last_day(sysdate) ds-sal from emp where job=’SALESMAN’) ;

ORA-00932: inconsistent datatypes: expected NUMBER got DATE Above last-day(sysdate) is returning a date which we are dividing with

number so this we convert into number or char.

Select empno,ename,sal,sal/to_number(substr(last_day(sysdate),1,2)) ds_sal from emp where job='SALESMAN';

(or)Select empno,ename,sal,sal/to_char(substr(last_day(sysdate),1,2)) ds_sal from emp where job='SALESMAN';

EMPNO ENAME SAL DS_SAL

7499 ALLEN 1600 51.6129032

7521 WARD 1250 40.3225806

7654 MARTIN 1250 40.3225806

7844 TURNER 1500 48.3870968

43) Daily salary of previous month.

Select empno,ename,salSal/(to_char(last_day(add_months(sysdate,-1),’dd’))) d_sal from emp;

EMPNO ENAME SAL DSAL

7369 SMITH 800 25.8064516

7499 ALLEN 1600 51.6129032

7521 WARD 1250 40.3225806

7566 JONES 2975 95.9677419

7654 MARTIN 1250 40.3225806

7698 BLAKE 2850 91.9354839

7782 CLARK 2450 79.0322581

7788 SCOTT 3000 96.7741935

7839 KING 5000 161.290323

7844 TURNER 1500 48.3870968

7876 ADAMS 1100 35.483871

7900 JAMES 950 30.6451613

7902 FORD 3000 96.7741935

- 29 -

Page 30: SQL

7934 MILLER 1300 41.9354839

14 rows selected

44) display total no.of remaining day in the month after current date.Selectto_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

45)find the total no.of days of current year.46) find the total no.of days of any given year.

Least():-

DATE FORMATES:

d - for day of week.dd - for dateddd - for day of the year dy - week day in character from (mon,tue..)

- 30 -

Page 31: SQL

day - it week day in character from (Monday,Tuesday,…)

select to_char(sysdate,'d dd ddd dy day') from dual;TO_CHAR(SYSDATE,'DDDDDDDYDAY'

7 10 010 sat saturday

MONTH FORMATES:

select to_char(sysdate,'mon month mm rm Rm') from dual;TO_CHAR(SYSDATE,'MONMONTHMMR

jan january 01 i I

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

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.

Select to_char(to_date('09-jan-2100'),'am pm') from dual;TO_CH

am am

Select to_char(sysdate,'ad bc a.d. b.c.') from dual;TO_CHAR(SYSDATE

ad ad a.d. a.d.

Note: ‘a.d. b.c.’ for this we must give the dots(.) at end.

- 31 -

Page 32: SQL

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;

W -- week of the month.WW -- week of the year.Q -- quarter of the year.J -- it gives the number of days between 1-jan-4413 BC from current date.Iw -- Select to_char(to_date('25-dec-2009'),'w ww') from dual;

TO_C

4 52

Select to_char(to_date('25-dec-2009'),'q') from dual;

T

4

Select to_char(to_date('25-dec-2009'),'j') from dual;TO_CHAR(TO_DATE('25-DEC-2009'),'J')/365

6726.55068

Select to_char(to_date('25-dec-2009'),'iw i iy iyy iyyy iyyyy') from dual;TO_CHAR(TO_DATE('25-DEC

52 9 09 009 2009 20099

47)display current date in folling formate.Saturday 10th January 2009 ad.

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 -

Page 33: SQL

saturday 10th january 2009 ad

48)display current date in folling formate:saturday the 10th of january 2009 12:32::42

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

select * from empwhere to_char(hiredate,'q') in (1,4) and to_char(hiredate,'yyyy')in(1981,1988);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7839 KING PRESIDENT  17-NOV-81 5000   10

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

50).list all the employees joined on Sunday.

Select * from empWhere to_char(hiredate,’dy’)=’SUN’;

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…

Select * from empWhere rtrim(to_char(hiredate,'fmday'))='sunday';

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

Also we useFM:- Full Mode.Select * from empWhere to_char(hiredate,'fmday')='sunday';

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

- 33 -

Page 34: SQL

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

FOREIGN KEY( reference constraint ) Reference constraints is to define a relationship between the columns of different tables and even between the columns of same table(self reference).

Reference constraint is to implement normalization. Reference constraints define the table as a dependent table or in child

table or detailed table. Reference constraints is defines the column as a foreign key column. A foreign key always refers to the primary key of some other table or in

some table.Note:option it refers to unique dey column.The table to which the foreign key referring is called master table. The clause those used along with reference constraints are

i) Foreign keyii) On delete cascadeiii) On delete set null

i) foreign key : it is optional in the case of column level constraints but it mandate for table level constraint.

ii) On delete cascade: it is a permission to oracle to delete the data from master table those it is having dependent information. Oracle delete both master and child data for specified master row.

iii) on delete set null: it is also a permission or a pre-Image to oracle to delete master record without deleting it’s corresponding child

record .but it update all the foreign key value of the delete master recodes to nulls

create table dept1(deptno number(2) primary key,dname varchar2(10) not null,loc varchar2(10) not null

);insert into dept1 values(30,'fin','ban');

DEPTNO DNAME LOC

10 acc hyd

20 acc hyd

30 fin ban

- 34 -

Page 35: SQL

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

1 ravi 3000 CLEARK 10

2 raJU 4000 CLEARK 20

Page no 41 to 51 pending …………….

- 35 -

Page 36: SQL

TABLES

Emp:EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7839 KING PRESIDENT  17-NOV-81 5000   10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100   20

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7934 MILLER CLERK 7782 23-JAN-82 1300   10

14 rows selected.

Dept :DEPTNO DNAME LOC

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

Salgrad:GRADE LOSAL HISAL

1 700 1200

2 1201 1400

3 1401 2000

4 2001 3000

5 3001 9999

- 36 -

Page 37: SQL

JOINSDisplay the details of the jems.

Select * from emp where ename=’JAMES’;Note: the above query based on ‘equvi join.

The above query is not based on no join bcz the quary is not based on single table.

Display the location where james is working.

Select loc from emp,dept where ename=’JAMES’;Note: the above query os based on equvi join.

Hear we not comparing colums of bouth the tables.Select dept.loc from emp,dept where emp.ename=’JAMES’ and dept.deptno=emp.deptno;

LOC

CHICAGO

Elias:Select d.loc from emp e,dept d where e.ename=’JEMS’ and d.deptno=e.deptno;

51).diplay all the emps of sales deportment.

Select e.* from emp e,dept d where d.dname='SALES' and d.deptno=e.deptno;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7900 JAMES CLERK 7698 03-DEC-81 950   30

6 rows selected.

52) display the all the salesman’s of sales deportment.

Select e.* from emp e,dept d where d.dname='SALES'and e.job='SALESMAN'and

- 37 -

Page 38: SQL

d.deptno=e.deptno;EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7844 TURNER SALESMAN 7698 08-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;

EMPNO

ENAME JOB MGR HIREDAT

E SAL

COMM

DEPTNO

LOC EXP

7369 SMITH CLERK 7902 17-DEC-80

800   20 DALLAS 854.29778

9

7499 ALLEN SALESMAN

7698 20-FEB-81

1600 300 30 CHICAGO

848.881123

7521 WARD SALESMAN

7698 22-FEB-81

1250 500 30 CHICAGO

848.714456

7566 JONES MANAGER 7839 02-APR-81

2975   20 DALLAS 845.46445

6

7654 MARTIN SALESMAN

7698 28-SEP-81

1250 1400 30 CHICAGO

830.547789

7698 BLAKE MANAGER 7839 01-MAY-81

2850   30 CHICAGO

843.047789

7782 CLARK MANAGER 7839 09-JUN-81

2450   10 NEW YORK

839.797789

7788 SCOTT ANALYST 7566 19-APR-87

3000   20 DALLAS 661.46445

6

7839 KING PRESIDENT

 17-NOV-81

5000   10 NEW YORK

826.381123

7844 TURNER

SALESMAN

7698 08-SEP-81

1500 0 30 CHICAGO

832.214456

7876 ADAMS CLERK 7788 23-MAY-87

1100   20 DALLAS 658.63112

3

7900 JAMES CLERK 7698 03-DEC-81

950   30 CHICAGO

825.047789

7902 FORD ANALYST 7566 03-DEC-81

3000   20 DALLAS 825.04778

9

7934 MILLER CLERK 7782 23-JAN-82 1300   10 NEW YORK

820.797789

14 rows selected. 53)display the managers of all the location in the descending order of their salaries.

Select e.*,d.loc,(sysdate-e.hiredate)/12 exp from emp e,dept d

- 38 -

Page 39: SQL

wheree.job='MANAGER'and d.deptno=e.deptno order by e.sal;

EMPNO ENAM

E JOB MGR

HIREDATE

SAL COMM DEPTN

O LOC EXP

7782 CLARK MANAGER

7839 09-JUN-81 2450   10 NEW YORK

839.798862

7698 BLAKE MANAGER

7839 01-MAY-81

2850   30 CHICAGO

843.048862

7566 JONES MANAGER

7839 02-APR-81 2975   20 DALLAS 845.46552

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.

select e.* from emp e,dept dwhere e.job in ('MANAGER','ANALYST') and d.dname in('ACCOUNTING','RESERCH') and(e.comm is null or e.comm=0) andmonths_between(sysdate,e.hiredate)/12 between 27 and 29 and e.deptno=d.deptno;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

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;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO GRADE

7369 SMITH CLERK 7902 17-DEC-80 800   20 1

7876 ADAMS CLERK 7788 23-MAY-87 1100   20 1

7900 JAMES CLERK 7698 03-DEC-81 950   30 1

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 2

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 2

7934 MILLER CLERK 7782 23-JAN-82 1300   10 2

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 3

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 3

7566 JONES MANAGER 7839 02-APR-81 2975   20 4

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30 4

7782 CLARK MANAGER 7839 09-JUN-81 2450   10 4

7788 SCOTT ANALYST 7566 19-APR-87 3000   20 4

7902 FORD ANALYST 7566 03-DEC-81 3000   20 4

7839 KING PRESIDENT  17-NOV-81 5000   10 5

14 rows selected.

- 39 -

Page 40: SQL

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

7566 JONES MANAGER 7839 02-APR-81 2975   20 4

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30 4

7782 CLARK MANAGER 7839 09-JUN-81 2450   10 4

57)display all grade 2 employees of 1981.select e.* from emp e,salgrade s where s.grade=2 and to_char(e.hiredate,'yyyy')=1981 and e.sal between s.losal and s.hisal;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

58) display employes number and name salary,daily salary grade in terms of *’slocation of all the employees.select e.empno,e.ename,e.sal,decode(s.grade,1,'*',2,'**',3,'***',4,'****',5,'*****') gradefrom emp e,dept d,salgrade swhere e.deptno=d.deptno ande.sal between s.losal and s.hisal;

EMPNO ENAME SAL GRADE

7369 SMITH 800 *

7876 ADAMS 1100 *

7900 JAMES 950 *

7521 WARD 1250 **

7654 MARTIN 1250 **

7934 MILLER 1300 **

7499 ALLEN 1600 ***

7844 TURNER 1500 ***

7566 JONES 2975 ****

7698 BLAKE 2850 ****

7782 CLARK 2450 ****

7788 SCOTT 3000 ****

7902 FORD 3000 ****

7839 KING

59)display all the grade of 4 and 5 employees of accounting and readerch department select e.* from emp e,salgrade s,dept dwhere s.grade in (4,5)and d.dname in('ACCOUNTING','RESERCH') ande.deptno=d.deptno and e.sal between s.losal and s.hisal;

- 40 -

Page 41: SQL

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7839 KING PRESIDENT  17-NOV-81 5000   10

60)display empno,name,sal,grade deptname of all emps those how belongs to first three grads and belongs to sales departments receiving some commission or those how belongs to grades 4 and 5 working with location with NEW YORK with annual salary more then 30000 without commission working for some manager whose number is 3rd digit should not be divisible by 2 or 4.

Select e.eno,e.name,e.sal,s.grade,e.deptno from emp e,dept d,salgrade sWhere (

s.grade in (1,2,3) andd.name='SALES'AND(e.comm is not null or e.comm<>0)

)--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 ormod (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

7499 ALLEN 1600 1 30

7521 WARD 1250 1 30

7654 MARTIN 1250 1 30

7844 TURNER 1500 1 30

7499 ALLEN 1600 2 30

7521 WARD 1250 2 30

7654 MARTIN 1250 2 30

7844 TURNER 1500 2 30

7499 ALLEN 1600 3 30

7521 WARD 1250 3 30

7654 MARTIN 1250 3 30

7844 TURNER 1500 3 30

7782 CLARK 2450 4 10

13 rows selected.

- 41 -

Page 42: SQL

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. Select a.* from emp a,emp b where b.ename=’JEMES’ and a.sal>b.sal;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7839 KING PRESIDENT  17-NOV-81 5000   10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100   20

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7934 MILLER CLERK 7782 23-JAN-82 1300   10

12 rows selected.

62) display the details of all the employees hose job is same as BLACK.

Select a.* from emp a,emp b where a.job=b.job and b.ename='BLAKE';EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7566 JONES MANAGER 7839 02-APR-81 2975   20

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

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

7369 SMITH CLERK 7902 17-DEC-80 800   20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

- 42 -

Page 43: SQL

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7844 TURNER SALESMAN 7698 08-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

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

8 rows selected.

NEW MODELS FOR JIONS:Display the employees number,name,sal,deptname of all the employees.Select emp e inner join dept d on (e.deptno=d.deptno);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DEPTNO DNAME LOC

7369 SMITH CLERK 7902 17-DEC-80 800   20 20 RESEARCH DALLAS

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 30 SALES CHICAGO

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 30 SALES CHICAGO

7566 JONES MANAGER 7839 02-APR-81 2975   20 20 RESEARCH DALLAS

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 30 SALES CHICAGO

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30 30 SALES CHICAGO

7782 CLARK MANAGER 7839 09-JUN-81 2450   10 10 ACCOUNTING NEW YORK

7788 SCOTT ANALYST 7566 19-APR-87 3000   20 20 RESEARCH DALLAS

7839 KING PRESIDENT  17-NOV-81 5000   10 10 ACCOUNTING NEW YORK

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 30 SALES CHICAGO

7876 ADAMS CLERK 7788 23-MAY-87 1100   20 20 RESEARCH DALLAS

7900 JAMES CLERK 7698 03-DEC-81 950   30 30 SALES CHICAGO

7902 FORD ANALYST 7566 03-DEC-81 3000   20 20 RESEARCH DALLAS

7934 MILLER CLERK 7782 23-JAN-82 1300   10 10 ACCOUNTING NEW YORK

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 -

Page 44: SQL

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

7369 SMITH 800 1

7876 ADAMS 1100 1

7900 JAMES 950 1

7521 WARD 1250 2

7654 MARTIN 1250 2

7934 MILLER 1300 2

7499 ALLEN 1600 3

7844 TURNER 1500 3

7566 JONES 2975 4

7698 BLAKE 2850 4

7782 CLARK 2450 4

7788 SCOTT 3000 4

7902 FORD 3000 4

7839 KING 5000 5

Display the salesmans of the sales dept in the desc order of there salaries.

Select e.*,d.dname from emp e inner join dept d on (e.deptno=d.deptno) where e.job='SALESMAN'and d.dname='SALES' order by e.sal desc;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO DNAME

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 SALES

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 SALES

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 SALES

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 SALES

Display the empno,ename,sal,grade,loc of the all the emp’s.

Select e.empno,e.ename,e.sal,s.grade,d.loc from (emp e inner join dept d on(e.deptno=d.deptno)) inner join salgrade s on(e.sal between s.losal and s. hisal);

EMPNO ENAME SAL GRADE LOC

7369 SMITH 800 1 DALLAS

7876 ADAMS 1100 1 DALLAS

7900 JAMES 950 1 CHICAGO

7521 WARD 1250 2 CHICAGO

7654 MARTIN 1250 2 CHICAGO

7934 MILLER 1300 2 NEW YORK

7499 ALLEN 1600 3 CHICAGO

7844 TURNER 1500 3 CHICAGO

7566 JONES 2975 4 DALLAS

7698 BLAKE 2850 4 CHICAGO

7782 CLARK 2450 4 NEW YORK

- 44 -

Page 45: SQL

7788 SCOTT 3000 4 DALLAS

7902 FORD 3000 4 DALLAS

7839 KING 5000 5 NEW YORK

Display all the managers of grade 4 working for NEWYARK,CHICAGO.

Select e.*,s.grade,d.loc from (emp e inner join dept d on(d.deptno=d.deptno) inner join salgrade s on(e.sal between s.losal and hisal))where s.grade =4 aND e.job='MANAGER' AND d.loc in('NEW YORK','CHICAGO');

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO GRADE LOC

7566 JONES MANAGER 7839 02-APR-81 2975   20 4 NEW YORK

7566 JONES MANAGER 7839 02-APR-81 2975   20 4 CHICAGO

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30 4 NEW YORK

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30 4 CHICAGO

7782 CLARK MANAGER 7839 09-JUN-81 2450   10 4 NEW YORK

7782 CLARK MANAGER 7839 09-JUN-81 2450   10 4 CHICAGO

SUB QUERY’S:-

It is alternative way of defining self join. As if query (Q1) retrieve the data from a table based on some

condition ,and that codiction gers the input from condition gets from the same table based on another query (Q2).then Q2 is called as sub query Q1 is main query.

Always sub query afe enclosed within parenthesis always subquerys are executed first. Maximum of 255 sub querys are one another can be defined always inner most sub-query executed first the propagation is always from inner most to the outer.

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.

Select * from emp e,dept d where d.dname='ACCOUNTING' ande.job=(select job from emp where ename ='JONES') and e.deptno=d.deptno;

EMPNO

ENAME

JOB MGR

HIREDATE

SAL COM

M DEPTN

O DEPTN

O DNAME LOC

7782 CLARK

MANAGER

7839 09-JUN-81

2450

  10 10 ACCOUNTING

NEW YOR

- 45 -

Page 46: SQL

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 empWhere job in(

select distinct e.job from emp e,dept d where d.dname='RESEARCH' ande.deptno=d.deptno ) anddeptno<>(select deptno from dept where dname='RESEARCH');

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7934 MILLER CLERK 7782 23-JAN-82 1300   10

7900 JAMES CLERK 7698 03-DEC-81 950   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

67) display employees details of employees of both ACCOUNTING and sales deportment where the employees of grade 4.Select e.* from emp e,dept dWhere d.dname in ('ACCOUNTING','SALES') and e.job In(

select distinct e.job from emp e,salgrade s where s.grade=2 and e.sal between s.losal and s.hisal) and e.deptno=d.deptno;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7934 MILLER CLERK 7782 23-JAN-82 1300   10

7900 JAMES CLERK 7698 03-DEC-81 950   30

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

68) display details of all emp’s are those who belongs to deportment where ADAMS,MANGARS MANAGERS MANAGER is working.

Select * from empWhere deptno =(

select deptno from empwhere empno=(

- 46 -

Page 47: SQL

select mgr from empwhere empno=(

select mgr from empwhere empno= (select mgr from emp where

ename='ADAMS'))));

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7839 KING PRESIDENT  17-NOV-81 5000   10

7934 MILLER CLERK 7782 23-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.

To solve above query we will see this…… Display the details of the all the employees deportment 10 whose salaries

is more then any employees of department 20.Select * from emp where ( ( select sal from emp where deptno=10)>(select sal from emp where deptno=20));

ORA-01427: single-row subquery returns more than one row

To solve above we use FILLERS.>ALL >5000<ALL <1300

>ANY >1300<ANY <5000

=ANY =1300 OR =2400 OR =5000 IN(1300,2450,5000)=ALL =1300 OR =2400 OR =5000

select sal from emp where deptno=10 and sal> any(select sal from emp where deptno=20);

70) select details of deportment 10 and 30 whose salaries are more then the salary of all the employees of deportment 20.

- 47 -

Page 48: SQL

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

7839 KING 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

7369 SMITH CLERK 7902 17-DEC-80 800   20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7839 KING PRESIDENT  17-NOV-81 5000   10

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7876 ADAMS CLERK 7788 23-MAY-87 1100   20

7900 JAMES CLERK 7698 03-DEC-81 950   30

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7934 MILLER CLERK 7782 23-JAN-82 1300   10

14 rows selected.

72) display details of all employees of FORUQselect * from emp where not exists(select * from emp where ename=’FORUQ’);

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.

Max,min and count function work on any data type but sum and avg work on only number data type.

- 48 -

Page 49: SQL

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

74) Display the highest salary of grade ‘4’.Select max(e.sal) from emp e,salgrade s s.hisal where s.grade=4 and e.sal between s.losal and s.hisal;

MAX(E.SAL)

3000

75) Display all the details of highest paid employees of ‘RESEARCH’ deportment.Select * from empWhere sal=( select max(e.sal) 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

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7902 FORD ANALYST 7566 03-DEC-81 3000   20

Note: hear in sub query gives max sal of RESEARCH deportment,one more time we checking for research deportment way bcz if not give that select statement select all the rows which have greatest sal of emp but we want only REASERCH dept.2.min()

Is to find the minimum of column value.

76) Display the details of the senior most employeesSelect * from emp where hiredate=(select min(hiredate) from emp);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

77) display the lowest paid employee working under ‘BLAKE’.Select * from empWhere sal =(

Select min (sal) from emp where mgr= (select empno from emp where ename=’BLAKE’)) andMgr= (select empno from emp where ename =’BLAKE’);

- 49 -

Page 50: SQL

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7900 JAMES CLERK 7698 03-DEC-81 950   30

3 sum (<col name>)

Is to find the total all column values.Display the total experience of all the mangers.Select sum (months_between(sysdate,hiredate)/12) exp from emp where job=’MANAGER’;

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

79).CONVERT NUMBERS INTO WORDS.Select to_char(to_date(round(sum(months_between(sysdate,hiredate)/12)),'j'),'jsp') total_exp from emp where empno in( select distinct mgr from emp);

TOTAL_EXP

one hundred fifty-nine

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

80)To display the avg of all the analyst.Select avg(sal) from emp where job=’ANALIST’;

81) Display the details of the all the employees hose salary is more then the avg sal of all the emp’s.

Select * from emp where sal>(select avg(sal) from emp);

- 50 -

Page 51: SQL

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7566 JONES MANAGER 7839 02-APR-81 2975   20

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7839 KING PRESIDENT  17-NOV-81 5000   10

7902 FORD ANALYST 7566 03-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

7566 JONES MANAGER 7839 02-APR-81 2975   20

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7839 KING PRESIDENT  17-NOV-81 5000   10

7902 FORD ANALYST 7566 03-DEC-81 3000   20

6 rows selected.

5) COUNT (<col name>)

Is to count the number of rows in the given set or given column.

83) Count the number of tables in the emp table.Select count(empno) from emp;

84)display the number of employees join in 1981.Select count(empno) from emp where to_char(hiredate,’yyyy’)=1981

COUNT(EMPNO)

10

85) count the number of employees belongs to grade 2.

Select count(*) from emp e,salgrade s where s.grade=2 and e.sal between s.losal and s.hisal;

COUNT(*)

3

- 51 -

Page 52: SQL

GORUP BY CLAUSE:

Select [distinct] <col>,<col>,…From <tab>,<tab>,…..[where <condation>]GROUP BY <col>,<col>,…[order by <col>,[asc/desc],<col>…]

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.

86) display the number of employees belongs to each year.Select to_char(hiredate,’yyyy’) year, count(*) no_of_emps from empGROUP BY to_char(hiredate,’yyyy’);

87) display max,min salary of each location grade wise..select d.loc,s.grade,max(e.sal),min(e.sal) from emp e,dept d, salgrade swhere d.deptno=e.deptno and e.sal between s.losal and s.hisal group by d.loc,s.grade;

LOC GRADE MAX(E.SAL) MIN(E.SAL)

DALLAS 1 1100 800

DALLAS 4 3000 2975

CHICAGO 1 950 950

CHICAGO 2 1250 1250

CHICAGO 3 1600 1500

- 52 -

Page 53: SQL

CHICAGO 4 2850 2850

NEW YORK 2 1300 1300

NEW YORK 4 2450 2450

NEW YORK 5 5000 5000

9 rows selected.

88) display number of Clark’s working for each deportment.Select deptno,count(*) from empWhere job='CLERK' group by deptno;

DEPTNO COUNT(*)

10 1

20 2

30 1

89) select to_char(hiredate,'yyyy') year, to_char(hiredate,'mm') month,count(*) no_of_empsfrom empgroup by rollup(to_char(hiredate,'yyyy'),to_char(hiredate,'mm'));

YEAR MO NO_OF_EMPS

1980 12 1

1980   1

1981 02 2

1981 04 1

1981 05 1

1981 06 1

1981 09 2

1981 11 1

1981 12 2

1981   10

1982 01 1

1982   1

1987 04 1

1987 05 1

YEAR MO NO_OF_EMPS

1987   2

    14

16 rows selected.

HAVING CLAUSE: -

Select [distinct] <col>, <col>,……----5

- 53 -

Page 54: SQL

From <tab>,<tab>,…-----------1[where <condiction>]-----------2group by <condiction>---------3HAVING <condiction>---------4[order by <col> [asc/desc]]---6

having clause is used only along with group by clause. Having clause is to check the condition on individual groups defined by

group by clsuse.

90) display the deportment where at least 5 emp’s are working.Select deptno,count(*) no_of_empsFrom empGroup by deptnoHaving count(*)>=5;

DEPTNO NO_OF_EMPS

20 5

30 6

91)display grade to which atleast 3 employees belong to …select s.grade ,count(*) no_of_empsfrom emp e,salgrade swhere e.sal between s.losal and s.hisalgroup by s.gradehaving count(*) >=3;

GRADE NO_OF_EMPS

1 3

2 3

4 5

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 sWhere e.sal between s.losal and s.hisal group by e.deptno,s.grade Having count(*)>2

DEPTNO GRADE COUNT(*)

20 4 3

93)display deportment where maximum number of employees are working.Select deptno,count(*) from empGroup by deptnoHaving count(deptno)=(select max(count(deptno)) from emp group by deptno);

- 54 -

Page 55: SQL

DEPTNO COUNT(*)

30 6

94)display the details of managers fro whom max employees rae working .select * from empwhere 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

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

95)display location where lowest avg salary is paid.Select d.loc from emp e,dept dWhere d.deptno=e.deptnoGroup by d.locHaving avg(e.sal)=(select min(avg(e.sal)) from emp e,dept d where d.deptno=e.deptno group by d.loc);

LOC

CHICAGO

96) avg salary based on gradesselect d.loc ,avg(e.sal) from emp e,dept d where d.deptno=e.deptno group by d.loc;

LOC AVG(E.SAL)

CHICAGO 1566.66667

DALLAS 2175

NEW YORK 2916.66667

97)details of highest paid employeeselect * from emp where sal=(select max(sal) from emp);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7839 KING PRESIDENT  17-NOV-81 5000   10

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 empWhere deptno=30 and exists (select deptno from emp where deptno=30 group by deptno having count(*)>6);

no rows selectedSelect * from empWhere deptno=30 and not exists (select deptno from emp where deptno=30 group by deptno having count(*)>6);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

- 55 -

Page 56: SQL

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30

7900 JAMES CLERK 7698 03-DEC-81 950   30

6 rows selected.

99)Display details of highest paid employees of each deportment select * from empwhere (deptno,sal) in(select d.deptno,max(e.sal) from emp e,dept d where e.deptno=d.deptno group by d.deptno);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7839 KING PRESIDENT  17-NOV-81 5000   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7902 FORD ANALYST 7566 03-DEC-81 3000   20

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

It is a condition correct when deptno,sal both set match with in(deptno,sal)

100)display lower paid employess of each location select e.*,d.loc from emp e,dept dwhere (d.loc,e.sal) in ( select d.loc.min(e.sal) from emp e,dept d where d.deptno=e.deptno group by d.loc) and e.deptno=d.deptno;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO LOC

7369 SMITH CLERK 7902 17-DEC-80 800   20 DALLAS

7900 JAMES CLERK 7698 03-DEC-81 950   30 CHICAGO

7934 MILLER CLERK 7782 23-JAN-82 1300   10 NEW YORK

101) display the highest paid employees of each year..select * from empwhere (to_char (hiredate,'yyyy'),sal) in (select to_char(hiredate,'yyyy'),max(sal) from emp group by to_char(hiredate,'yyyy'));

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 17-DEC-80 800   20

7839 KING PRESIDENT  17-NOV-81 5000   10

7934 MILLER CLERK 7782 23-JAN-82 1300   10

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

CO RELATED SUBQUERY:102)display details of employees hose salary is more then avg sal of there won deportment.

- 56 -

Page 57: SQL

Select * from emp xWhere sal>(select avg(sal) from emp y where y.deptno=x.deptno)

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7839 KING PRESIDENT  17-NOV-81 5000   10

7902 FORD ANALYST 7566 03-DEC-81 3000   20

6 rows selected.

103)Display the details of all the employees hose salary is less the avg salary of there jobSelect * from emp x where sal=(select max(sal) from emp y where y.deptno=x.deptno);

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7788 SCOTT ANALYST 7566 19-APR-87 3000   20

7839 KING PRESIDENT  17-NOV-81 5000   10

7902 FORD ANALYST 7566 03-DEC-81 3000   20

104)Display employees whose salary is more then of their won location select e.*,x.loc from emp e,dept x where e.sal>=(select max(e.sal) from emp e,dept y where y.loc=x.loc and e.deptno=y.deptno) and e.deptno=x.deptno;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO LOC

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30 CHICAGO

7788 SCOTT ANALYST 7566 19-APR-87 3000   20 DALLAS

7839 KING PRESIDENT  17-NOV-81 5000   10 NEW YORK

7902 FORD ANALYST 7566 03-DEC-81 3000   20 DALLAS

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

7369 SMITH CLERK 7902 17-DEC-80 800   20

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7839 KING PRESIDENT  17-NOV-81 5000   10

- 57 -

Page 58: SQL

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

7369 SMITH CLERK 7902 17-DEC-80 800   20

7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30

7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30

7566 JONES MANAGER 7839 02-APR-81 2975   20

7698 BLAKE MANAGER 7839 01-MAY-81 2850   30

7782 CLARK MANAGER 7839 09-JUN-81 2450   10

6 rows selected.

107)display 2nd highest salary.Select * from emp eWhere (select count(distinct sal) from emp s where s.sal >e.sal)=2;

Select * from emp where (select max(sal) from emp e where sal<(select max(sal) from emp));

Select sal,densc_rank() over(order by sal desc) 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)

………………………Multiple updates………………….

- 58 -

Page 59: SQL

INDEX

Every index maintain two columnsi) Key column: - it has max of 32 columnii)Row id.

When a key column of index is built on or multiple column combination then it is called composite key, concatenate key.

Max 32 columns are allowed. The first column of the concatenated key is called priority column. The index with a concatenated key concatenate key are composite key. 32 are allowed only for B-tree index. Oracle 8i introduced a new type index called as bit map index,in which the

data is stored based on function. In bit-map max 30 colums are allowed for the comcatination key. In oracle 9i bit-map indexes are begin enhanced wit multi table concept.

i.e the bit map index also be created in the joining column of the query.

Cordiality level: -duplication level types arei) High (B-tree preferred)

ii) Normal Bit-map preferred) iii) Low (Bit-map preferred)If the table is transaction table or heap table B-tree is preferred.

Heap table :Heap table is a table in which the table is stored in the order the way you

enter. Oracle 9i introduced a new type of is IOT

In which data is stored in the sorted order of primary key value for IOT primary key is must.

IOT’s are exclusively design for data ware has data Base and heep table mend for transaction D.B in which data modification is common task.

These table are require explicit indexes Every index occupy memory creates data intendency more no.of indexes

creates performance issu that grade performance. This B-tree indexes two types:

i)non unique index: 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.

- 59 -

Page 60: SQL

Creation of unique indexes on primary key and unique key not allowed.INDEX:

Syntax:CREATE [unique] INDEX <index name>ON <table name>

(<col>, <col>……);

1)Create an index on COMM column.create index e_noind on emp(comm);

2)create an index on job column

create index on emp(job);

3)create an index on lower case emp names on employees .

4) Create index on experience of employees

Create index l_ind on emp(months_between(sysdate,hiredate)/12);ERROR at line 1: ORA-01743: only pure functions can be indexedNote: sysdate is changed daily so we can’t create index in it.

5) Create index on the hiredate column on descending column

create index n_index on emp((hiredate) order by desc);ERROR at line 1: ORA-00907: missing right parenthesis

So we do like this: create index hiredate_ind on emp(hiredate desc);

6)create composite index on deportment number and job columns in the ascending deportment number is descending.

create index dept_ind on emp(deptno asc,job desc);

7). Create reverse key index on grade column of sal grade table.

Create index g_ind on salgrade (grade)reverse It starts searching is done in B-tree from leaf node.

8)Create a reverse key index on the department column number of dept table on descending order

create index raj on dept(deptno desc) reveres ;

- 60 -

Page 61: SQL

. ERROR at line 1: ORA-02158: invalid CREATE INDEX optionNote: index can’t use both desc and reverse.

9) create unique index on deptno on emp.

Create unique index dept_u_index on dept(deptno);

10)create a bitmap index on the department number column of emp table;

create bitmap index bm_deptno on emp(deptno);

CLUSTER

Syntax:

Create cluster <cluster name> (<colname> data type (width));

Create index <inde name> on cluster <cluster name>; A cluster is a definition of common column, which is used for deferent

tables. Cluster is required for those tables are commonly used together using join

condition. Every cluster must require cluster index. Cluster is created before insert the data into the cluster table .

create cluster dnoclu(dno number(2));

create table tab1(dept number(2) primary key,dname varchar2(10))cluster dnoclu(dept);

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.

i)Create a view to list all the clarks.

- 61 -

Page 62: SQL

Create view cl_view as select * from emp where job=’CLACK’;

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.

Create view rajAs Select ename,empno,sal,deptno from emp order by deptno desc;

Note: order by clause is invalid in view definition up to oracle 8i in 9i it is allowed.

iii). Create a view to list the employee no,ename,and experience of all the emp’s

create view exp as select ename,empno,months_between(sysdate,hiredate)/12 from emp;ORA-00998: must name this expression with a column alias

Note:All the columns of the views those are derived from arethematic expressions, psedo columns and function-based columns must required, column name aliases.

create view exp as select ename,empno,months_between(sysdate,hiredate)/12 exp from emp;

iv)create a view to list empno,name,salary,grade and location of all the employees.Create view emp_rajAsSelect e.ename,e.empno,s.grade,d.loc, from emp e, salgrade s, dept dWhere e.sal between s.losal and s.hisal and e.deptno=d.deptno;

v)create a view to list employees of deptno 20

create a view r_dept as select * from emp e where deptno=20 with check option;

- 62 -

Page 63: SQL

read only: it is only for see.

Rules for DML on view:

For data deletion: Data deletion is not possible if the view is derived from more then

one data base table. For data updating:

Updating is not allowed more then one table ,* updating is not allowed on the columns of the view those are derived from airhematic expression, psudo coloumn and function.

For data insertion:Above 2 rulesAnd it is not possible to insert the data into view of the view is missing the mandatory columns of the database table.

View trigger:

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.

Alter view <view name> compile;

Replace view <view name> as select statement…

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.

We create a view without a base table is called forced table is called forced table:Create force view <view name>As select statement…

To know hole deffination:Select * from user_views where view_name=’view_name’;

To drop:Drope <view> <view name>;

SYNONYM

- 63 -

Page 64: SQL

Synonym is a data base object Synonym is alternate name is to the data object. A synonym is to providing security to data base object as well as providing

convenience during data query.

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 >;

1)create a synonym for emp table.Create synonym emp_sy for emp;

2)create a synonym for view emp_details.Create synonym emp_d_syn for emp_details.

3)create synonym for above synonym.Create synonym edj for emp_d_syn;

4)create public synonym for emp table.Create public synonym emp for scott.emp;

- 64 -

Page 65: SQL

- 65 -