0% found this document useful (0 votes)
179 views38 pages

Solutions of SQL

Uploaded by

barikanubhab5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
179 views38 pages

Solutions of SQL

Uploaded by

barikanubhab5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Experiment No.

-01

Using the default table of Oracle given above.


Write SQL queries for the following:

(a) List the names and code of all employees.


1(a)>>SELECT ENAME, EMPNO FROM EMP;
(b) List the names, employee code and department code of
all clerks.
1(b)>>SELECT ENAME, EMPNO, DEPTNO FROM EMP WHERE
JOB='CLERK';
(c) List the names, employee code and salary of all
managers.
1(c)>>SELECT ENAME, EMPNO, SAL FROM EMP WHERE
JOB='MANAGER';
(d) List the names, employee code and hiredate of all
analysts.
1(d)>>SELECT ENAME, EMPNO, HIREDATE FROM EMP WHERE
JOB='ANALYST';
(e) List the employees whose salary lies between 2000 and
3000.
1(e)>>SELECT *FROM EMP WHERE SAL BETWEEN 2000 AND 3000;
(f) List the employees whose salary less than 1000.
1(f)>>SELECT *FROM EMP WHERE SAL <1000;
(g) List the employees whose salary greater than 4000.
1(g)>>SELECT *FROM EMP WHERE SAL >4000;
(h) List the employees whose salaries are 800, 1600 or
2450.
1(h)>>SELECT *FROM EMP WHERE SAL=800 OR SAL=1600 OR
SAL=2450;
>>SELECT *FROM EMP WHERE SAL IN (800,1600,2450);
(i) List the names of all employees who are either clerks or
salesman or analyst.
IT Department PAGE No:1
1(i)>>SELECT *FROM EMP WHERE JOB='CLERK' OR
JOB='SALESMAN' OR JOB='ANALYST';
(j) List the employee those who are not getting commission.
1(j)>>SELECT *FROM EMP WHERE COMM IS NOT NULL;

(k) List the employee those who are getting commission.


1(k)>>SELECT *FROM EMP WHERE COMM IS NULL; (l)
List the employee name starts with ‘F’.
1(l)>>SELECT ENAME FROM EMP WHERE ENAME LIKE 'F%';

Experiment No. -02

a. List the names and job of all employees who have names exactly 5
characters in length.

a. >>SELECT ENAME,JOB FROM EMP WHERE LENGTH(ENAME)=5;

b. List all employees whose names start with ‘G’ .

b.>>SELECT ENAME,JOB FROM EMP WHERE ENAME LIKE 'G%';

c. List all employees who name ends with ‘N’.


c. >>SELECT ENAME,JOB FROM EMP WHERE ENAME LIKE '%N';

d. List the names and job of all employees who have names exactly 5
characters in length and ends with ‘S’.

d. >>SELECT ENAME,JOB FROM EMP WHERE LENGTH(ENAME)=5


AND ENAME LIKE '%S';

e. List all employees who have not joined between 1/1/81 and
31/12/81.
IT Department PAGE No:2
e. >>SELECT ENAME,HIREDATE FROM EMP
WHERE TO_DATE(HIREDATE,'DD-MM-YY')<'01-JAN-81'
OR
TO_DATE(HIREDATE,'DD-MM-YY')>'31-DEC-81';

f. List all employees whose job does not start will “CL”.

f. >>SELECT ENAME,JOB FROM EMP WHERE JOB NOT LIKE 'CL%';


g. List all managers who earn more than Rs. 4000/-.

g. >>SELECT ENAME,SAL FROM EMP WHERE JOB='MANAGER' AND


SAL>4000;

h. List all clerks and salesman who earn more than Rs. 1600/-

h. >>SELECT ENAME,JOB,SAL FROM EMP WHERE SAL >1000 AND JOB


IN ('CLERK','SALESMAN');

i. List the names and salaries of all employees who were joined as
manager during 1981.

i. >>SELECT ENAME,SAL,HIREDATE,JOB FROM EMP WHERE


TO_DATE(HIREDATE,'DD-MM-YY')>'01-JAN-81'
AND
TO_DATE(HIREDATE,'DD-MM-YY') <'31-DEC-81' AND
JOB='MANAGER';

Experiment No. -03

For the EMP relation, frame the following queries using SQL.
(a) Calculate the average salary of all employees.

A) SELECT AVG(SAL) "AVERAGE SAL" FROM EMP; (b) Calculate the


average salary of all Managers.

IT Department PAGE No:3


B) SELECT AVG(SAL) "AVERAGE SAL" FROM EMP WHERE JOB='MANAGER';
(c)Calculate the total salary of all employees.

C) SELECT SUM(SAL) "TOTAL SAL" FROM EMP;

(d) Calculate the total salary of all managers.


D) SELECT SUM(SAL) "TOTAL SAL" FROM EMP WHERE
JOB='MANAGER'; (e) Find the minimum salaries
earned by the employees.

E) SELECT MIN(SAL) "MINIMUM SAL" FROM EMP'; (f)


Find the maximum salaries earned by the employees.

F) SELECT MAX(SAL) "MAXIMUM SAL" FROM EMP; (g)


Find the minimum salaries earned by a clerks.

