Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Oracle SQL-Lab Set

Database Management Systems Lab Manual for Graduates

Uploaded by

keshavarn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Oracle SQL-Lab Set

Database Management Systems Lab Manual for Graduates

Uploaded by

keshavarn
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Oracle SQL-Lab Manual

To create user login as system:


Connect system/password@HostString;

Creating User:
Create user username identified by password

Granting Privileges to user:


Grant connect, resource to username;

Changing password of any user(Execute if you want to change password):


Alter user username identified by newpassword;

Login to newly created account:


Connect username/password@HostString;

Creating Employee Table:


create table employee
(emp_id number(4) primary key,
first_name varchar2(15) not null,
last_name varchar2(15),
job_id varchar2(10),
doj date default sysdate,
salary number(10,2),
dept_id number(4),
manager_id number(4));

Input data into the employee table:

insert into employee values


(101,'Maheswarappa','CK','Principal','12-jul-1984',95000,1,101);

insert into employee values


(102,'Channakeshava','RN','Asst Proff','08-sep-2009',31000,2,101);

insert into employee values


(103,'Govinda','Raju','Proff','18-sep-1984',91000,3,101);

insert into employee values


(104,'Phalaksha','Murthy','Asst Proff','09-sep-2009',31000,3,103);

insert into employee values


(105,'Sannamma','TV','Proff','10-sep-1985',89000,4,101);

insert into employee values


(106,'Ravi','Kiran','Asst Proff','05-jan-2009',32000,4,105);

insert into employee values


(107,'Krishna','Murthy','Asst Proff','8-sep-2009',31000,4,105);

insert into employee values


(108,'Shilpa','','Asst Proff','18-nov-2009',31000,4,105);

insert into employee values


(109,'Naveen','Kumar','Asst Proff','15-sep-2009',31000,3,103);

insert into employee values


(110,'Balaraj','','Proff','25-may-1985',90000,5,101);

Department of Computer Science, Govt. Science College, Chitradurga Page 1


Oracle SQL-Lab Manual

insert into employee values


(111,'Sheetal','Margankop','Asst Proff','15-jan-2007',90000,5,110);

insert into employee values


(112,'Nagaraja','S','Proff','15-jan-1992',62000,5,110);

insert into employee values


(115,'Dhananjaya',null,'Asst Proff','20-jun-1975',40000,null,null);

insert into employee values


(114,'Anil','Banakar','Proff','20-jun-1975',65552,null,null);
Emp_ID First_name Last_name Job_ID DOJ Salary Dept_ID Manager_ID
115 Dhananjaya Asst Proff 20-Jun-75 40000
114 Anil Banakar Proff 20-Jun-75 65552
101 Vivekananda CM Principal 12-Jul-84 95000 1 101
102 Channakeshava RN Asst Proff 8-Sep-09 31000 2 101
103 Govinda Raju Proff 18-Sep-84 91000 3 101
104 Phalaksha Murthy Asst Proff 9-Sep-09 31000 3 103
106 Ravi Kiran Asst Proff 5-Jan-09 32000 4 105
107 Krishna Murthy Asst Proff 8-Sep-09 31000 4 105
108 Shilpa Asst Proff 18-Nov-09 31000 4 105
105 Sannamma TV Proff 10-Sep-85 89000 4 101
109 Naveen Kumar Asst Proff 15-Sep-09 31000 3 103
110 Balaraj Proff 25-May-85 90000 5 101
120 Ravindranath 28-Sep-10 52975
111 Sheetal Margankop Asst Proff 15-Jan-07 90000 5 110
112 Nagaraja S Proff 15-Jan-92 62000 5 110

Create Department Table:


create table departments
(dept_id number(4),
dept_name varchar2(20),
manager_id number(4));

insert into departments values(1,'Administration',101);


insert into departments values(2,'Computer Science',102);
insert into departments values(3,'Mathematics',103);
insert into departments values(4,'Physics',105);
insert into departments values(5,'Chemistry',110);
insert into departments values(6,'Botany',null);
insert into departments values(7,'Zoology',null);
insert into departments values(null,'Microbiology',null);

