LAB MANUAL
PART A
(PART A : TO BE REFFERED BY STUDENTS)
Experiment no: - 10
Experiment Name: - Subqueries.
Aim: - Performing practical by executing Subqueries.
Resource required: - Oracle Live SQL
Theory: -
• SUBQUERIES:
- The subquery (inner query) executes once before the main query.
- The result of the subquery is used by the main query (outer query).
Syntax: SELECT select_list
FROM table
WHERE expr operator
(SELECT select_list
FROM table);
e.g.: SELECT last_name
FROM employees
WHERE salary>
(SELECT salary
FROM employees
WHERE last_name = ‘Abel’);
LAST_NAME
King
Kochhar
Types of Subqueries:
- Single-row subquery
- Multiple-row subquery
Single- row Subqueries:
- return only one row and uses single-row comparison operators( =, >, <, and so on).
e.g.: SELECT last_name
FROM employees
WHERE salary>
(SELECT salary
FROM employees
WHERE last_name = ‘Abel’);
LAST_NAME
King
Kochhar
Multiple- row Subqueries:
- Return more than one row and uses multiple-row comparison operators (IN, ANY,
ALL).
e.g.: SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary < ANY
(SELECT salary
FROM employees
WHERE job_id = ‘IT_PROG’)
AND job_id <> ‘IT_PROG’;
EMPLOYEE_ID LAST_NAME JOB_ID SALARY
124 Mourgas ST_MAN 5800
141 Rajs ST_CLERK 3600
142 Davies ST_CLERK 3100
e.g.: SELECT employee_id, last_name, job_id, salary
FROM employees
WHERE salary < ALL
(SELECT salary
FROM employees
WHERE job_id = ‘IT_PROG’)
AND job_id <> ‘IT_PROG’;
EMPLOYEE_ID LAST_NAME JOB_ID SALARY
141 Rajs ST_CLERK 3500
142 Davies ST_CLERK 3100
143 Matos ST_CLERK 2600
e.g.: Null values in a subquery
SELECT emp.last_name
FROM employees emp
WHERE emp.employee_id NOT IN
(SELECT mgr.manager_id
FROM employees mgr);
➢ Multiple-Column Subqueries:
WHERE (MANAGER_ID, DEPARTMENT_ID) IN
Subquery
100 90
102 60
124 50
-Each row of the main query is compared to values from a multiple row and
Multiple-column subquery.
Column comparisons:
Column comparisons in a multiple-column subquery can be:
- Pairwise comparisons
- Nonpairwise comparisons
- Pairewise Comparison Subquery:
e.g.: SELECT employee_id, manager_id, department_id
FROM employees
WHERE (manager_id, department_id) IN
(SELECT manager_id, department_id
FROM employees
WHERE employee_id IN (178,174))
AND employee_id NOT IN (178,174);
-Nonpairewise Comparison Subquery:
e.g.: SELECT employee_id, manager_id, department_id
FROM employees
WHERE manager_id IN
(SELECT manager_id
FROM employees
WHERE employee_id IN (174,141))
AND department_id IN
(SELECT department_id
FROM employees
WHERE employee_id IN (174,141))
AND employee_id NOT IN (174,141);
Using a Subquery in the FROM Clause:
e.g.: SELECT a.last_name, a. salary,
a.department_id, b.salavg
FROM employees a, (SELECT department_id,
AVG (salary) salavg
FROM employees
GROUP BY department_id) b
WHERE a.department_id = b.department_id
AND a. salary > b.salavg;
Conclusion:
In this practical you have learned how to use sub-queries
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the practical. The soft copy
must be uploaded on the Blackboard or emailed to the concerned lab in charge faculties at the end of the
practical in case the there is no Black board access available)
Roll No :- C-68 Name: Sandesh Bhagat
Class: SE-C Batch: C4
Date of Experiment: Date of Submission:
Grade:
B.1: Read the given question carefully and write a SQL statement and check the output.
(Put the query and its output after each question.)
1. Write a query to display the last name and hire date of any employee in the same department as
Zlotkey. Write the query again and this time exclude Zlotkey from the output
Batch c1: Zlotkey
Batch c2: Abel
Batch c3: Vargas
Including ZLOTKEV
2. Create a query to display the employee id’s and last names of all employees who earn more than
the average salary. Arrange the result in descending order of salary.
3. Write a query that displays the employee id and last names of all employees who work in a
department with any employee whose last name contains the alphabet ‘u’ (smallcase)
Batch c1: u
Batch c2: z
Batch c3: b
4. Display the last name, department id, and job ID of all employees whose department location id
is 1700.
Batch c1: 1700
Batch c2: 1500
Batch c3 1800
5. Display the last name and salary of every employee who reports to King. (Note the case of the
last_name)
Batch c1: King
Batch c2: Hunold
Batch c3: Mourgos
6. Display the department id, last name, and job ID for every employee in the Executive
department.
Batch c1:
Executiv
e Batch
c2: IT
Batch c3:
Marketin
g
7. Display the last name, department name, & salary of any employee whose salary
and commission match the salary & commission of any employee located in location
ID 170.
B.2: Write an observation according to the query execution.
• Performance: Subqueries can sometimes result in slower performance, especially if the
subquery is executed multiple times for each row in the outer query. However, modern SQL
databases have optimization strategies that try to mitigate this issue, but performance can still
degrade if the subquery involves a large dataset.
• Complexity: Subqueries increase the complexity of the query, which can make it harder to
read and maintain. They are useful for breaking down complex operations into manageable parts,
but if used excessively, they can make the query harder to understand.
• Execution Order: The subquery is typically executed first (depending on the type of
subquery) and its result is used by the outer query. Understanding this order helps to ensure that
the query works as expected.
• Types of Subqueries:
• Single-row Subquery: If the subquery returns only a single value, it’s used in a
comparison like = in the outer query.
• Multiple-row Subquery: If the subquery returns multiple rows, operators like IN or ANY
are used.
• Correlated Subquery: A subquery that references columns from the outer query. It is
executed for each row of the outer query.
• Optimization: Sometimes subqueries can be rewritten as joins to improve performance,
especially if the subquery returns large datasets. This is important for database optimization and
can result in better execution plans.