G)SELECT MAX(SAL) "MAXIMUM SAL" FROM EMP WHERE JOB='CLERK' (h)


Find the maximum salaries earned by a salesman.

H)SELECT MAX(SAL) "MAXIMUM SAL" FROM EMP WHERE JOB='SALESMAN'


(i) Find the minimum and maximum and average salaries earned by a
employees.

I) SELECT MAX(SAL) "MAXIMUM SAL",MIN(SAL) "MINIMUM


SAL",AVG(SAL) "AVERAGE SAL" FROM EMP
(j) Find the minimum and maximum and average salaries earned by a clerks.

J) SELECT MAX(SAL) "MAXIMUM SAL",MIN(SAL) "MINIMUM SAL",AVG(SAL)


"AVERAGE SAL" FROM EMP WHERE JOB

(k) List the total number of employees and the average salaries of the
different departments.

K) SELECT DEPTNO,COUNT(*), AVG(SAL) "AVARAGE SAL" FROM EMP GROUP BY


DEPTNO
(l) Calculate total number of employees.

L)SELECT COUNT(*) "NO OF EMPLOYEES" FROM EMP (m)


Calculate total number of managers.
IT Department PAGE No:4
M) SELECT COUNT(*) "NO OF EMPLOYEES" FROM EMP WHERE JOB='MANAGER'
(n) Calculate the number of employees who are not getting any
commission.
N)SELECT COUNT(*) "NO OF EMPLOYEES" FROM EMP WHERE COMM IS NULL

(o) Calculate the number of employees who are getting any commission.

O)SELECT COUNT(*) "NO OF EMPLOYEES" FROM EMP WHERE COMM IS NOT


NULL

(p) List the details of all managers in ascending order of joining dates.

P)SELECT * FROM EMP WHERE JOB='MANAGER' ORDER BY HIREDATE

(q) List the average salaries for each different job.

Q) SELECT AVG(SAL) "AVERAGE SALARY" FROM EMP GROUP BY JOB (r)


Display the average salary for each different job.

R) SELECT AVG(SAL) "AVERAGE SALARY" FROM EMP GROUP BY JOB

(s) Display the minimum, maximum, and average salaries for each job
group.

S)SELECT JOB,MAX(SAL) "MAXIMUM SAL",MIN(SAL) "MINIMUM SAL",AVG(SAL)


"AVERAGE SAL" FROM EMP GROUP BY JOB

(t) Find all departments which have less than 3 employees.

T)SELECT DEPTNO,COUNT(*) "NO OF EMPLOYEE" FROM EMP GROUP BY


DEPTNO HAVING COUNT(*)<4

(u) List the details of the employees in ascending order of department


number, and within each department, in descending order of salary.

U)SELECT * FROM EMP ORDER BY DEPTNO,SAL DESC

(v) Display the name, deptno and annual salary of each employee in order
salary and deptno
IT Department PAGE No:5
V) SELECT ENAME,DEPTNO,SAL
FROM EMP
ORDER BY DEPTNO
(w) Display the name of employee who earns maximum salary.

W)SELECT ENAME,SAL FROM EMP WHERE


SAL= (SELECT MAX(SAL)
FROM EMP)
(x) Display the name of employee who earns minimum salary.

X)SELECT ENAME,SAL FROM EMP


WHERE SAL=(SELECT MIN(SAL)
FROM EMP)

(y) Display the name of employee who earns maximum salary whose job is
salesman.

Y)SELECT ENAME,SAL,JOB FROM EMP


WHERE SAL=(SELECT MAX(SAL)
FROM EMP
WHERE JOB='SALESMAN')
(z) Display the name of employee who earns minimum salary whose job is
clerk.

Z)SELECT ENAME,SAL,JOB
FROM EMP WHERE SAL
=(SELECT MIN(SAL)
FROM EMP
WHERE JOB='CLERK')
aa) Display the department number whose average salary is
maximum.

AA)SELECT DEPTNO,AVG(SAL) FROM EMP


GROUP BY DEPTNO
HAVING AVG(SAL)=(SELECT MAX(AVG(SAL)) FROM
EMP
GROUP BY DEPT NO)

IT Department PAGE No:6


Experiment No. – 04
Using the default table of Oracle, such as Emp and Dept.
(a) List all employee names, dept name and the city, in department name
order.

A)SELECT ENAME, DNAME ,LOC FROM EMP, DEPT


WHERE EMP.DEPTNO= DEPT.DEPTNO
ORDER BY DNAME
(b) List all employee name, dept number, dept name and salary.

B)SELECT ENAME, DNAME ,DEPT.DEPTNO,SAL FROM


EMP, DEPT
WHERE EMP.DEPTNO= DEPT.DEPTNO

(c)List all employees working in Dallas in descending order of salary.

C) SELECT ENAME ,LOC,SAL


FROM EMP, DEPT
WHERE EMP.DEPTNO= DEPT.DEPTNO AND LOC='DALLAS'
ORDER BY SAL
(d) List all employees’ name, job, salary and department name for everyone
in the company except clerks. Sort the report with respect to job and
salary.

D) SELECT ENAME ,JOB,SAL,DNAME


FROM EMP, DEPT
WHERE EMP.DEPTNO= DEPT.DEPTNO AND JOB
NOT LIKE 'CL%' ORDER
BY SAL,JOB