Dept_ID Dept_name Manager_ID


1 Administration 101
2 Computer Science 102
4 Physics 105
6 Botany
Department of Computer Science, Govt. Science College, Chitradurga Page 2
Oracle SQL-Lab Manual

7 Zoology
3 Mathematics 103
5 Chemistry 110
Micro Biology

Create Customer Table:


create table customer
(cust_id number(4),
first_name varchar2(15),
last_name varchar2(15),
address varchar2(50),
city varchar2(30),
phone varchar2(20),
email varchar2(30),
primary key (cust_id));

insert into customer values


(001,'Channakeshava','RN','Vemana Nagara','Chitradurga','+919448133333','keshavarn@gmail.com');

insert into customer values


(002,'Phalaksha','Murthy','IUDP Layout','Chitradurga','08194-333333','p.murthy@gmail.com');

insert into customer values


(003,'Krishna','Murthy','IUDP Layout','Chitradurga','+9745454545','krishna.murthy@yahoo.com');

insert into customer values


(008,'Chandrashekhar','Talari','Stadium Road','Chitradurga','08194-344444','chandru.t@gmail.com');

insert into customer values


(005,'Krishna','Kumar','PB Road','Davangere','08192-333333','kumar@rediffmail.com');

insert into customer values


(004,'Narayana','Murthy','ITPL','Bangalore','080-22442255','narayan@infosys.com');

insert into customer values


(007,'Ajim','Premji','Wipro','Bangalore','08055555553','premji@wipro.com');

insert into customer values


(006,'Madan','Gopal','Higher Education Department','Bangalore','08022222223','m.gopal@kar.nic.in');

insert into customer values


(009,'Nagalambika','Devi','Collegiate Education Departmemnt','Bangalore','08022112289','DCE@kar.nic.in');

Cust_ID First_name Last_name Address City Phone e-Mail


1 Channakeshava RN Kelagote Chitradurga 08194-222222 keshavarn@gmail.com
2 Phalaksha Murthy IUDP Layout Chitradurga 08194-333333 p.murthy@gmail.com
3 Krishna Murthy IUDP Layout Chitradurga 9745454545 krishna.murthy@yahoo.com
8 Chandrashekhar Talari Stadium Road Chitradurga 08194-344444 chandru.t@gmail.com
5 Krishna Kumar PB Road Davangere 08192-333333 kumar@rediffmail.com
4 Narayana Murthy ITPL Bangalore 080-22442255 narayan@infosys.com
7 Ajim Premji Wipro Bangalore 8055555553 premji@wipro.com
6 Madan Gopal Higher Education Department Bangalore 8022222223 m.gopal@kar.nic.in
9 Nagalambika Devi Collegiate Education Departmemnt Bangalore 8022112289 DCE@kar.nic.in

Department of Computer Science, Govt. Science College, Chitradurga Page 3


Oracle SQL-Lab Manual

Create Salarygrades table:


create table salgrade
(grade char(1),
highsal number(10,2),
lowsal number(10,2));

insert into salgrade values ('A',200000,100000);


insert into salgrade values ('B',99999,50000);
insert into salgrade values ('C',49999,30000);
insert into salgrade values ('D',29999,10000);
Grade Highsal Lowsal
A 200000 100000
B 99999 50000
C 49999 30000
D 29999 10000

1. Working with Table and data using another table.


