PRACTICAL5_6_9_DBMS
PRACTICAL5_6_9_DBMS
Practical – 5
Displaying data from Multiple Tables (join): [Annexure V]
Q-1 Find advisors for students in departments whose names start with "Comp".
A
select * from instructor inner join advisor on id=i_id and dept_name like 'Comp%';
Q-2 List courses that are prerequisites for courses with titles containing 'Advanced'.
Q-3 Find instructors teaching courses with "Programming" in the course title or classroom
number.
A
select ins.id,cor.course_id,t.id,t.course_id,ins.name,cor.title from instructor ins inner join teaches t
on t.id=ins.id inner join course cor on cor.course_id=t.course_id where cor.title like '%Programming'
or cor.title like 'Programming%' or cor.title like '%Programming%';
Q-4 Write a query to display the last name, department number, and department name for all
employees.
A
select last_name,employees.department_id,d.department_name from employees inner join
departments d on employees.department_id=d.department_id;
Q-5 Display the employee last name and department name for all employees who have an a
(lowercase) in their last names.
A
select last_name,department_name from employees e inner join departments d on
e.department_id=d.department_id where e.last_name=lower(last_name);
Q-6 Display the employee last name and employee number along with their manager’s last
name and manager number. Label the columns Employee, Emp#, Manager, and Mgr#,
respectively.
A
select e1.last_name as "employee", e1.employee_id as "emp#", e2.last_name as "manager",
e2.employee_id as "mgr#" from employees e1 left join employees e2 on e1.manager_id =
e2.employee_id;
Q-7 Modify (6) to display all employees including King, who has no manager. Order the
results by the employee number
A
select e1.last_name as "employee", e1.employee_id as "emp#", e2.last_name as "manager",
e2.employee_id as "mgr#" from employees e1 left join employees e2 on e1.manager_id =
e2.employee_id order by e1.employee_id;
Q-8 Create a query to display the name and hire date of any employee hired after employee
Davies.
A
select distinct e1.employee_id, e1.first_name as "employee name", e1.hire_date as "hired date"
from employees e1 left join employees e2 on e1.department_id = e2.department_id where
e1.first_name != 'davies' and e1.hire_date > '19-mar-05';
Q-9 Display the names and hire dates for all employees who were hired before their
managers, along with their manager’s names and hire dates. Label the columns
Employee, Employee Hired, Manager, and Mgr Hired, respectively.
A
select e1.first_name as "Employee", e1.hire_date as "Hired", e2.first_name as
"Manager",e2.hire_date as "Manager Hired" from employees e1 left join employees e2 on
e1.manager_id = e2.employee_id where e2.hire_date>e1.hire_date;
Q-10 Create a unique listing of all jobs that are in department 80. Include the location of the
department in the output.
A
select distinct jobs.job_id, jobs.job_title, departments.department_name, locations.street_address
from jobs inner join employees on jobs.job_id = employees.job_id inner join departments on
employees.department_id = departments.department_id inner join locations on
departments.location_id = locations.location_id where departments.department_id = 80;
SCHEMA: -
Practice
Perform following queries on JOIN for further practice work
A
select
e.employee_id,e.first_name,e.last_name,e.department_id,c.country_name,r.region_name
from employees e inner join departments d on d.department_id=e.department_id inner join
locations l on l.location_id=d.location_id inner join countries c on c.country_id=l.country_id
inner join regions r on r.region_id=c.region_id;
A
select d.department_id, d.department_name, e.employee_id, e.first_name, e.last_name, e.job_id,
e.department_id from departments d left join employees e on d.department_id = e.department_id;
A
select e1.employee_id,e1.first_name,e1.last_name,e1.job_id, e2.first_name as "Manager First
name",e2.last_name as "Manager Last name", e2.employee_id as "Manager ID" from employees e1
left join employees e2 on e1.manager_id = e2.employee_id;
Q-5 Retrieve all employees who have the same job title as their manager.
A
select e1.first_name,e1.last_name,e1.job_id,e2.manager_id,e2.first_name,e2.last_name,e2.job_id
from employees e1 inner join employees e2 on e1.manager_id = e2.employee_id where e1.job_id =
e2.job_id;
A
select distinct d.department_id,d.department_name from departments d left join employees e1 on
d.department_id NOT IN (e1.department_id);
Q-7 Retrieve job changes along with new and old departments.
A
select jc_old.employeeid,jc_old.employeename,jc_old.jobtitle as oldjobtitle,d_old.departmentname
as olddepartment,jc_new.jobtitle as newjobtitle,d_new.departmentname as
newdepartment,jc_new.changedate from jobchanges jc_old inner join jobchanges jc_new on
jc_old.employeeid=jc_new.employeeid and jc_old.changedate<jc_new.changedate inner join depts
d_old on jc_old.departmentid=d_old.departmentid inner join depts d_new on
jc_new.departmentid=d_new.departmentid where jc_old.changedate<jc_new.changedate order by
jc_old.employeeid,jc_new.changedate;
SCHEMA: -
Practical – 6
To apply the concept of Aggregating Data using Group functions: [Annexure VI]
Q-1 Display the highest, lowest, sum, and average salary of all employees. Label the
columns Maximum, Minimum, Sum, and Average, respectively. Round your results
to the nearest whole number.
A
select round(max(salary)) as maximum,round(min(salary)) as minimum, round(sum(salary)) as
sum, round(avg(salary)) as average from employees;
Q-2 Write a query that displays the difference between the highest and lowest salaries.
Label the column DIFFERENCE.
Q-3 Create a query that will display the total number of employees and, of that total, the
number of employees hired in 1995, 1996, 1997, and 1998.
A
select count(*) as "Total Employees",sum(case when to_char(hire_date,'YY')='05' then 1 else 0 end)
as "Hired in 2005",sum(case when to_char(hire_date,'YY')='06' then 1 else 0 end) as "Hired in 2006",
sum(case when to_char(hire_date,'YY')='07' then 1 else 0 end) as "hired_in 2007",sum(case when
to_char(hire_date,'YY')='08' then 1 else 0 end) as "Hired in 2008" from employees;
Q-4 Find the average salaries for each department without displaying the respective
department numbers.
A
select avg(salary) as "Average Salary" from employees group by department_id;
Q-5 Write a query to display the total salary being paid to each job title, within each
department.
A
select department_id,job_title,sum(salary) as "Total Salary" from employees,jobs group by
department_id,job_title;
Q-6 Find the average salaries > 2000 for each department without displaying the respective
department numbers.
A
select avg(salary) as "Average Salary" from employees group by department_id having
avg(salary)>2000;
Q-7 Display the job and total salary for each job with a total salary amount exceeding 3000
and sorts the list by the total salary.
A
select job_title,sum(max_salary) as "Total Salary" from jobs group by job_title having
sum(max_salary)>3000 order by sum(max_salary);
SCHEMA: -
Practical – 9
Add and Remove constraint: [Annexure IX]
A
alter table job add primary key(job_id);
Q-2 Add foreign key constraint on employee table referencing job table.
Q-3 Add composite primary key on lock table (lock table does not exist, while creating
table add composite key).
A
Create table lock_ (id int not null, name varchar(20) not null, constraint ck_locked primary
key(id,name));
A
SELECT constraint_name, constraint_type FROM user_constraints WHERE table_name =
'EMPLOYEE';
Schema: -