Practical No: 1
Select queries & joins
Aim (A):- Select queries on single table using alias where & order by clause
->Consider a table Emp with attributes:-
Emp_no
E_name
Job
Hire_date
Salary
Commission
Dept_no
1) List all the details of the Emp table.
Ans: Select * from Emp;
2) List all the Employee names along with their salaries from Emp table.
Ans: Select E_name, Salary from Emp;
3) List all the details of the employee when salary is greater than thousand.
Ans: Select * from Emp where Salary>1000;
4) List all the details when the commission is zero.
Ans: Select * from Emp where Commission=0;
5) List employee number & job of the employees who belong to department number 20.
Ans: Select Emp_no, Job, Dept_no from Emp where Dept_no=20;
6) List the employee name & salary when it is greater than 2000.
Ans: Select E_name, Salary from Emp where salary>2000;
7) List the names of the employees who are working as salesman.
Ans: Select E_name, Job from Emp where Job=salesman;
8) List the names of the employee who are working with department number 30.
Ans: Select E_name, Dept_no from Emp where Dept_no=30;
9) List the names of employees who are not managers with their corresponding jobs.
Ans: Select E_name, Job from Emp where Job! =manager;
10) Display the unique department number from Emp table
Ans: Select distinct Dept_no from Emp;
11) List the employees whose salary is between 1000 & 2000.
Ans: Select E_name from Emp where Salary between 1000 and 2000;
12) List the details where the name starts with ‘w’.
Ans: Select * from Emp where E_name like ’w%’;
13) List the details where employee name starts with ‘m’ & ends with ‘r’.
Ans: Select * from Emp where E_name like ‘m%r’;
14) List the details where job ends with ‘r’.
Ans: select * from Emp where Job like ‘%r’;
15) List the employee number, name & salary where the job is clerk, manager or president.
Ans: Select Emp_no, E_name, Salary from Emp where Job in (‘clerk’, ‘manager’, ‘president’);
16) List the different jobs from the employee table.
Ans: Select distinct Job from Emp;
17) List the employee details where the job is manager & department number is 10.
Ans: Select * from Emp where Job=’manager’ And Dept_no =10;
18) List the names of the employees as a column name headed employee name where commission
is null.
Ans: Select E_name as “Emp_name where Commission is null” from Emp where Commission is null;
19) List the names of the employees & their salaries with the help of the alias employee-alias where
the salary is either 2000, 3000 or 4000.
Ans: Select E_name as “Employee name” salary as “SALARY” from Emp table where Salary=2000 or
Salary=3000 or Salary=4000;
20) List the employee name & job of the employee in the following format with column name as
“Employee works as the employee name is working as job.
Ans: Select ‘The EMPLOYEE”// E_name //”is working as // job as “Employee working as” from Emp;
Aim (B):- Select queries on single table using aggregate function & the group by
clause.
1) Display total minimum, maximum & average salary of the employees by appropriate titles or
column names for the result
Ans: Select Min (Salary) “Minimum, Max (Salary)”Maximum sum (Salary) ”Total”, “Avg(Salary), “
Average” from Emp;
2) Select the different amount of salaries existing in the EMP table
Ans: Select distinct Salary from Emp;
3) Display the total number of rows from the Emp table.
Ans: Select count (*) “count” from Emp;
4) Display the total Salary & count of the Salary on the basis of the Job.
Ans: Select Job, sum (Salary), count (Salary) from Emp group by Job;
5) Display the count of the job by the groups of jobs over department number where the minimum
salary is greater than 800 & sort the result in descending order of the department number.
Ans: Select count (Job) from Emp where (Select Min (Salary) from Emp) > 800 group by having Dept_no
order by Department number desc;
* Consider a relation Dept with the attributes <Dept _no>, <Dept_name>, <Location>
Aim(C):- Query in data from multiple data using all the types of joins
1) Cross Join:
Ans: Select Emp.Emp_no, Emp_name, Emp.Job, Dept. Dept_no, Dept.Dept_name, Dept.
Location from Emp cross join Dept;
2) Inner Join:
Ans: Select Emp.Emp_no, Emp.Emp_name, Emp.Job, Dept. Dept_no, Dept.Dept_name, Dept.
Location from Emp inner join Dept on Emp. Dept_no=Dept. Dept_no;
3) Natural Join:
Ans: Select Emp.Emp_no, Emp.Emp_name, Emp.Job , Dept. Dept_no,
Dept.Dept_name,Dept.Location from Emp natural join Dept;
4) Eqiu-Join:
Ans: Select Emp_no, Emp_name, Emp.Job, Dept_no,Dept_name, Location from Emp join Dept
on Emp. Dept_no=Dept. Dept_no;
5) Outer Join:
A) Left Outer Join:
Ans: Select Emp_no, Emp_name, Job, Dept_no, Dept_name, Location from left outer join Dept on
Emp.Dept_no=Dept. Dept_no;
B) Right Outer Join:
Ans: Select Emp_no, Emp_name, Job, Dept_no, Dept_name, Location from Emp right outer join
Dept_no on Emp.Dept_no=Dept.Dept_no;
C) Full Outer Join:
Ans: Select Emp_no, Emp_name, Job, Dept_no, Dept_name, Location from Emp full outer join Dept
on Emp.Dept_no=Dept.Dept_no;
6) Self Join:
Ans: Select (Emp_name as “Employee_name”) ,( E1,Emp_name as “manager”
Practical No 2:
Subqueries DML & DDL:
Aim(A) : Creating simple tables with constraints
1) Table 1: Client _master
Create table Client_master
Client_no number(4) primary key check (client_no like ‘c%’),
Client_name varchar(10) NOT NULL
Adress varchar(30)
City char (10)
Pincode number(6)
State char(10)
Bal_due number(5)
);
2) Table 2: Sales_order
Create table Sales_order
Order_no number(4) primary key check (order_no like ‘0%’),
Client_no number(4)
Order_date NOT NULL
Sales_address varchar(30)
D-type
D_date
Order_status
Aim(B): Manipulating Data (Insert, Update & Delete)
1) Book
Create table book
Title char(10) primary key
Author char(10)
Publisher char(10)
Category varchar(10)
Year number(4)
Price number(4)
);
2) Distributor
Create table Distributor
Ds_id varchar(6) primary key check (Ds_id like ‘D%’),
Ds_name char(7)
City char(9)
Discount number(7)
Credit
);
3) Orders
Create table Table_order
Order_no number (5) primary key
Title char (10)
Ds_id varchar (6)
Quantity varchar(6)
);
Insert command:
1) Book
Insert into Book values
(‘Closing stock’, ‘Joseph Heller’, ‘Simon & Schuler’, ‘Novel’, 1994, 200);
Insert into Book values
(‘Internet’, ‘’, ‘Microsoft’, ‘Computing’, 1993, 35);
Insert into Book values
(‘Code complete’, ‘Mcomel’, ‘Microsoft’ , ‘Computing’, 1993, 35);
2) Distributor
Insert into Distributor values
(‘D001’, ‘Landmark’, ‘Canada’, 15, 95);
Insert into Distributor values
(‘D002’, ‘Houston’, ‘London’, 40, 35);
Insert into Distributor values
(‘D003’, ‘New York’, ‘Boston’, 10, 25);
3) Orders
Insert into Orders values
(1000, ‘Closing time’,’D001’, 100);
Insert into Orders values
(1001, ‘Code complete’, ‘D002’, 40);
Insert into Orders values
(1002,’Internet’,’D003’, 50);
Update Command:
1) Book
Update Book
Set Author=’Microsoft’
Where Title=’Internet’;
Update Book
Set Price=90
Where Title=’Code complete’;
2) Distributor
Update Distributor
Set City=’London’
Where Ds_id=’D002’;
Update Distributor
Set Discount=20
Where city=’London’;
3) Order
Update Order
Set Title =’Internet’
Where Order_no=1001;
Update Order
Set Title=’Code complete’
Where Order_no=1002;
Delete Command:
1) Delete from Book
Where Title=’Internet’;
2) Delete from Distributor
Where Ds_id=’D002’;
3) Delete from Order
Where title=’Internet’;
Aim(C):- Query In Single & Multiple Table Using Subqueries
1) List the details of the book where the price is greater than the average price & where
the category starts with ‘c’.
Ans: Select * from Book where Price >(Select avg (Price) from Book) AND Category like
‘c%’;
2) List the titles of the book where the distributor is landmark.
Ans: Select the Titles from Order where Ds_id= (Select Ds_id from Order O, Distributor D
where O.Ds_id=D.Ds_id AND D.Ds_name =’Landmark’);
3) List the distributor when the quantity order is among 10, 15 & 25.
Ans: Select D.Ds_name=D.Ds_id from distributor D, Order O, where D.Ds_id=O.Ds_id
AND O.Quantity in (Select Quantity from Order where Quantity in (15,20,25);
4) List the names if all the distributors who are supplying books of software reuse.
Ans: Select D.Ds_name, D.Ds_id from Distributor D, Order O, Book B where D.Ds_id AND
B.Title =O.Title AND B.Category=’software reuse’;
5) List the distinct titles of the book where the distributor exists.
Ans: Select B Title from Book B Distributor D where D Ds_id! =” ”;
6) Find the employee names where their salary is greater than the average salary & they
belong to the same department no.
Ans: Select E.E_name, E.Salary from Emp e ,Department D whre E. avg(Salary)> E.Salary
AND E.Department_no= D.Department_no;
7) List the titles where the quantity is greater than minimum credit having the same
distributor id.
Ans: Select B.Title from Book B, Distributor D, Order O where D.Ds_id=D.Ds_id AND
D.Title=B.Title AND O.Credit >(Select min(Credit) from Order);
8) Display the author & their publisher for the book where title is closing time & price is
less than discount.
Ans: Select Author, Publisher from Book where Title=’Closing time’ AND Price (Select
Discount from Distributor);
9) List the titles of the book when the price is greater than the minimum credit & title
starts with ‘s’ or quantity is greater than 10 ?
Ans: Select B.Title From Book B, Distributor D , Order O where B.Title =O.Title (Select
min(credit) from Distributor ) AND B.Title like ‘S%’ or O.quantity >0;
Practical No: 3
Aim (A): Creating Views, Sequences, Indexes & Synonyms.
1) Create a table for student id to start with 10 increment by 3 & bind it with the student
table also create synonym of the same.
Create table Student
(
Student_id number(3) primary key
Student_name varchar2 (30)
Class varchar2(10)
);
Create sequence stud_id
START with 10
INCREMENT by 3;
Insert into Student
Values
(stud_id.nextval, ’Tom’, ‘abc-a’);
Insert into Student
Values
(stud_id.nextval, ’smith’, ‘abc-a’);
2) Create a synonym for the table Book named as Book synonym & display the contents of
the table
Create public synonym Book synonym for Book;
3) Create view for the following:
a) Where the title starts with‘s’.
Create view vw_Book
As
Select * from Book where title like ‘S%’;
b) Which contains the records of various titles, authors respectively.
Create view vw_Book2
As
Select Title, Authors from Book;
c) Which contains the Distributor id & distributor who belong to the city ‘London’.
Create view vw_Distributor
As
Select Ds_id, Ds_name from Distributor where city=’London’;
d) Contains average quantity on the basis of the Distributor id.
Create view vw_Order
As
Select Ds_id, avg(quantity) as average quantity from Order group by Ds_id;
e) Contains the title of the book for the distributor who have placed order.
Create view vw_Order2
As
Select Title, Ds_name from Order;
Aim (B):- Using set operators, date-time functions, roll up, cube and
grouping sets.
1) Use the date functions of months when the commission is zero & increment the date by 3
months.
Select add_months (’08-Sept-81’, 3) from Emp where Commission =0;
2) Find the number of the months using month_between function.
Select Hire_date, month_between(’28-Sept-81’, ’09-jun-81’) from Emp;
3) Calculate the last date of the moth where the commission is 300.
Select last_day(’09-jun 81’) from Emp where Commission=300;
4) Return the next day from the Emp table where the name of the employee is ‘martin’.
Select next_day (’21-feb-81’, ‘1, sat’) from Emp where Emp_name=’martin’;
5) Find the system date from the system .
Select sysdate from dual;
Aim (C): roll up, cube and grouping sets.
1) Find out the job, department number from the Emp table group by the job &
department number using the roll up function.
Select Job, Dept_no from Emp group by rollup (Job, Dept_no);
2) Group by Job & department number using the cube function.
Select Job , Dept_no from Emp group by cube (Job, Dept_no);
Aim (D): Granting and revoking privileges on user objects.
Create user abc01
Identified by abc02;
Grant Select on Emp to abc01.
Revoke select on Emp from abc02;
Practical no:- 04 Working With
Advance Subqueries & with clause
Aim: - Implement hierarchical retrieval on Emp table
1) When the manager is null.
Select Emp_no, Emp_name level from Emp
Where manager is null
Start with Emp_no=7853
Connect by prior EMp_no=manager
Order by level ,Emp_no;
2) When the Emp_no starts with 7499.
Select Emp_no, Emp_name level from Emp
With Emp_no=7499
Connect By level, Emp_no;
Practical 5: Basic PL/SQL, INDEX BY tables,
PL/SQL Record and FOR loop.
Aim (A):- Creating anonymous pl/sql block
1) Create and execute a simple anonymous block that prints hello world.
--> begin
dbms_output.put_line('hello world');
End;
/
2) Create and execute a simple anonymous block that gives the current date.
-->declare
system_date Date;
Begin
select sysdate Into system_date from dual;
dbms_output.put_line('the date is::||system_date);
End;
/
3) Create a block to update salary of employee by incrementing the sal by 1000.
--> Begin
update=emp
set sal=sal+1000
where empno=7844;
End;
/
4) Create a block of employee 7844 can be deleted.
-->Begin
delete from emp
where empno=7844;
End;
/
5) Write a program to print number of people working in a department.
-->Declare
e.count number(10);
BEGIN
Select count(ename) Into ecount from emp;
where job='clerk'
dbms_output.put_line(ecount);
End;
/
6) Create a pl/sql block to find the area of circle where the variables pi is declared as constant & the area of
the circle is calculated must be stored in the table area.
-->DECLARE
pi constant number:=3.14;
rad number(5,2);
area number(10,2);
BEGIN
rad=10;
area:=pi*rad*rad;
dbms_output.put_line('radius:='||rad);
dbms_output.put_line('area:=||area);
End;
/
Aim (B):- Define create index by table process a number of rows from a table and populate another table.
1) Write a program to find the sum of first ten given nos.
-->DECLARE
s number;
BEGIN
s:=0;
loop
s:=s+2;
Exit when s=20;
End loop;
dbms_output.put_line('the sum of first 10 even number=115');
End;
/
2) Write a program to check whether a character is a vowel or constant.
-->DECLARE
character char(i):=upper('&chara C');
result varchar(20);
BEGIN
result:=
Case character
when 'A' then 'It is a vowel'
when 'E' then 'It is a vowel'
when 'I' then 'It is a vowel'
when 'O' then 'It is a vowel'
when 'U' then 'It is a vowel'
Esle:- IT is a constant
End;
dbms_output.put_line(result);
End;
/
3) Write a program to print reverse of the number.
-->DECLARE
n number (10):=(&num b);
remain number(10);
s.number(10);
temp number(10);
BEGIN
s:=0;
temp:=n;
while n>0 loop
remain:=mod(n,10);
s:=(s*10)+remain;
n:=trunc(n/10);
End loop;
dbms_output.put_line('the reverse of'||temp||'is='||s);
End;
/
4) Write a program to check it is palindrome or not.
-->DECLARE
n number (10):=(&num b);
remain number(10);
s.number(10);
temp number(10);
BEGIN
s:=0;
while n>0 loop
remain:=mod(n,10);
s:=(s*10)*remain;
n:=trunc(n/10);
End loop;
if(s=temp)then
dbms_output.put_line('the number is'|| 'a palindrome');
Else
dbms_output.put_line('the number is'|| 'not a palindrome');
End if;
End;
/
5).Write a program to print the numbers from 1 to 100.
-->DECLARE
N NUMBER(3):=1;
V VARCHAR2(1000);
BEGIN
WHILE N <=1000
LOOP
V:=V||''||N;
N:=N+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE(V);
END;
/
6)Write a program to print 1st 10 natural number.
--> DECLARE
n number:=10;
BEGIN
FOR : 1 n 1..n loop
dbms_output.put_line(1);
Endloop;
End;
/
7) Write a program to display employee name and sal by cursor.
-->DECLARE
eid emp.ename%type;
en emp.sal%type;
cursor c_emp IS
select ename,sal from emp;
BEGIN
OPEN c_emp;
Loop
Fetch c_emp into eid,en;
dbms_output.put_line(eid||' '||en);
EXIST when c_emp%not found;
End Loop;
Close c_emp;
End;
/
8) Write a program to read the salary of the employee from table and update the salary than 1500 by 2000.
-->DECLARE
t number;
BEGIN
update emp
set sal=sal+1500;
if sal% found then
t:=sal% rowcount;
dbms_output.put_line('salary for'||t||'rows are updated');
End If;
End;
/
9) Write a program to insert a square of natural number.
-->DECLARE
S_result number(20);
n_number(20);
BEGIN
n:=(&n);
for c_n in 1..n loop
s_result:=c_n*c_n;
insert into data VALUES(S_result);
END loop;
END;
/
10) Write a program to print Fibonacci series up to 10.
-->DECLARE
FEBO NUMBER:=1;
V VARCHAR2(100);
BEGIN
FOR I IN 1..10
LOOP
FOR J IN 1..I
LOOP
FEBO:=FEBO*J;
V:=J||'*'||V;
END LOOP;
DBMS_OUTPUT.PUT_LINE(RTRIM(V,'*')||'='||FACT);
FEBO:=1;
V:=NULL;
END LOOP;
END;
/
11) Write a program to insert the string by user and length in a table.
-->DECLARE
String varchar(20):=(&str);
BEGIN
INSERT INTO str_table values (String,(String));
END;
/
Practical No:-06
1) Using function calculate area of triangle.
-->create or replace function
triangle_area(l number,b number)
return number is area number;
BEGIN
area:=1/2*l*b;
return area;
END;
/
DECLARE
len number:=&length;
bre number:=&breath;
BEGIN
dbms_output.put_line('area of triangle');
dbms_output.put_line('tirangle_area(len,bre));
END;
/
2) Write a program to find simple interest (procedure).
-->create or replace procedure
proc_simple(p number,n number,r number)
IS
BEGIN
sim_in:=p*n*r/100;
dbms_output.put_line('simple intrest is');
dbms_output.put_line(sim_in);
END;
/
exec proc_simple(1000,2,10.60);
3) Write a program to display record from employee table employee name starts with ‘a’ (procedure).
--> create or replace procedure
proc_emp
IS
v_emp emp% rowtype;
cursor emp_cur is
select *from emp;
where ename like 'a%';
BEGIN
open emp_cur;
loop
FETCH emp_cur into v_emp;
dbms_output.put_line(v_emp.ename||' '||v_emp.empno||' '||v_emp.sal||' '||v_emp.deptno);
Exit when emp_cur % not found;
END loop;
close emp_cur;
END;
/
exec proc_emp;
4) Write a function to calculate the sum of employee salary who belongs to deptartment no 10.
-->create or replace function
emp_sal
return nmber is
v_sal number;
BEGIN
Select sum(salary)into v_salary from emp where
deptno=10;
Group by deptno;
Return v_salary;
END;
/
select emp_sal from dual;
5) Write a procedure to display record from employee table where salary >1000.
-->create or replace procedure
emp_proc
IS
v_emp emp% rowtype;
cursor emp_cur is
Select * from emp;
where sal>1000;
BEGIN
open emp_cur;
loop
Fetch emp_cur into v_emp;
dbms_output.put_line(v_emp.empname||' '||v_emp.empno||' '||v_emp.sal||' '||v_emp.deptno);
Exit when emp_cur% not found;
END loop;
close emp_cur;
END;
/
exec emp_proc;
Practical No:- 07
1) Create calculator using packages.
-> create or replace pakkage calc 12
AS
function add (a in number,b in number)
return number;
function sub (a in number,b in number)
return number;
procedure mul (a in number,b in number, c out number);
procedure div( a in number,b in number,c out number);
END;
/
create or replace package body calc 12
IS
function add(a in number,b in number)
return number
IS
c number;
BEGIN
c:=a+b;
return c;
END;
function sub(a in number,b in number)
return number
IS
c number;
BEGIN
c:=a-b;
return c;
END;
procedure mul(a in number,b in number,c out number)
return number
IS
c number;
BEGIN
c:=a*b;
return c;
END;
procedure div(a in number,b in number,c out number)
return number
IS
c number;
BEGIN
c:=a/b;
return c;
END;
/
DECLARE
x number:=(&x);
y number:=(&y);
z number;
gr number:=(&gr);
BEIGN
when gr=1 then
z:=calc 12.add(x,y);
dbms_output.put_line(z);
when gr=2 then
z:=calc 12.sub(x,y);
dbms_output.put_line(z);
when gr=3 then
z:=calc 12.mul(x,y,z);
dbms_output.put_line(z);
when gr=4 then
z:=calc 12.div(x,y,z);
dbms_output.put_line(z);
else
dbms_output.put_line('no');
END case;
END;
/
Aim :-CREATE DML TRIGGERS
1)Create a trigger for after insert on table.
-->create or replace trigger first_trigger
after insert on product
for each row
BEGIN
bdms_output.put_line('product id'||'new product'||'product name:'||'new
prod_name'||'company'||'ne.comp_name'||'unit.price:'||new.unit_price');
END;
/
2)Create statement level trigger before update on table.
-->create or replace trigger stmnt_cmp
before update on emp
BEGIN
bdms_output.put_line('before update,statement level',sysdate);
END;
/
3) Create statement level trigger after update on table.
-->create or replace trigger stmnt_cmp
after update on emp
BEGIN
bdms_output.put_line('after update,statement level',sysdate);
END;
/
4) Create row level trigger before update on table.
-->create or replace trigger row_emp
before update on emp
for each row
BEGIN
bdms_output.put_line('before update,rowlevel','of emp table');
END;
/
5) Create row level trigger after update on table.
-->create or replace trigger row_emp
after update on emp
for each row
BEGIN
bdms_output.put_line('after update row level','of emp table');
END;
/