a. Create empsal table using first_name,last_name,salary of employee table.
create table empsal as
select first_name,last_name,salary
from employee
where 'true'='false';
b. Copy data from employee to empsal table whose salary is more than 50000.
insert into empsal
select first_name,last_name,salary
from employee
where salary>40000;
c. delete all rows from empsal table.
Delete from empsal;
d. Copy firstname,lastname,salary of all rows from employee to empsal.
insert into empsal
select first_name, last_name, salary
from employee;
e. delete rows from empsal whose salary is less than 70000;
delete from empsal
where salary <70000;
2. Modifying table structure and updating data.
a. Create empsalary table using employee table.
create table empsalary as
select emp_id, first_name, last_name, salary
from employee;
b. Add a new column grade to empsalary table.
alter table empsalary
add (grade char(1));
c. set grades for employees based on salary(d-salary<25000,c if salary between
50000and 25000, b-if salary between 100000 and 50000, a if salary morethan
1lakh).
Update empsalary
set grade=(case when salary <25000 then 'd'
when salary <50000 and salary>=25000 then 'c'
when salary<100000 and salary>=50000 then 'b'
else 'a' end );
d. Drop the grade column;
alter table empsalary
drop column grade;

Department of Computer Science, Govt. Science College, Chitradurga Page 4


Oracle SQL-Lab Manual

3. Keys:
a. Explicitly Adding Primary key:
alter table departments
add constraint dept_pk
primary key (dept_id)

b. Adding a foreign key Explicitly using cascade option.


alter table employee
add constraint empdept_fk
foreign key (dept_id)
references departments(dept_id)
on delete cascade;

c. Check the difference by deleting one row from departments:


select * from employee;
delete from departments where dept_id=3;
select * from employee;

d. Dropping foreign key


Alter table employee
drop constraint empdept_fk;
e. adding a foreign key explicitly using set null option.
alter table employee
add constraint empdept_fk
foreign key (dept_id)
references departments(dept_id)
on delete set null;

select * from employee;


delete from departments where dept_id=3;
select * from employee;
f. Dropping Primary key:
alter table departments
drop constraint dept_pk;

4. Using Where Clause(Comparison, between and set comparision).


Create Employee table with (emp_id, first_name, last_name, job_id, doj, salary,
dept_id, manager_id) and execute the following queries.
a. Display employee ID, name, salary whose salary is more than 50000.
select emp_id, first_name, salary
from employee
where salary > 50000;
b. Display employee ID, name, designation whose designation is ‘Asst Proff’.
select emp_id, first_name, job_id
from employee
where job_id='Asst Proff';
c. Display employee ID, Name, date of joining who has joined to duty between ’01-
jan-1991’ and ‘31-dec-2000’.
select emp_id, first_name, doj
from employee
where doj BETWEEN '01-jan-1991' and '31-dec-2000';
d. Display employee ID, Name, whose employee id is not matching to any of (101,
106, 108, 111).
select emp_id, first_name
from employee
where emp_id not in (101, 106, 108, 111);

Department of Computer Science, Govt. Science College, Chitradurga Page 5


Oracle SQL-Lab Manual

5. Using Where Clause(Matching Characters and NULL values).


Create Employee table with (emp_id, first_name, last_name, job_id, doj, salary,
dept_id, manager_id) and execute the following queries.
a. Display employee ID, Name whose name contains character ‘h’ in the 2nd
position.
select emp_id, first_name, last_name
from employee
where first_name like '_h%';
b. Display employee information whose name contains character ‘n’ in any
position.
select emp_id, first_name, last_name
from employee
where first_name like '%n%';
c. Containing ‘ha’ combination any where in first_name:
select emp_id, first_name || ' ' || last_name "Name"
from employee
where first_name like '%ha%'
d. Display employee information who does not have last name.
select emp_id, first_name, last_name
from employee
where last_name is NULL;
e. Display employee information whose last name is not null.
select emp_id, first_name, last_name
from employee
where last_name is not null;

6. Where Clause (Using Logical operators to join more than one conditions).
a. Display employee information who is a proffessor and whose manager id is not
in (101,102)
select emp_id, first_name, last_name, job_id, DOJ, manager_id
from employee
where job_id = 'Proff' and manager_id not in(101,102);
b. Display employee information who is working in department 3 and his salary is
more than 50000.
select emp_id, first_name, last_name, dept_id, salary
from employee
where dept_id = 3 and salary >=50000;
c. Display employee information who is a proffessor or Principal
select emp_id, first_name, last_name, job_id
from employee
where job_id = 'Proff' or job_id = 'Principal';
d. Display employee information whose job_id is principal or Proffessor and
department id not equal to 4;
select emp_id, first_name, last_name, job_id, DOJ,dept_id
from employee
where (job_id = 'Proff' or job_id ='Principal') and dept_id!=4;