(e) List all employee names who work in the same city as an employee
named ‘FORD’
E) SELECT ENAME ,LOC FROM EMP, DEPT
WHERE EMP.DEPTNO= DEPT.DEPTNO

IT Department PAGE No:7


AND LOC=(SELECT LOC
FROM EMP,DEPT
WHERE EMP.DEPTNO= DEPT.DEPTNO
AND
ENAME='FORD')

(f) Display the name of the dept that has no employee.

F)SELECT DNAME FROM DEPT


WHERE DEPTNO= ANY (SELECT DEPTNO
FROM DEPT
MINUS
SELECT DEPTNO
FROM EMP)

Assignment No: -05

(a) List the employees belonging to the department 20. a)


SELECT ENAME FROM EMP WHERE DEPTNO=20

b) List the name and salary of the employees whose salary is more than
1000.

b) SELECT ENAME,SAL FROM EMP WHERE SAL>1000

c) List the employee number and the name of the manager.

c) SELECT ENAME,EMPNO FROM EMP WHERE JOB='MANAGER'

d) List the names of the clerks working in the department


20.

d) SELECT JOB FROM EMP


WHERE DEPTNO=20
e) List the name of the analysts and salesman.

e) SELECT ENAME,JOB FROM EMP


IT Department PAGE No:8
WHERE JOB IN ('SALESMAN','ANALYST')

f) List the details of the employees who have joined before the end of
September 81.

f)SELECT * FROM EMP


WHERE TO_DATE(HIREDATE ,'DD-MM-YY')
< '30-SEP-81'
(g) List the names of the employees who are not managers.

g) SELECT ENAME FROM EMP WHERE JOB NOT LIKE 'MANAGER'

h) List the name of the employees whose employee numbers


7369,7521,7839,7934,7788.

h) SELECT ENAME,EMPNO
FROM EMP
WHERE EMPNO IN (7369,7521,7839,7934,7788)
i) List the employee details not belonging to the department 10,30 and
40.

h) SELECT *FROM EMP WHERE DEPTNO NOT IN (10,30,40)

j) List the employee names who have joined before 30th June’81 and after
December ’81.

h) SELECT ENAME

FROM EMP
WHERE TO_DATE(HIREDATE,'DD-MM-YY')<'30-JAN-81' OR
TO_DATE(HIREDATE,'DD-MM-YY')>'31-DEC-81'

k) List the name of the employee and designation (job) of the employee
who does not report to anybody (who doesn’t have manager).

h) SELECT ENAME,JOB FROM EMP WHERE MGR IS NULL

IT Department PAGE No:9


l) List the different jobs (designations) available in the emp table.

l) SELECT DISTINCT JOB FROM EMP

m) List the employees not assigned to any department.

M)SELECT ENAME FROM EMP WHERE DEPTNO IS NULL

n) List the details of the employees whose salary is greater than 2000
and not eligible for commission.

M)SELECT * FROM EMP WHERE SAL>2000 AND COMM IS NULL

o) List the employee names having ‘I’ as the second character.

O) SELECT ENAME FROM EMP WHERE ENAME LIKE '_I%'

p) List the name, salary and PF amount of all the employees (PF is
calculated as 10% of salary).

P) SELECT ENAME,0.1*SAL "PF"


FROM EMP

q) List the employee number, name and salary in ascending order of


salary.

Q) SELECT ENAME,EMPNO,SAL
FROM EMP ORDER
BY SAL

r) Lists the employee name and hiredate in descending order of hiredate.

R) SELECT ENAME,HIREDATE
FROM EMP
ORDER BY HIREDATE DESC

s) List the employee name, salary, PF, HRA, DA and gross salary;order the
result in ascending order of gross.HRA is 50% of salary and DA is 30%
of salary.

IT Department PAGE No:10


R) SELECT ENAME,SAL,0.1*SAL "PF",0.5*SAL "HRA",0.3*SAL "DA",SAL+
0.1*SAL+0.5*SAL+0.3*SAL "GROSS"
FROM EMP
ORDER BY JOB
t) List the department number and the total salary payable in each
department. List the jobs and number of employees in each job.The
result should be in descending order of the number of employees.

R) SELECT DEPTNO,SUM(SAL) "TOTAL SAL"


FROM EMP
GROUP BY DEPTNO

SELECT JOB,COUNT(*) "EMPNO"


FROM EMP GROUP
BY JOB
ORDER BY COUNT(*) DESC

u) List the total salary, maximum, minimum and average salary of the
employee’s job wise.

R) SELECT JOB,SUM(SAL) "TOTAL SAL", MAX(SAL) "MAX", MIN(SAL)


"MIN",AVG(SAL) "AVERAGE" FROM
EMP
GROUP BY JOB
v) List the average salary from each job excluding manager.

R) SELECT AVG(SAL) "AVERAGE", JOB FROM EMP


GROUP BY JOB
HAVING JOB NOT LIKE 'MANAGER'

w)List the average monthly salary for each job type within department.

R) SELECT AVG(SAL) "MONTHLY", JOB, DEPTNO


FROM EMP
GROUP BY DEPTNO,JOB

