DBMS Assignment
DBMS Assignment
Assignment #02
Assignment #03
1) List the number of employees and average salary for employees in department 20.
2) List name, salary and PF amount of all employees. (PF is calculated as 10% of
basic salary)
3) List names of employees who are more than 2 years old in the company.
4) List the employee details in the ascending order of their basic salary.
5) List the employee name and hire date in the descending order of the hire date.
6) List employee name, salary, PF, HRA, DA and gross; order the results in the
ascending order of gross. HRA is 50% of the salary and DA is 30% of the salary.
7) List the department numbers and number of employees in each department.
8) List the department number and total salary payable in each department.
9) List the jobs and number of employees in each job. The result should be in the
descending order of the number of employees.
10) List the total salary, maximum and minimum salary and average salary of the
employees jobwise.
11) List the total salary, maximum and minimum salary and average salary of the
employees, for department 20.
12) List the total salary, maximum and minimum salary and average salary of the
employees jobwise, for department 20 and display only those rows having an
average salary > 1000
Assignment #02
Assignment #03
1) List the number of employees and average salary for employees in department 20.
2) List name, salary and PF amount of all employees. (PF is calculated as 10% of
basic salary)
3) List names of employees who are more than 2 years old in the company.
4) List the employee details in the ascending order of their basic salary.
5) List the employee name and hire date in the descending order of the hire date.
6) List employee name, salary, PF, HRA, DA and gross; order the results in the
ascending order of gross. HRA is 50% of the salary and DA is 30% of the salary.
7) List the department numbers and number of employees in each department.
8) List the department number and total salary payable in each department.
9) List the jobs and number of employees in each job. The result should be in the
descending order of the number of employees.
10) List the total salary, maximum and minimum salary and average salary of the
employees jobwise.
11) List the total salary, maximum and minimum salary and average salary of the
employees, for department 20.
12) List the total salary, maximum and minimum salary and average salary of the
employees jobwise, for department 20 and display only those rows having an
average salary > 1000
1) SELECT COUNT(empno),AVG(sal)
FROM emp WHERE deptno=20;
2) SELECT ENAME,SAL "SALARY",SAL*.1 "PF"
FROM EMP;
3) SELECT ENAME FROM EMP
WHERE ROUND(SYSDATE-HIREDATE)>720
4) SELECT * FROM EMP
ORDER BY SAL;
5) SELECT ENAME,HIREDATE FROM EMP
ORDER BY HIREDATE DESC;
6) SELECT ENAME,SAL,SAL*.1 "PF",SAL*.5 "HRA", SAL*.3 "DA",
SAL+SAL*.1+SAL*.5+SAL*.3 "GROSS"
FROM EMP
ORDER BY SAL+SAL*.1+SAL*.5+SAL*.3;
7) SELECT DEPTNO,COUNT(EMPNO)
FROM EMP
GROUP BY DEPTNO;
8) SELECT DEPTNO,SUM(SAL)
FROM EMP
GROUP BY DEPTNO;
9) SELECT JOB, COUNT(EMPNO)
FROM EMP
GROUP BY JOB
ORDER BY COUNT(EMPNO);
10) SELECT JOB,SUM(SAL),MAX(SAL),MIN(SAL),AVG(SAL)
FROM EMP
GROUP BY JOB;
11) SELECT DEPTNO,SUM(SAL),MAX(SAL),MIN(SAL),AVG(SAL)
FROM EMP
GROUP BY DEPTNO
HAVING DEPTNO=20;
12) SELECT JOB,SUM(SAL),MAX(SAL),MIN(SAL),AVG(SAL)
FROM EMP
GROUP BY JOB,DEPTNO
HAVING DEPTNO=20 AND AVG(SAL)>1000;
JOIN :
-- The above query display all tuples(specific attributes) with the relation that one tuple(record) of 1st table
is associated with all the tuples of other table and similerly all the tuples of 1st table are associated with
other table.
*/
/*
-- Inner(Equi) join between two table
-- The above query is To display all employee no. and the corresponding department name.
*/
-- SelfJoin .....
-- Find all the employee name and their corresponding manager name.
/*
SELECT emp.empno "Employee No.",dept.dname "Department Name"
FROM dept,emp
WHERE emp.deptno(+)=dept.deptno;
*/
PL/SQL Block:
CURSOR:
declare
cursor cur_emp_ord is select empno,sal from emp order by sal desc;
meno emp.empno%type;
msal emp.sal%type;
begin
open cur_emp_ord;
dbms_output.put_line('Employee No. Salary');
dbms_output.put_line('------------ ------- ');
loop
fetch cur_emp_ord into meno,msal;
exit when (cur_emp_ord%rowcount-1)=5;
dbms_output.put_line(' '||meno||' '||msal);
end loop;
close cur_emp_ord;
end;
/
declare
cursor cur_emp_da is select empno,sal from emp;
meno emp.empno%type;
msal emp.sal%type;
mda emp.da%type;
begin
open cur_emp_da;
loop
fetch cur_emp_da into meno,msal;
exit when cur_emp_da%notfound;
update emp set da=msal*.74 where empno=meno;
end loop;
close cur_emp_da;
end;
/
PL/SQL Assignment:
Write a PL/SQL Block
1) To find the largest from the three numbers.
2) To find the factorial value of any number.
3) To print the Fibonacii Series of n numbers.
4) To compute the area of the circle with radius 2,4,6………40. and store the data into a table ‘circle’
containing attributes ‘radius’ and ‘area’.
5) To accept the marks for three subjects from a student, calculate its average. If average <50 then print
FAIL, average is between 50 to 60 then Second Division, average is between 60 to 75 then First
Division, average is in between 75 and above then print Distinction.
6) To accept the empno from EMP table and calculate the tax on salary based on the following –
Basic Salary Tax
Less than 1500 5% of Salary
1500.2500 7% of Salary
2501.3500 9% of Salary
3501 and above 10%of Salary.
7) To calculate Gross Salary on the basis of basic salary if DA is 40% of basic, HRA is 20% of basic
and PF deduction is 12% of basic salary, update all the records in emp table.
8) To display all Clerks from emp table using CURSOR.
9) To insert the records of all managers from emp table to newemp and also display on the screen using
CURSOR.
10) To delete all employees belonging to department 50 and check if at least one row has been processed
using SQL%FOUND attribute. Insert the value into a table del_history and see the result.
11) To delete all employees whose job = CLERK and update the emp table with jb = CAPTAIN and sal
= 8000. Check if any row have been processed using SQL%NOTFOUND attribute. If it is true then
ROLLBACK and if it is false then COMMIT.
12) To declare a cursor that selects all employees in a given department and raise the salary by 0.05%
and updates the table and also update another table which keeps track of raised amount.
13) To display the five lowest paid employees.
14) Create a TRIGGER for does not permit deletion of any row from emp table on Sunday.
15) Create a table dupemp with the records those are inserted, updated or deleted from emp table using
TRIGGER.
16) Create a function to calculate factorial of any number.
17) Create a procedure for hike the salary by 10% of all employees.
18) Create a function to calculate simple interest.
19) Create a package for displaying the emp_name, in ascending order of their hiredate, highest salary
and rank.
Solution:
/* 1) To find the largest from the three numbers */
declare
no1 number;
no2 number;
no3 number;
begin
no1:=&no1;
no2:=&no2;
no3:=&no3;
if no1>no2 then
if no1>no3 then
dbms_output.put_line('Largest is '||no1);
else
dbms_output.put_line('Largest is '||no3);
end if;
else
if no2>no3 then
dbms_output.put_line('Largest is '||no2);
else
dbms_output.put_line('Largest is '||no3);
end if;
end if;
end;
/
DECLARE
no number;
fact number:=1;
i number;
BEGIN
no:=&no;
FOR i IN 2..no
LOOP
fact:=fact*i;
end loop;
DBMS_OUTPUT.PUT_LINE('fACTORIAL OF '||no||' IS '||fact);
END;
/
DECLARE
no NUMBER;
fn NUMBER:=0;
sn NUMBER:=1;
nn NUMBER;
cn NUMBER:=2;
BEGIN
no:=&no;
if no=1 then
dbms_output.put_line(fn);
end if;
if no>=2 then
dbms_output.put_line(fn||' , '||sn);
end if;
if no>2 then
loop
nn:=fn+sn;
dbms_output.put_line(nn);
fn:=sn;
sn:=nn;
cn:=cn+1;
exit when cn=no;
end loop;
else
dbms_output.put_line('Wrong entry ');
end if;
end;
/
DECLARE
rd circle.radius%type:=2;
ar circle.area%type;
pi constant number:=3.14;
BEGIN
while rd<=40
loop
ar:=pi*rd*rd;
insert into circle values(rd,ar);
rd:=rd+2;
end loop;
end;
/
*/
DECLARE
marks1 number;
marks2 number;
marks3 number;
average number ;
BEGIN
marks1:=&marks1;
marks2:=&marks2;
marks3:=&marks3;
average:=(marks1 + marks2 + marks3)/3;
if average>=75 then
dbms_output.put_line('Distincion');
elsif average>=60 then
dbms_output.put_line('First Division');
elsif average>=50 then
dbms_output.put_line('Second Division');
else
dbms_output.put_line('FAIL');
end if;
end;
/
/*
6) To accept the empno from EMP table and calculate the tax on salary
based on the following –
Basic Salary Tax
Less than 1500 5% of Salary
1500-2500 7% of Salary
2501-3500 9% of Salary
3501 and above 10%of Salary.
*/
DECLARE
eno emp.empno%type;
msal emp.sal%type;
tax number;
BEGIN
eno:=&eno;
select sal into msal from emp where empno=eno;
if msal>3500 then
tax:=msal*0.1;
else
tax:=msal*0.05;
end if;
dbms_output.put_line('Tax = '||tax);
end;
/
/* 7) To calculate Gross Salary on the basis of basic salary if DA is 40% of basic, HRA is 20% of basic
and PF deduction is 12% of basic salary.
*/
DECLARE
basic number;
da number;
hra number;
pf number;
gross number;
BEGIN
basic:=&Basic;
da:=basic*0.4;
hra:=basic*0.2;
pf:=basic*0.12;
gross:=basic+da+hra-pf;
dbms_output.put_line('Gross Salary '||gross);
END;
/
DECLARE
cursor clerk_emp is select ename from emp where job like 'CLERK';
name emp.ename%type;
BEGIN
open clerk_emp;
loop
fetch clerk_emp into name;
exit when clerk_emp%notfound;
dbms_output.put_line(name);
end loop;
end;
/
/* 9) To insert the records of all managers from emp table to newemp and also display on the screen using
CURSOR.
*/
DECLARE
cursor manager_emp is select empno,ename,hiredate,sal from emp where job like 'MANAGER';
eno emp.empno%type;
name emp.ename%type;
hdt emp.hiredate%type;
basic emp.sal%type;
BEGIN
open manager_emp;
dbms_output.put_line('eno Name Date Basic');
loop
fetch manager_emp into eno,name,hdt,basic;
exit when manager_emp%notfound;
insert into newemp values(eno,name,hdt,basic);
dbms_output.put_line(eno||' '||name||' '||hdt||' '||basic);
end loop;
close manager_emp;
end;
/
/* 10) To delete all employees belonging to department 50 and check if at least one row has been processed
using SQL%FOUND attribute. Insert the value into a table del_history and see the result.
*/
BEGIN
/* 12) To declare a cursor that selects all employees in a given department and raise the salary by 0.05%
and updates the table and also update another table which keeps track of raised amount.
*/
declare
cursor cur12 is select empno,ename,sal,deptno from emp where deptno=&deptno;
eno emp.empno%type;
name emp.ename%type;
salary emp.sal%type;
sal1 emp.sal%type;
dn emp.deptno%type;
begin
open cur12;
loop
fetch cur12 into eno,name,salary,dn;
exit when cur12%notfound;
sal1:=salary;
insert into sal_incr values(eno,name,salary);
salary:=salary+salary*0.05;
update emp set sal=salary where deptno=dn and sal=sal1;
end loop;
close cur12;
end;
/
num number;
result number;
BEGIN
num:=#
result:=factorial(num);
dbms_output.put_line('Factorial of '||num||' is '||result);
fact_pro(num,result);
dbms_output.put_line('Factorial of '||num||' is '||result);
end;
/
declare
pr number;
i number;
y number;
begin
pr:=&Principal;
i:=&Interest;
y:=&Year;
dbms_output.put_line('Simple Interest = '||simple_int(pr,i,y));
end;
/
TRIGGER
Triggers are stored programs, which are automatically executed or fired when some events occur.
Triggers are in fact, written to be executed in response to any of the following events:
Benefits of Triggers
Triggers can be written for the following purposes:
Auditing
Creating Triggers
The Syntax for creating a trigger is:
Where,
CREATE [OR REPLACE] TRIGGER trigger_name : Creates or replace an existing trigger with
thetrigger_name.
{BEFORE | AFTER | INSTEAD OF} : This specifies when the trigger would be executed. The INSTEAD
OF clause is used for creating trigger on a view.
{INSERT [OR] | UPDATE [OR] | DELETE}: This specifies the DML operation.
[OF col_name]: This specifies the column name that would be updated.
[ON table_name]: This specifies the name of the table associated with the trigger.
[REFERENCING OLD AS o NEW AS n]: This allows you to refer new and old values for various DML
statements, like INSERT, UPDATE, and DELETE.
[FOR EACH ROW]: This specifies a row level trigger, i.e., the trigger would be executed for each
row being affected. Otherwise the trigger will execute just once when the SQL statement is executed,
which is called a table level trigger.
WHEN (condition): This provides a condition for rows for which the trigger would fire. This clause
is valid only for row level triggers.
Example:
To start with, we will be using the CUSTOMERS table we had created and used in the previous chapters:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
The following program creates a row level trigger for the customers table that would fire for INSERT or
UPDATE or DELETE operations performed on the CUSTOMERS table. This trigger will display the salary
difference between the old values and new values:
CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
When the above code is executed at SQL prompt, it produces the following result:
Trigger created.
Here following two points are important and should be noted carefully:
OLD and NEW references are not available for table level triggers, rather you can use them for
record level triggers.
If you want to query the table in the same trigger, then you should use the AFTER keyword,
because triggers can query the table or change it again only after the initial changes are applied and the
table is back in a consistent state.
Above trigger has been written in such a way that it will fire before any DELETE or INSERT or
UPDATE operation on the table, but you can write your trigger on a single or multiple operations, for
example BEFORE DELETE, which will fire whenever a record will be deleted using DELETE operation on
the table.
Triggering a Trigger
Let us perform some DML operations on the CUSTOMERS table. Here is one INSERT statement which will
create a new record in the table:
Because this is a new record so old salary is not available and above result is coming as null. Now, let us
perform one more DML operation on the CUSTOMERS table. Here is one UPDATE statement which will
update an existing record in the table:
UPDATE customers
SET salary = salary + 500
WHERE id = 2;
When a record is updated in CUSTOMERS table, above create trigger display_salary_changes will be fired
and it will display following result:
Old salary: 1500
New salary: 2000
Salary difference: 500