7. Formatting the output Result by putting Column aliases, using expressions and
ordering the Data.
a. Display employeeID,First_name,designation,salary by putting appropriate Column
Aliases.
select emp_id as "Employee ID", first_name as "Employee Name",job_id as
Designation, salary
from employee;
b. Display first_name and Last_name concatenated using a space in between.
select first_name || ' ' || last_name as "Employee Name"
from employee;

Department of Computer Science, Govt. Science College, Chitradurga Page 6


Oracle SQL-Lab Manual

c. Display each employees name and Annual salary.


Select first_name, salary*12 as "anual salary"
from employee;
d. Display employee id,name and his salary in descending order of salary, if
salary is same for more than one they have to be sorted according to
alphabetical order of name in ascending.
select emp_id, first_name, salary
from employee
order by salary desc, first_name asc;
8. Using SubQueries in where Clause.
a. (Set Membership) Display employee ID,name,dept_id who is manager in department
table.
select emp_id, first_name, dept_id
from employee
where emp_id in (select manager_id
from departments);
b. (Set comparison)
Display employee information who is getting more salary than any one of the
employee in department no 5.
select emp_id,first_name,salary
from employee
where salary > some (select salary
from employee
where dept_id=5);
Display employee information who is getting more salary than all of the
employees in department no 5.
select emp_id, first_name, dept_id, salary
from employee
where salary > all (select salary
from employee
where dept_id=5);
c. (Test for Empty Relations)Display employee information whose department is in
department table.
select emp_id,first_name,dept_id
from employee E
where exists (select dept_id
from departments D
where E.dept_id= D.dept_id);
d. (Test for Absence of duplicate Tuples)Display employee Information who is only
one employee in his department.
select emp_id, first_name, dept_id
from employee E
where unique (select D.first_name
from employee D
where D.dept_id=E.dept_id);
9. Sub Queries in From Clause.
a. Display maximum of total salary of any department.
select max(T.total
from (select dept_id, sum(salary) as total
from employee group by dept_id) T;
b. Display dept_id, sum of salary in each dept whose total salary is more than
100000.
select dept_id, total
from (select dept_id, sum(salary) as total
from employee
group by dept_id) T
where total > = 100000;

Department of Computer Science, Govt. Science College, Chitradurga Page 7


Oracle SQL-Lab Manual

c. Display Total salary of each department whose department is not null.


select dept_id,total
from (select dept_id, sum(salary) as total
from employee
group by dept_id) T
where dept_id is not null;
d. Display department name along with no of employees in the department.
select dept_name,count
from (select dept_id,count(*) as count
from employee
group by dept_id) E, departments D
where E.dept_id = d.dept_id;
10. Aggregate Functions:
a. Display minimum, maximum, average, sum, of salary of all employees.
select count(salary), min(salary), max(salary), avg(salary), sum(salary)
from employee;
b. Display minimum, maximum, average, sum, of salary given to employees and no.
of employees in each department:
select dept_id,count(salary),Min(salary),Max(salary),avg(salary),sum(salary)
from employee
group by dept_id;
c. Display minimum, maximum, average, sum, of salary given to employees and no.
of employees under each manager:
select manager_id,count(salary),Min(salary),Max(salary),avg(salary),sum(salary)
from employee
group by manager_id;
d. Display Dept_id,total salary for that department whose total salary is more
than 1,50,000;
select dept_id,sum(salary) as total
from employee
group by dept_id
having sum(salary) >150000;
11. Using single row Functions:
a. Display First_name, remove ‘a’ from ends of first_name, and show length of
first_name.
select first_name, trim('a' from first_name), length(first_name)
from employee;
b. Display first_name, and salary of employee rounded to thousandth place.
select first_name, salary, round(salary,-4)
from employee;

c. Display employee name in small letters, Capital letters, and Initial letters
capitals.
select first_name, upper(first_name), lower(first_name), initcap(first_name)
from employee;
d. Implement Substr(),Instr(),lpad(), and rpad() functions on first_name of
employee table.
select first_name, substr(first_name,2,7), instr(first_name,'h'),
lpad(first_name,15,'*'), rpad(first_name,15,'*')
from employee;
12. Joning More than one table.
a. (Equi-Join)Display employee ID,first_name,job_id,dept_id from employee table
along with dept_name from department table.
select emp_id, first_name, job_id, E.dept_id, dept_name
from employee E, departments D
where E.dept_id = D.dept_id;

Department of Computer Science, Govt. Science College, Chitradurga Page 8


Oracle SQL-Lab Manual

b. (Self–join)Display employee name along with his manager name in the same
department
select E.first_name,M.first_name
from employee E,employee M
where E.manager_id = M.emp_id;
c. (non-equi join)Display Employee name and his grade according to the grades
fixed in the salgrades table.
select emp_id,first_name,salary,grade
from employee, salgrade
where salary between lowsal and highsal
d. Display employee name,salary and his department whose salary is more than
50000.
select emp_id, first_name, salary, dept_name
from employee E, departments D
where E.dept_id = D.dept_id and salary >=50000;

13. SQL Joins.

a. Display employee ID, Name, Dept_id and Dept name who have matching department
in the corresponding department table.
select emp_id, first_name, D.dept_id,dept_name
from employee E inner join Departments D
on E.dept_id= d.dept_id
b. Display employee ID, Name, Dept_id and Dept name who have matching department
in the corresponding department table, along with the employee information who
do not have departments assigned.
select emp_id, first_name, D.dept_id,dept_name
from employee E LEFT OUTER join Departments D
on E.dept_id= d.dept_id;
c. Display employee ID, Name, Dept_id and Dept name who have matching department
in the corresponding department table, along with the Department Names which
do not have any employees in it.
select emp_id, first_name, D.dept_id,dept_name
from employee E Right OUTER join Departments D
on E.dept_id= d.dept_id;
d. Display employee ID, Name, Dept_id and Dept name who have matching department
in the corresponding department table, along with the employee information who
do not have departments assigned and Department Names which do not have any
employees in it.
select emp_id, first_name, D.dept_id,dept_name
from employee E Full OUTER join Departments D
on E.dept_id= d.dept_id;

14. Set Operators.

a. Display Employee Names who are also Customers.


select first_name , last_name
from Employee
INTERSECT
select first_name , last_name
from customer;
b. Display Employee names along with customers eliminating repeated names:
select first_name , last_name
from Employee
UNION
select first_name , last_name
from customer;

Department of Computer Science, Govt. Science College, Chitradurga Page 9


Oracle SQL-Lab Manual

c. Display Employee names along with customers Allowing repeated names:


select first_name , last_name
from Employee
UNION ALL
select first_name , last_name
from customer;
d. Display Employee names who are not customers.
select first_name , last_name
from Employee
MINUS
select first_name , last_name
from customer;
e. Display Customers who are not employees.
select first_name , last_name
from customer
MINUS
select first_name , last_name
from employee;

15. Creating and working with views.


a. Create a view to Display Emp_id,first_name,salary from employee.
Create view empnew as
select emp_id, first_name, salary
from employee;
b. Insert a row to employee table using the view created.
insert into empnew values(120,'Ravindranath',52975);
c. Create a view to display emp_id,first_name concatenated with last_name as
Name, job_id and his department name.
create view empdept as
select emp_id, first_name|| ' ' ||last_name as Name, job_id, dept_name
from employee , departments
where employee.dept_id=departments.dept_id;
d. Create a view to display department name along with maximum salary in the
department.
create view maxsal as
select dept_name, highsal
from (select dept_id, max(salary) as highsal
from employee
group by dept_id) E, departments D
where E.dept_id = d.dept_id;

Department of Computer Science, Govt. Science College, Chitradurga Page 10

You might also like