x) List average salary for all departments employing more than five
people.
R) SELECT DEPTNO, COUNT(*) "NO OF EMP" , AVG(SAL) "AVERAGE"
IT Department PAGE No:11
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*)>5

S) List job of all the employees where maximum salary is greater


than or equal to 3000.

Y) SQL> SELECT JOB FROM EMP


GROUP BY JOB
HAVING MAX(SAL)>=3000;

Z) List the total salary, maximum and minimum salary and the average
salary of employees job wise for department number 20 and display
only those rows having average salary greater than 1000.

z) SELECT JOB,SUM(SAL) "TOTAL SAL", MAX(SAL) "MAX", MIN(SAL) "MIN",


AVG(SAL) "AVERAGE"
FROM EMP
WHERE deptno=20 and
GROUP BY JOB
HAVING AVG(SAL)>1000

Assignment No: -6
(a) List the employees earns more than any employee in CHICAGO.

A> SELECT ENAME ,SAL


FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
AND
SAL>(SELECT MAX(SAL)
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
AND
LOC='CHICAGO' GROUP BY LOC) SQL>
/

IT Department PAGE No:12


ENAME SAL
---------- --------- JONES
2975
SCOTT 3000 KING
5000
FORD 3000

(b) List the name of the employee who works in the same department as
SMITH.

B> SELECT ENAME


FROM EMP
WHERE DEPTNO=(SELECT DEPTNO
FROM EMP
WHERE ENAME='SMITH')
SQL> /

ENAME
----------
SMITH
JONES
SCOTT
ADAMS
FORD
(c)List the name, employee number ,their manager name and manager
number.

C> SELECT E1.ENAME,E1.EMPNO,E2.ENAME,E2.MGR


FROM EMP E1, EMP E2 WHERE
E1.MGR=E2.EMPNO
SQL> /

ENAME EMPNO ENAME MGR


---------- --------- ---------- ---------
SMITH 7369 FORD 7566
ALLEN 7499 BLAKE 7839
--------- --------- --------- -------- MILLER
7934 CLARK 7839

13 rows selected.
IT Department PAGE No:13
(d) List the name of the employee job is same as ‘CLARK’.

D> SELECT ENAME,JOB


FROM EMP
WHERE JOB=(SELECT JOB
FROM EMP
WHERE ENAME='CLARK') SQL>
/

ENAME JOB
---------- ---------
JONES MANAGER
BLAKE MANAGER
CLARK MANAGER
(e) List the name of employee whose salary is more than ‘TURNER’.

E> SELECT ENAME


FROM EMP
WHERE SAL>(SELECT SAL
FROM EMP
WHERE ENAME='TURNER')
SQL> /

ENAME
----------
ALLEN
……
……
FORD

6 rows selected.
f) List the name of employee who joined after ‘ALLEN’.

F> SELECT ENAME ,HIREDATE


FROM EMP
WHERE HIREDATE>(SELECT HIREDATE
FROM EMP
WHERE ENAME='ALLEN')
SQL> /

IT Department PAGE No:14


ENAME HIREDATE
---------- ---------
WARD 22-FEB-81 …………
…………
MILLER 23-JAN-82

11 rows selected.
g) Display the name of the department whose job is ‘SALESMAN’.

G> SELECT DISTINCT DNAME ,JOB


FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
AND
JOB='SALESMAN'
SQL> /

DNAME JOB
-------------- ---------
SALES SALESMAN
Z) Display the name of the department in which ‘FORD’ works.

H> SELECT DNAME ,JOB


FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
AND
ENAME='FORD'
SQL> /

DNAME JOB
-------------- ---------
RESEARCH ANALYST
AA) Display the name of the department whose salary is maximum.

I> SELECT DNAME


FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
AND
SAL=(SELECT MAX(SAL)
IT Department PAGE No:15
FROM EMP)
SQL> /

DNAME
--------------
ACCOUNTING

BB) Display the name of the city(location) in which ‘SMITH’ works.

J> SELECT LOC


FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
AND
ENAME='SMITH'
SQL> /
LOC
-------------
DALLAS
CC) Display the name of the city in which the manager works.

K> SELECT LOC FROM


EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
AND
JOB='MANAGER'
SQL> /
LOC
-------------
DALLAS
CHICAGO
NEW YORK
(f) Display the grade of the employee named ‘MARTIN’.

IT Department PAGE No:16


m)List the employees earns more than every employee in ‘DALLAS’.
M > SELECT ENAME ,SAL
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
AND
SAL>(SELECT MAX(SAL)
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
AND
LOC='DALLAS')
SQL> /

ENAME SAL
---------- ---------
KING 5000
n) Display the name of the department which has no employee.

N> SELECT DISTINCT DNAME


FROM DEPT
WHERE DEPTNO=ANY(SELECT DEPTNO
FROM DEPT
MINUS
SELECT DEPTNO
FROM EMP)
SQL> /
DNAME
--------------
OPERATIONS
o) List name, employee number and the name, employee number of their
managers’ manager

p) list the name of the employee who joined in the same year of ‘ADAMS’.
P> SELECT ENAME

IT Department PAGE No:17


FROM EMP
WHERE TO_CHAR(HIREDATE,'YY')=(SELECT
TO_CHAR(HIREDATE,'YY') FROM
EMP
WHERE ENAME='ADAMS'
SQL> /
ENAME
----------
SCOTT
ADAMS

q) list the name of the employee who joined in the same month of
‘BLAKE’.

