0% found this document useful (0 votes)
9 views

PRACTICAL5_6_9_DBMS

The document contains a series of SQL queries and their corresponding answers related to database management, specifically focusing on displaying data from multiple tables using joins, aggregating data, and managing constraints. It includes practical exercises for retrieving employee information, course prerequisites, and salary statistics, as well as adding and removing constraints in a database schema. Each query is followed by its SQL command, demonstrating various database operations.

Uploaded by

DHRUV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

PRACTICAL5_6_9_DBMS

The document contains a series of SQL queries and their corresponding answers related to database management, specifically focusing on displaying data from multiple tables using joins, aggregating data, and managing constraints. It includes practical exercises for retrieving employee information, course prerequisites, and salary statistics, as well as adding and removing constraints in a database schema. Each query is followed by its SQL command, demonstrating various database operations.

Uploaded by

DHRUV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

CE263-DBMS D24CE189

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'.

select * from prereq inner join course on prereq.course_id=course.course_id where


course.title like '%Advanced%' or course.title like 'Advanced%' or course.title like
'%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%';

U & P U Patel Department of Computer Engineering Page 1


CE263-DBMS D24CE189

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;

U & P U Patel Department of Computer Engineering Page 2


CE263-DBMS D24CE189

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';

U & P U Patel Department of Computer Engineering Page 3


CE263-DBMS D24CE189

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: -

U & P U Patel Department of Computer Engineering Page 4


CE263-DBMS D24CE189

U & P U Patel Department of Computer Engineering Page 5


CE263-DBMS D24CE189

Practice
Perform following queries on JOIN for further practice work

Q-1 List employees, their country, and region.

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;

Q-2 Retrieve employees who have changed jobs.

select distinct jc1.employeeid,jc1.employeename from jobchanges jc1 inner join


jobchanges jc2 on jc1.employeeid=jc2.employeeid and jc1.jobtitle<>jc2.jobtitle;

Q-3 Retrieve all departments and employees, including departments without


employees.

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;

U & P U Patel Department of Computer Engineering Page 6


CE263-DBMS D24CE189

Q-4 Retrieve all employees and their managers (self join).

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;

Q-6 Retrieve departments without employees.

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);

U & P U Patel Department of Computer Engineering Page 7


CE263-DBMS D24CE189

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;

U & P U Patel Department of Computer Engineering Page 8


CE263-DBMS D24CE189

Q-8 Retrieve employees working in locations in the USA.

A select e.employee_id,e.first_name,e.last_name,e.department_id,c.country_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 where
c.country_name=’USA’;

SCHEMA: -

U & P U Patel Department of Computer Engineering Page 9


CE263-DBMS D24CE189

U & P U Patel Department of Computer Engineering Page 10


CE263-DBMS D24CE189

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.

select max(salary)-min(salary) as "Difference" from employees;

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.

U & P U Patel Department of Computer Engineering Page 11


CE263-DBMS D24CE189

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;

U & P U Patel Department of Computer Engineering Page 12


CE263-DBMS D24CE189

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: -

U & P U Patel Department of Computer Engineering Page 13


CE263-DBMS D24CE189

U & P U Patel Department of Computer Engineering Page 14


CE263-DBMS D24CE189

Practical – 9
Add and Remove constraint: [Annexure IX]

Q-1 Add primary key constraint on job_id in job table.

A
alter table job add primary key(job_id);

Q-2 Add foreign key constraint on employee table referencing job table.

alter table employee add foreign key(job_id) references job(job_id);

U & P U Patel Department of Computer Engineering Page 15


CE263-DBMS D24CE189

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));

Q-4 Remove primary key constraint on job_id.

alter table job drop primary key cascade;

Q-5 Remove foreign key constraint on employee table.

A
SELECT constraint_name, constraint_type FROM user_constraints WHERE table_name =

U & P U Patel Department of Computer Engineering Page 16


CE263-DBMS D24CE189

'EMPLOYEE';

alter table employee drop constraint SYS_C008396;

SELECT constraint_name, constraint_type FROM user_constraints WHERE table_name =


'EMPLOYEE';

Schema: -

U & P U Patel Department of Computer Engineering Page 17


CE263-DBMS D24CE189

U & P U Patel Department of Computer Engineering Page 18

You might also like