Oracle SQL-Lab Set
Oracle SQL-Lab Set
Creating User:
Create user username identified by password
7 Zoology
3 Mathematics 103
5 Chemistry 110
Micro Biology
3. Keys:
a. Explicitly Adding Primary key:
alter table departments
add constraint dept_pk
primary key (dept_id)
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;
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;
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;
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;