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

Lecture 11 SQL Continued Nested Queries

This document discusses nested queries, group by, and having clauses in SQL. It provides examples and explanations of: 1) Nested queries which involve placing one query inside another query with an inner query executed first to return values used by the outer query. 2) Group by clauses which group identical data and are often used with aggregate functions. 3) Having clauses which filter groups created by group by clauses since aggregate functions cannot be used with where clauses. 4) Examples are provided to find the second highest salary, departments and employee counts, and highest salary by department to demonstrate these concepts.

Uploaded by

smangla2be22
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Lecture 11 SQL Continued Nested Queries

This document discusses nested queries, group by, and having clauses in SQL. It provides examples and explanations of: 1) Nested queries which involve placing one query inside another query with an inner query executed first to return values used by the outer query. 2) Group by clauses which group identical data and are often used with aggregate functions. 3) Having clauses which filter groups created by group by clauses since aggregate functions cannot be used with where clauses. 4) Examples are provided to find the second highest salary, departments and employee counts, and highest salary by department to demonstrate these concepts.

Uploaded by

smangla2be22
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

SQL Concepts: Nested Queries/ Sub

Queries, Group By and Having Clause


Nested Query

• In SQL, a nested query involves a query that is placed within another query.
Output of the inner query is used by the outer query.

• A nested query has two SELECT statements: one for the inner query and another
for the outer query.
Syntax of Nested Queries

• The basic syntax of a nested query involves placing one query inside of another query.

• Inner query or subquery is executed first and returns a set of values that are then used by the
outer query.

• The syntax for a nested query is as follows:


Types of Nested Queries in SQL

• Subqueries can be either correlated or non-correlated

Non-correlated (or Independent) Nested Queries : Non-correlated (or Independent) subqueries are executed
independently of the outer query. Their results are passed to the outer query.

Correlated subqueries are executed once for each row of the outer query. They use values from the outer query to
return results.
Example 1
• Write a SQL query to display maximum salary from the Emp table
• Write a SQL query to display Employee name who is taking maximum
salary
E_id E_name Dept Salary
1 Ram HR 10000
2 Amrit MRKT 20000
3 Ravi HR 30000
4 Nitin MRKT 40000
5 Varun IT 50000
Write a SQL query to display maximum salary from the Emp table

Select Max(salary) from Emp;

Output : 50,000
Write a SQL query to display maximum salary from the Emp table
Select Max(salary) from Emp;
Output : 50,000

Write a SQL query to display Employee name who is taking maximum


salary Outer Query
Inner Query

Select E_name from employee where Salary = (Select Max(salary) from Emp);
10000 = 50000 False Output = 50000
20000=50000 False
30000=50000 False
40000=50000 False
50000=50000 True
Example 2: Consider a table given below
E_id E_name Dept Salary
1 Ram HR 10000
2 Amrit MRKT 20000
3 Ravi HR 30000
4 Nitin MRKT 40000
5 Varun IT 50000

1. Write a SQL query to display the second highest salary from Emp table.
2. Write a SQL query to display name of employee who is taking second highest
salary.
Example: Consider a table given below
E_id E_name Dept Salary
1 Ram HR 10000
2 Amrit MRKT 20000
3 Ravi HR 30000
4 Nitin MRKT 40000
5 Varun IT 50000

1. Write a SQL query to display the second highest salary from Emp table.
(Select Salary from Emp where Salary <> Select Max(Salary) from Emp);
10000 <> 50000 True
20000<>50000 True
30000<>50000 True
40000<>50000 True 50,000
50000<>50000 False
1. Write a SQL query to display the second highest salary from Emp table.
(Select Salary from Emp where Salary <> Select Max(Salary) from Emp);
10000 <> 50000 True
20000<>50000 True
30000<>50000 True
40000<>50000 True 50,000
The output is 50000<>50000 False

10,000
20,000
30,000
40,000

(Select Max(Salary) from Emp where Salary <> Select Max(Salary) from Emp);
Output : 40,000
2. Write a SQL query to display name of employee who is taking second
highest salary.

Select E_name from Emp where Salary = (Select Max(Salary) from Emp where Salary <> Select Max(Salary) from Emp);
10000 =40000 False
20000=40000 False E_id E_name Dept Salary
30000=40000 False
40000=40000 True 1 Ram HR 10000
2 Amrit MRKT 20000
3 Ravi HR 30000
Output : Nitin will be output
4 Nitin MRKT 40000
5 Varun IT 50000
Example 3:
• Write a query to display all department names along with their
number of employees.
E_id E_name Dept Salary
1 Ram HR 10000
2 Amrit MRKT 20000
3 Ravi HR 30000
4 Nitin MRKT 40000
5 Varun IT 50000

Dept Name No. of


employees
Expected output: HR 2
MRKT 2
IT 1
Brainstorm how to address this ?
GROUP BY Statement/ Clause

• The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of
some functions.
• i.e. if a particular column has the same values in different rows then it will arrange these rows in a
group.
• For Example : Find the number of customers in each country

• It is often used with aggregate functions to group the result-set by one or more columns.
• The SELECT statement is used with the GROUP BY clause in the SQL query.
• WHERE clause is placed before the GROUP BY clause in SQL.
• ORDER BY clause is placed after the GROUP BY clause in SQL.
Example 3: Continue…
• Write a query to display all department names along with their
number of employees.
E_id E_name Dept Salary
1 Ram HR 10000
2 Amrit MRKT 20000
3 Ravi HR 30000
4 Nitin MRKT 40000
5 Varun IT 50000

