Solutions of SQL
Solutions of SQL
-01
a. List the names and job of all employees who have names exactly 5
characters in length.
d. List the names and job of all employees who have names exactly 5
characters in length and ends with ‘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”.
h. List all clerks and salesman who earn more than Rs. 1600/-
i. List the names and salaries of all employees who were joined as
manager during 1981.
For the EMP relation, frame the following queries using SQL.
(a) Calculate the average salary of all employees.
(k) List the total number of employees and the average salaries of the
different departments.
(o) Calculate the number of employees who are getting any commission.
(p) List the details of all managers in ascending order of joining dates.
(s) Display the minimum, maximum, and average salaries for each job
group.
(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.
(y) Display the name of employee who earns maximum salary whose job is
salesman.
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.
(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
b) List the name and salary of the employees whose salary is more than
1000.
f) List the details of the employees who have joined before the end of
September 81.
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.
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).
n) List the details of the employees whose salary is greater than 2000
and not eligible for commission.
p) List the name, salary and PF amount of all the employees (PF is
calculated as 10% of salary).
Q) SELECT ENAME,EMPNO,SAL
FROM EMP ORDER
BY SAL
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.
u) List the total salary, maximum, minimum and average salary of the
employee’s job wise.
w)List the average monthly salary for each job type within department.
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
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.
Assignment No: -6
(a) List the employees earns more than any employee in CHICAGO.
(b) List the name of the employee who works in the same department as
SMITH.
ENAME
----------
SMITH
JONES
SCOTT
ADAMS
FORD
(c)List the name, employee number ,their manager name and manager
number.
13 rows selected.
IT Department PAGE No:13
(d) List the name of the employee job is same as ‘CLARK’.
ENAME JOB
---------- ---------
JONES MANAGER
BLAKE MANAGER
CLARK MANAGER
(e) List the name of employee whose salary is more than ‘TURNER’.
ENAME
----------
ALLEN
……
……
FORD
6 rows selected.
f) List the name of employee who joined after ‘ALLEN’.
11 rows selected.
g) Display the name of the department whose job is ‘SALESMAN’.
DNAME JOB
-------------- ---------
SALES SALESMAN
Z) Display the name of the department in which ‘FORD’ works.
DNAME JOB
-------------- ---------
RESEARCH ANALYST
AA) Display the name of the department whose salary is maximum.
DNAME
--------------
ACCOUNTING
ENAME SAL
---------- ---------
KING 5000
n) Display the name of the department which has no employee.
p) list the name of the employee who joined in the same year of ‘ADAMS’.
P> SELECT ENAME
q) list the name of the employee who joined in the same month of
‘BLAKE’.
ENAME
----------
ADAMS
MILLER
DNAME
--------------
SALES
Assignmet no: 07
VALUES('&order_no','&order_date','&client_no','&del_add','&salesman_no','&del_t
ype','&del_date');
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)>>
Table created.
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
NO VALUE
--------- --------- 5 120
SQL> @ass81;
NO VALUE
--------- --------- 6 720
SQL> @ass81;
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'.
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
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
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;
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
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;
10 rows selected.
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.
Table created.
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;
SQL> @ass91;
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);
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
DECLARE
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
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;
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);
end loop;
commit;
end;
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
6 rows selected.
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
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