Advanced SQL All Practicals
Advanced SQL All Practicals
Emp_no
E_name
Job
Hire_date
Salary
Commission
Dept_no
2) List all the Employee names along with their salaries from Emp table.
3) List all the details of the employee when salary is greater than thousand.
5) List employee number & job of the employees who belong to department number 20.
6) List the employee name & salary when it is greater than 2000.
8) List the names of the employee who are working with department number 30.
9) List the names of employees who are not managers with their corresponding jobs.
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’.
13) List the details where employee name starts with ‘m’ & ends with ‘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’);
17) List the employee details where the job is manager & department number is 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;
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:
3) Natural Join:
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;
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;
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:
Practical No 2:
Subqueries DML & DDL:
Aim(A) : Creating simple tables with constraints
1) Table 1: Client _master
2) Table 2: Sales_order
2) Distributor
);
3) Orders
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);
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
Delete Command:
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.
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 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.
2) Create a synonym for the table Book named as Book synonym & display the contents of
the table
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’;
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.
3) Calculate the last date of the moth where the commission is 300.
4) Return the next day from the Emp table where the name of the employee is ‘martin’.
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);
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;
/
--> Begin
update=emp
set sal=sal+1000
where empno=7844;
End;
/
Aim (B):- Define create index by table process a number of rows from a table and populate another table.
-->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;
/
-->DECLARE
character char(i):=upper('&chara C');
result varchar(20);
BEGIN
result:=
Case character
when 'A' then 'It is a vowel'
Esle:- IT is a constant
End;
dbms_output.put_line(result);
End;
/
-->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;
/
-->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;
/
--> DECLARE
n number:=10;
BEGIN
FOR : 1 n 1..n loop
dbms_output.put_line(1);
Endloop;
End;
/
-->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;
/
-->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;
/
-->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.
DECLARE
len number:=&length;
bre number:=&breath;
BEGIN
dbms_output.put_line('area of triangle');
dbms_output.put_line('tirangle_area(len,bre));
END;
/
exec proc_simple(1000,2,10.60);
3) Write a program to display record from employee table employee name starts with ‘a’ (procedure).
exec proc_emp;
4) Write a function to calculate the sum of employee salary who belongs to deptartment no 10.
5) Write a procedure to display record from employee table where salary >1000.
exec emp_proc;
Practical No:- 07
1) Create calculator using packages.
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);
else
dbms_output.put_line('no');
END case;
END;
/