HR
Select Dept from Emp group by Dept ;
HR
MRKT
MRKT
IT
HR
Select Dept, count (*) from Emp group by Dept ;
HR
MRKT
MRKT
IT
HR 2
MRKT 2
IT 1
Example : Group By
HAVING Clause
• HAVING Clause is used with GROUP BY clause and filter the groups created by the
GROUP BY clause

• The HAVING clause enables you to specify conditions that filter which group
results appear in the results.

• The HAVING clause was added to SQL because the aggregate functions like SUM,
AVG, MIN,MAX,COUNT cannot be used with WHERE clause.

• The WHERE clause places conditions on the selected columns , where as the
HAVING clause places conditions on groups created by the GROUP BY clause
Order of clauses in SQL

• SELECT
• FROM
• WHERE
• GROUP BY
• HAVING
• ORDER BY
Example Table: Emp
WRITE YOUR QUERY HERE
GROUP BY vs HAVING CLAUSE
WHERE vs HAVING Clause
Example : Subqueries
• Write a query to display the highest salary department-wise along with the
names of employees who are taking it.
E_id E_name Dept Salary
1 Ram HR 10000
2 Amrit MRKT 20000
3 Ravi HR 30000
4 Nitin MRKT 40000
5 Varun IT 50000

Select e_name from EMP where Salary IN


(Select dept,max(Salary) from Emp group by dept);
HR 30000
MRKT 40000
IT 50000
IN and NOT IN

• SQL provides the IN and NOT IN comparison operators to test for membership in a set of values.

• It returns true if the value is equal to at least one value in the list, false otherwise
for IN and true if the value is not in the list and false otherwise for NOT IN.

• The types of the values to be tested and the members of the test set have to be
a string or numeric type or null, and have to be compatible with each other, otherwise an
error is raised.
Example: IN and NOT IN
Eid Ename Address
1 Ravi Chd
2 Varun Delhi
Details of employee whose address is either 3 Nitin Pune
Delhi or Pune or Chandigarh 4 Robin Banglore
5 Ammy Chd

WRITE YOUR QUERY Eid Pid Pname Location


1 P1 IOT Bangalore
5 P2 Big data Delhi
3 P3 Retail Mumbai
4 P4 Android Hyderabad
• Find names of employee who are working on a project
Eid Ename Address
EMP
1 Ravi Chd
2 Varun Delhi
3 Nitin Pune
Select Ename from Emp where Eid IN (Select distinct (Eid) from Project) 4 Robin Banglore
5 Ammy Chd
Project
Eid Pid Pname Location
1 P1 IOT Bangalore
5 P2 Big data Delhi
3 P3 Retail Mumbai
4 P4 Android Hyderabad
EXISTS and NOT EXISTS (Used in Correlated nested query)

• The EXISTS condition in SQL is used to check whether the result of a correlated nested query is
empty (contains no tuples) or not.
• The result of EXISTS is a boolean value True or False. It can be used in a SELECT, UPDATE, INSERT
or DELETE statement.
Example: EXISTS
Eid Ename Address
EMP
1 Ravi Chd
2 Varun Delhi
Find the details of employee who is working 3 Nitin Pune
on at least one project 4 Robin Banglore
5 Ammy Chd

Project
Select * from EMP where Eid Exists Eid Pid Pname Location
(Select Eid from project where 1 P1 IOT Bangalore
Emp.Eid=project.Eid)
5 P2 Big data Delhi

Eid Ename Address 3 P3 Retail Mumbai

1 Ravi Chd 4 P4 Android Hyderabad

3 Nitin Pune
4 Robin Banglore
5 Ammy Chd
Example: NOT EXISTS
Eid Ename Address
EMP
1 Ravi Chd
2 Varun Delhi
Find the details of employee who is working 3 Nitin Pune
on at least one project 4 Robin Banglore
5 Ammy Chd

Project
Select * from EMP where Eid Not Exists Eid Pid Pname Location
(Select Eid from project where 1 P1 IOT Bangalore
Emp.Eid=project.Eid)
5 P2 Big data Delhi
3 P3 Retail Mumbai
4 P4 Android Hyderabad
IN vs EXISTS
Correlated Subquery (Synchronized Query)
• It is a sub query that uses the values from outer query.
• Top down approach.
• Query : Find all employees detail who work in department
EMP Dept

Eid Ename Address Eid Did Name


1 A Delhi 1 D1 HR
2 B Pune 2 D2 IT
3 A Chd 3 D3 MRKT
4 B Delhi 4 D4 Testing
5 C Pune
6 D Mumbai
7 E Hyd Eid Ename Address
1 A Delhi
2 B Pune
Select * from EMP where Eid Exists 3 A Chd
(Select Eid from dept where dept.Eid=Emp.Eid)
4 B Delhi
Nested Query Vs Correlated Query Vs Joins ???
Example
• Query : Find details of employee who work in any department

E_id Ename Dept


EMP
1 A E_id Dno Name
2 B 1 D1 IT
3 C 2 D2 HR
4 D 3 D3 MRKT
5 E

Nested Query : Select * from EMP where E_id IN (Select E_id from dept)

Correlated Query : Select * from EMP where E_id Exists (Select E_id from dept where
dept.E_id=Emp.E_id)
Eid Ename
Joins: Select attributes from emp,dept where dept.E_id=Emp.E_id 1 A
2 B
3 C
Thank you

45

You might also like