Q> SELECT ENAME FROM EMP WHERE TO_CHAR(HIREDATE,'MM')=(SELECT


TO_CHAR(HIREDATE,'MM') FROM EMP WHERE ENAME='BLAKE'
SQL> /
ENAME
----------
BLAKE
ADAMS
r) list the name of the employee who joined in the same year of
‘ADAMS’.

R> SELECT ENAME


FROM EMP
WHERE TO_CHAR(HIREDATE,'DD')=(SELECT
TO_CHAR(HIREDATE,'DD')
FROM EMP
WHERE ENAME='ADAMS'
SQL> /

ENAME
----------
ADAMS
MILLER

s) List the name of the department who gets commissions


s) SELECT DISTINCT DNAME
IT Department PAGE No:18
FROM EMP,DEPT
WHERE EMP.DEPTNO=DEPT.DEPTNO
AND
COMM IS NOT NULL
SQL> /

DNAME
--------------
SALES

Assignmet no: 07

1) Create Table Clientmaster


1(a)>> create table Clientmaster
(client_no varchar(6) primary key,
c_name varchar(20) NOT NULL,
add1 varchar(30), add2
varchar(15), pincode
number(8), state varchar(15),
city varchar(15), bal_due
number(10,2),
constraint ck_clno check(client_no like 'c%'));

2) Describe the Table Clientmaster


SQL> desc clientmaster
Name Null? Type
----------------------------------------------------- -------- ----------------------------------
CLIENT_NO NOT NULL VARCHAR2(6)
C_NAME NOT NULL VARCHAR2(20)
ADD1 VARCHAR2(30)
ADD2 VARCHAR2(15)
PINCODE NUMBER(8)
STATE VARCHAR2(15)
CITY VARCHAR2(15)
BAL_DUE NUMBER(10,2)
IT Department PAGE No:19
3. create table Salesmaster
SQL> create table Salesmaster
(salesman_no varchar2(6) primary key,
s_name varchar2(20) NOT NULL, add1
varchar2(15) , add2 varchar2(15),
pincode number(8), state varchar2(20),
city varchar2(15),
samount number(8,2) NOT NULL, quantity
number(8,2) NOT NULL, ytd_sales number
(8,2) NOT NULL, constraint ck_slno
check(salesman_no like 's%'), constraint ck_smt
check (samount <>0),
constraint ck_qty check (quantity <>0)
);
4. Describe the Table salesmaster

SQL> desc salesmaster;


Name Null? Type
----------------------------------------------------- -------- ------------------------------------
SALESMAN_NO NOT NULL VARCHAR2(6)
S_NAME NOT NULL VARCHAR2(20)
ADD1 VARCHAR2(15)
ADD2 VARCHAR2(15)
PINCODE NUMBER(8)
STATE VARCHAR2(20)
CITY VARCHAR2(15)
SAMOUNT NOT NULL NUMBER(8,2)
QUANTITY NOT NULL NUMBER(8,2)
YTD_SALES NOT NULL NUMBER(8,2)

5. CREATE TABLE Salesorder

SQL>CREATE TABLE Salesorder


(order_no VARCHAR2(6) PRIMARY KEY,
order_date DATE ,
client_no VARCHAR2(6) REFERENCES Clientmaster, del_add
VARCHAR2(25) ,
salesman_no VARCHAR2(6) REFERENCES Salesmaster,
del_type char(1) DEFAULT 'f',
del_date DATE,
CONSTRAINT ck_orno check(order_no like 'o%'),
IT Department PAGE No:20
CONSTRAINT ck_dtype check (del_type in ('f','p')),
CONSTRAINT ck_ddate check (del_date >order_date)
);

6. DESCRIBE TABLE Salesorder


SQL> desc Salesorder;
Name Null? Type
----------------------------------------------------- -------- ---------------------
ORDER_NO NOT NULL VARCHAR2(6)
ORDER_DATE DATE
CLIENT_NO VARCHAR2(6)
DEL_ADD VARCHAR2(25)
SALESMAN_NO VARCHAR2(6)
DEL_TYPE CHAR(1) DEL_DATE
DATE

7. Insert value in Clientmaster Table

SQL>INSERT INTO Clientmaster


