Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

UNIVERSITY OF CHITTAGONG

Course Details:
Database Systems
CSE-414
Assignment-2

Submitted to:
Dr. Rudra Pratap Deb Nath
Associate Professor
Department of Computer Science and Engineering
University of Chittagong

Submitted by:
Umme Fahmida Trisha
ID:20701027
Department of Computer Science and Engineering
University of Chittagong

Submission Date:
January 22, 2024

1
Contents
1 Chapter : 18 3
1.1 Problem 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Problem 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Problem 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Problem 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.5 Problem 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.6 Problem 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.7 Problem 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.7.1 Answer : a. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.7.2 Answer : b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.8 Problem 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.9 Problem 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.10 Problem 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.11 Problem 11 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2
1 Chapter : 18
1.1 Problem 1
@: Problem Statement
Write a query to display the last name, department number, and salary of any employee whose
department number and salary both match the department number and salary of any employee
who earns a commission.

1 SELECT last_name , department_id , salary


2 FROM hr . employees
3 WHERE ( salary , department_id ) IN
4 ( SELECT salary , department_id
5 FROM hr . employees
6 WHERE commission_pct IS NOT NULL ) ;

OUTPUT :

3
1.2 Problem 2
@: Problem Statement
Display the last name, department name, and salary of any employee whose salary and com-
mission match the salary and commission of any employee located in location ID1700.

1 SELECT e . last_name , d . department_name , e . salary


2 FROM hr . employees e , hr . departments d
3 WHERE e . department_id = d . department_id
4 AND ( salary , NVL ( commission_pct ,0) ) IN
5 ( SELECT salary , NVL ( commission_pct ,0)
6 FROM hr . employees e , hr . departments d
7 WHERE e . department_id = d . department_id
8 AND d . location_id = 1700) ;

OUTPUT :

4
1.3 Problem 3
@: Problem Statement
Create a query to display the last name, hire date, and salary for all employees who have the
same salary and commission as Kochhar.
Note: Do not display Kochhar in the result set

1 SELECT last_name , hire_date , salary


2 FROM hr . employees
3 WHERE ( salary , NVL ( commission_pct ,0) ) IN
4 ( SELECT salary , NVL ( commission_pct ,0)
5 FROM hr . employees
6 WHERE last_name = ’ Kochhar ’)
7 AND last_name != ’ Kochhar ’;

OUTPUT :

5
1.4 Problem 4
@: Problem Statement
Create a query to display the employees who earn a salary that is higher than the salary of all
of the sales managers (JOB ID = ’SA MAN’). Sort the results on salary from highest to lowest.

1 SELECT last_name , job_id , salary


2 FROM hr . employees
3 WHERE salary > ALL ( SELECT salary
4 FROM hr . employees
5 WHERE job_id = ’ SA_MAN ’)
6 ORDER BY salary DESC ;

OUTPUT :

6
1.5 Problem 5
@: Problem Statement
Display the details of the employee ID, last name, and department ID of those employees who
live in cities whose name begins with T

1 SELECT employee_id , last_name , department_id


2 FROM hr . employees
3 WHERE department_id IN
4 ( SELECT department_id
5 FROM hr . departments
6 WHERE location_id IN
7 ( SELECT location_id
8 FROM hr . locations
9 WHERE city LIKE ’T % ’) ) ;

OUTPUT :

7
1.6 Problem 6
@: Problem Statement
Write a query to find all employees who earn more than the average salary in their departments.
Display last name, salary, department ID, and the average salary for the department.Sort by
average salary. Use aliases for the columns retrieved by the query as shown in the sample output.

1 SELECT e . last_name ename , e . salary salary ,


2 e . department_id deptno , AVG ( a . salary ) dept_avg
3 FROM hr . employees e , hr . employees a
4 WHERE e . department_id = a . department_id
5 AND e . salary > ( SELECT AVG ( salary )
6 FROM hr . employees
7 WHERE department_id = e . department_id )
8 GROUP BY e . last_name , e . salary , e . department_id
9 ORDER BY AVG ( a . salary ) ;

OUTPUT :

8
1.7 Problem 7
@: Problem Statement
Find all employees who are not supervisors.
a. First do this using the NOT EXISTS operator.
b. Can this be done by using the NOT IN operator? How, or why not?

1.7.1 Answer : a

1 SELECT outer . last_name


2 FROM hr . employees outer
3 WHERE NOT EXISTS ( SELECT ’X ’
4 FROM hr . employees inner
5 WHERE inner . manager_id = outer . employee_id ) ;

OUTPUT :

9
1.7.2 Answer : b

1 SELECT outer . last_name


2 FROM hr . employees outer
3 WHERE outer . employee_id
4 NOT IN ( SELECT inner . manager_id
5 FROM hr . employees inner ) ;

1.8 Problem 8
@: Problem Statement
Write a query to display the last names of the employees who earn less than the average salary
in their departments.

1 SELECT last_name
2 FROM hr . employees outer
3 WHERE outer . salary < ( SELECT AVG ( inner . salary )
4 FROM hr . employees inner
5 WHERE inner . department_id =
outer . department_id ) ;

OUTPUT :

10
1.9 Problem 9
@: Problem Statement
Write a query to display the last names of the employees who have one or more coworkers in
their departments with later hire dates but higher salaries.

1 SELECT last_name
2 FROM hr . employees outer
3 WHERE EXISTS ( SELECT ’X ’
4 FROM hr . employees inner
5 WHERE inner . department_id = outer . department_id
6 AND inner . hire_date > outer . hire_date
7 AND inner . salary > outer . salary ) ;

OUTPUT :

11
1.10 Problem 10
@: Problem Statement
Write a query to display the employee ID, last names, and department names of all employees.
Note: Use a scalar subquery to retrieve the department name in the SELECT statement.

1 SELECT employee_id , last_name ,


2 ( SELECT department_name
3 FROM hr . departments d
4 WHERE e . department_id = d . department_id ) department
5 FROM hr . employees e
6 ORDER BY department ;

OUTPUT :

12
1.11 Problem 11
@: Problem Statement
Modify the query in lab 05 04.sql to display the minimum, maximum, sum, and average salary
for each job type. Resave lab 05 04.sql as lab 05 05.sql. Run the statement in lab 05 05.sql.

1 SELECT job_id ,
2 ROUND ( MAX ( salary ) ) AS " Maximum " ,
3 ROUND ( MIN ( salary ) ) AS " Minimum " ,
4 ROUND ( SUM ( salary ) ) AS " Sum " ,
5 ROUND ( AVG ( salary ) ) AS " Average "
6 FROM employees
7 GROUP BY job_id ;

OUTPUT :

13

You might also like