VALUES
('&client_no','&c_name','&add1','&add2','&pincode','&state','&city','&bal_du
e')

SQL> select * from Clientmaster;

SQL> commit; Commit


complete.
8. Insert value in Salesorder Table

2(c)>> INSERT INTO Salesorder

VALUES('&order_no','&order_date','&client_no','&del_add','&salesman_no','&del_t
ype','&del_date');

SQL> select * from Salesorder;

ORDER_ NO ORDER_DATE CLIENT_NO DEL_ADD SALESMAN_NO DEL_TYPE


DEL_DATE
------ --------- ------ ------------------------- ------ - ---------
o01 10-MAR-17 c02 station road,ranchi s01 F 15-MAR-
17

IT Department PAGE No:21


o02 09-FEB-17 c01 club road doranda s01 p 13-MAR17
……
…………………………………
10 rows selected.

SQL> commit;

Commit complete.

PLSQL Program:

Q1. Write a PL/SQL block which will accept a value from the user and it will
insert the result into a new table 'result'.
Attributes :
no number(3) , primary key value
number(20)

ans: 8(a)>>

SQL> CREATE Table Result


2 (no NUMBER(3) PRIMARY KEY,
3 value NUMBER(20));

Table created.

Open PL/sql notepad:


SQL> ed ass81

IT Department PAGE No:22


DECLARE
n NUMBER(3);
fact NUMBER(20);
BEGIN
DELETE FROM Result;*/
n:=&n;
fact:=1; for
cntr in 1..n
loop
fact:=fact*cntr;
end loop;

dbms_output.put_line('The factorial value='||fact);


insert into Result values(n,fact); end;

SQL> set serveroutput on;

SQL> @ass81;
Input truncated to 5 characters
15 /
Enter value for n: 5
old 6: n:=&n;
new 6: n:=5;
The factorial value=120

PL/SQL procedure successfully completed.

SQL> select * from result;

NO VALUE
--------- --------- 5 120

SQL> @ass81;

IT Department PAGE No:23


Input truncated to 5 characters
15 /
Enter value for n: 6 old
6: n:=&n;
new 6: n:=6;
The factorial value=720

PL/SQL procedure successfully completed.

SQL> select * from Result;

NO VALUE
--------- --------- 6 720

SQL> @ass81;

Input truncated to 5 characters


15 /
Enter value for n: 9 old
6: n:=&n;
new 6: n:=9;
The factorial value=362880

PL/SQL procedure successfully completed.

SQL> select * from Result;

NO VALUE
--------- ---------
6 720
5 120
9 362880

Q2. Write a PL/SQL block which will accept upper and lower limit values
from the user and it will insert the factorial value into table 'result'.

Open PL/sql notepad:

DECLARE
fact NUMBER(20);
IT Department PAGE No:24
n1 NUMBER(3);
n2 NUMBER(3);
n NUMBER(3);
BEGIN
n1:=&n1;
n2:=&n2;
for cntr1 in n1..n2
loop
n:=cntr1;
fact:=1;
for cntr2 in 1..n
loop
fact:=fact*cntr2;

END LOOP;
dbms_output.put_line('The factorial value= '|| fact);
insert into Result values(n,fact);
END LOOP;
END;

SQL> @ass82;
Enter value for n1: 5 old
7: n1:=&n1; new
7: n1:=5; Enter
value for n2: 9 old 8:
n2:=&n2;
new 8: n2:=9;
The factorial value=120
The factorial value=720
The factorial value=5040
The factorial value=40320
The factorial value=362880

PL/SQL procedure successfully completed

SQL> select * from Result;

NO VALUE
--------- ---------
5 120
6 720
7 5040
IT Department PAGE No:25
8 40320
9 362880
Q3. Write a PL/SQL block which will accept an employee no check his salary
if it is less than 1000 then make it increment by 20% of the salary but if it is
less than 1000 then make the increment of 10% only.
i) Copy the table emp and named as emp1
ii) Insert the data employee_name ,employee_no, old salary, new
salary & date of changes into a new table named oldsal

SQL>CREATE TABLE Emp1 AS SELECT * FROM Emp;

SQL> CREATE Table oldsal


(ename VARCHAR2(10), empno
NUMBER(4),
old_sal NUMBER(10,2), new_sal
NUMBER(10,2), dt_chng DATE);

DECLARE
msal NUMBER(10,2);
mempno NUMBER(4);
msal1 NUMBER(10,2);
mname VARCHAR2(10);
BEGIN
mempno:='& mempno';
SELECT sal INTO msal FROM Emp1 WHERE empno=mempno;
if msal<1000 then
UPDATE Emp1 set sal=sal*1.20 WHERE empno=mempno;
else
UPDATE Emp1 set sal=sal*1.10 WHERE empno=mempno; end
if;
SELECT ename,empno,sal INTO mname, mempno,msal1 FROM Emp1 WHERE
empno=mempno;
insert into oldsal values(mname,mempno,msal,msal1,sysdate);
dbms_output.put_line('EMPNO NAME OLD_SAL NEW_SAL DT_CHNG');
dbms_output.put_line('------------------------------------------------------');
dbms_output.put_line(mempno||' '||mname||' '||msal||' ' ||msal1||'
'||sysdate);

END;
IT Department PAGE No:26
SQL>select * from oldsal;

ENAME EMPNO OLD_SAL NEW_SAL DT_CHNG


---------- --------- --------- --------- ---------
SMITH 7369 800 960 06-APR-17
SMITH 7369 960 1152 06-APR-17
SMITH 7369 1152 1267. 06-APR-17
SMITH 7369 1267.2 1393.9 06-APR-17
ALLEN 7499 1600 1760 06-APR-17

8(d)>>write a PL/SQL Block having a simple for lop to insert 10 rows into a
new database table.
The attribute of the new table is loop index, counter variable and
either of two character strings are inserted(characteristic will be either odd
or even

Sql> CREATE Table Temp


(num_col1 NUMBER(4), num_col2
NUMBER(4) ,
char_col VARCHAR2(15)
);

DECLARE
x NUMBER:=100;
BEGIN
DELETE FROM Temp;
for i in 1..10
loop
if mod(i,2) =0 then
insert into Temp values(i,x,'i is even' else
insert into Temp values(i,x,'i is odd');
end if; x:=x+100; end loop; commit;
end;

IT Department PAGE No:27


SQL> select *from Temp;

NUM_COL1 NUM_COL2 CHAR_COL


--------- --------- ---------------
1 100 i is odd
2 200 i is even
3 300 i is odd
4 400 i is even
5 500 i is odd
6 600 i is even
7 700 i is odd
8 800 i is even
9 900 i is odd
10 1000 i is even

10 rows selected.

Create SQL block using ‘CURSOR’.

1. Write a Pl/SQL block using cursor to select 5 highest paid employee from
the emp1 table amnd to display the emp_no, emp_name and salary in
descending order.

SQL> CREATE Table Temp1


(emp_name VARCHAR2(10), emp_no
NUMBER(4),
salary NUMBER(10,2))
SQL> /

Table created.

IT Department PAGE No:28


Declare

cursor c1 is select empno,ename,sal from emp1 order by sal desc;

mname varchar2(10);
mempno number(4); msal
number(10,2);
begin

open c1;
for i in 1..5
loop
fetch c1 into mempno,mname,msal;
exit when c1%NOTFOUND;

insert into Temp1 values(mempno,mname,msal);


commit;
end loop;

dbms_output.put_line('empno empname cur sal date');


dbms_output.put_line('-----------------------------------------------');
dbms_output.put_line(mempno||' '||mname||' '||msal||'
'||mdate);
close c1; end;

SQL> @ass91;

SQL> select * from temp1;

EMP_NAME EMP_NO SALARY


---------- --------- --------- KING
7839 5000
SCOTT 7788 3000
FORD 7902 3000
JONES 7566 2975
BLAKE 7698 2850

2. Write a PL/SQL block using implicit cursor that update the salary of the
employee by 500 for a given dept no. If there is no record of employee in the table
IT Department PAGE No:29
it will display ”no employee selected” otherwise it will display the no of records
of employee whose salary has been changed. Ans:

DECLARE
total_rows NUMBER(2);
mdeptno NUMBER(3);

BEGIN update emp1 set sal=sal+500


where deptno=&mdeptno;

if sql % NOTFOUND then


else
total_rows:=sql%rowcount;
dbms_output.put_line(total_rows||'employee selected ');

end if;
commit;
end;

output:
Input truncated to 1 characters
15 /
Enter value for mdeptno: 30
old 5: update emp1 set sal=sal+500 where deptno=&mdeptno; new
5: update emp1 set sal=sal+500 where deptno=30;
6employee selected
PL/SQL procedure successfully completed.

SQL> @ass92
Input truncated to 1 characters
15 /
Enter value for mdeptno: 70 old 5: update emp1 set
sal=sal+500 where deptno=&mdeptno; new 5: update emp1
set sal=sal+500 where deptno=70;
No employee selected
PL/SQL procedure successfully completed.

3. Write a PL/SQL block using explicit cursor that makes the increment of
the salary of employee belonging to same department .if the salary is
greater than 2000 the increment will be 10%. Otherwise increment

IT Department PAGE No:30


will be 5% . display the emp_no , only increment of salary and changing
date.
Ans:

DECLARE

mdeptno NUMBER(3); mempno


emp1.empno%type;
msal number(10,2);

cursor c_emp is SELECT empno,sal


FROM Emp1
where emp1.deptno=&mdeptno;
BEGIN
dbms_output.put_line('empno salary Date');
dbms_output.put_line('------------------------------------------------------');

open c_emp;
loop
fetch c_emp into mempno,msal;
exit when c_emp%notfound;

if msal>2000 then
dbms_output.put_line(mempno||' '||msal*0.10||' ' ||sysdate); update
emp1 set sal=msal*1.10
where deptno=mdeptno and sal>2000; else

dbms_output.put_line(mempno||' '||msal*0.05||' ' ||sysdate); update


emp1 set sal=msal*1.05
where deptno=mdeptno and sal<=2000;
end if;
end loop;
commit; close
c_emp; end;

output: SQL>
@ass93
24 /
Enter value for mdeptno: 30
IT Department PAGE No:31
old 5: cursor c_emp is SELECT empno,sal FROM Emp1 where
emp1.deptno=&mdeptno;
new 5: cursor c_emp is SELECT empno,sal FROM Emp1 where
emp1.deptno=30;

empno salary Date


------------------------------------------------------ 7499
226 13-APR-17
7521 87.5 13-APR-17
7654 87.5 13-APR-17
7698 335 13-APR-17
7844 100 13-APR-17
7900 72.5 13-APR-17

PL/SQL procedure successfully complete

4. Write a PL/SQL block using explicit cursor that makes the increment of the
salary of 5% belonging to same department, update in emp1. Display the
emp_no , the old salary, dept_name and date of changes. Also create a new
table EMP_raise that record the emp_no, oldsalary, dept_nameand date of
changes

ANS:
DECLARE mdeptno
NUMBER(3);

cursor cc_emp is SELECT empno,sal,dname

IT Department PAGE No:32


FROM Emp1,dept
where emp1.deptno=dept.deptno
and
emp1.deptno=&mdeptno;
BEGIN
dbms_output.put_line('empno deptname oldsalary Date');
dbms_output.put_line('------------------------------------------------------');

for emp_rec in cc_emp loop


dbms_output.put_line(emp_rec.empno||' '||emp_rec.dname||' '
||emp_rec.sal||' '||sysdate);

update emp1 set sal=emp_rec.sal*1.05


where empno=emp_rec.empno;

insert into emp_raise values (emp_rec.empno, emp_rec.dname ,


emp_rec.sal , sysdate);

end loop;
commit;

end;

SQL> create table emp_raise


(
empno number(6),
dname varchar2(20),
sal number(7,2),
c_date date
);

Table created.

SQL> @ass94
17 /
Enter value for mdeptno: 30
old 3: cursor cc_emp is SELECT empno,sal,dname FROM Emp1,dept where
emp1.deptno=dept.deptno and em
new 3: cursor cc_emp is SELECT empno,sal,dname FROM Emp1,dept where
emp1.deptno=dept.deptno and em

IT Department PAGE No:33


empno deptname oldsalary Date
------------------------------------------------------
7499 SALES 2260 13-APR-17
7521 SALES 1750 13-APR-17
7654 SALES 1750 13-APR-17
7698 SALES 3350 13-APR-17
7844 SALES 2000 13-APR-17
7900 SALES 1450 13-APR-17

PL/SQL procedure successfully completed.

SQL> select * from emp_raise;

EMPNO DNAME SAL C_DATE


--------- -------------------- --------- --------- 7499 SALES
2260 13-APR-17
7521 SALES 1750 13-APR-17
7654 SALES 1750 13-APR-17
7698 SALES 3350 13-APR-17
7844 SALES 2000 13-APR-17
7900 SALES 1450 13-APR-17

6 rows selected.

SQL> select * from emp1;

EMPNO ENAME JOB MGR HIREDATE SAL COMM


DEPTNO
--------- ---------- --------- --------- --------- --------- --------- -
--------
7369 SMITH CLERK 7902 17-DEC-80 1393.92 20
7499 ALLEN SALESMAN 7698 20-FEB-81 2373 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1837.5 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1837.5 1400 30
------
-------
14 rows selected.

IT Department PAGE No:34


STORED PROCEDURE & FUNCTION

1. A) Create an enquiry table with


enq_no Number(5) and
first _name varchar2(10)

Create a stored procedure & function which to be referred from some


PL/SQL block, that returns the fname whenever the enquiry_no is given
from the PL/SQL block . When the enquiry_no is not found in db table then
it will give the error message “This enquiry_no is not available”.

1. B) Repeat the same question by using function

create or replace procedure findname(enqno1 in number,fname1


out varchar2) is fname2
varchar2(30);
begin select fname into fname2
enquiry where
enqno=enqno1;

fname1:=fname2; exception
when no_data_found then raise_application_error(-
20100,'This enquiry no. is not Available'); end;

Pl/SQL BLOCK

declare enqno2
number(5); fname2
varchar2(30); begin
enqno2:=lenqno2; findname(enqno2,fname2);
dbms_output_line('The name is'||fname2); end;

ANS B.
IT Department PAGE No:35
create or replace function getname(enqno1 in number) return varchar2 is
fname1 varchar2(30);

begin
select fname into fname1from enquiry where enqno=enqno1;

return(fname1); exception

when no_data_found then


raise_application_error(-20001,'This enquiry no. is not Available'); end;

PL/SQL BLOCK:-

declare

Menqno number(5);
fname2 varchar2(30);

begin

Menqno:=&Menqno;
FNAME2:=getname(Menqno); dbms_output_line('The name is'||fname2);

end;

TRIGGER

Table name: Client_Master

IT Department PAGE No:36


Name Null? Type
----------------------------------------------------- -------- ----------------------------------
CLIENT_NO VARCHAR2 (6) Primary Key/ First letter
must start with ‘C’
C_NAME VARCHAR2 (20) NOT NULL
ADD1 VARCHAR2 (30)
ADD2 VARCHAR2 (15)
PINCODE NUMBER (8)
STATE VARCHAR2 (15)
CITY VARCHAR2 (15)
BAL_DUE NUMBER (10,2)

Table name: Auditclient


Column Data type Size Attributes
name
Client_no varchar 6
Name Varchar2
Bal_due Number 10,2
Operation Varchar2 8
UserID Varchar2 20
Odate date

1. Auditclient keeps track of the records deleted or updated when some


operations are carried out.

Create a row trigger which will execute after updation or deletion of


client_master in such a way that it will insert the old client_no, old_balance,
operation and system date into new table auditclient.

IT Department PAGE No:37


CREATE TRIGGER audit_trail
AFTER UPDATE OR DELETE ON client_master
FOR EACH ROW
DECLARE
Oper varchar2(8);
Client_no varchar2(6);
Name varchar2(20);
Bal_due number(10,2);
BEGIN
IF UPDATING THEN
Oper:=’update’; END
IF;
IF DELETING THEN
Oper:=’delete’;
END IF;
Client_no:= :old.client_no;
Name := :old.name;
Bal_due := :old.Bal_due;
INSERT INTO auditclient
VALUES (client_no, name, bal_due, oper,user,
sysdate);
END;

IT Department PAGE No:38

You might also like