Lecture 11 SQL Continued Nested Queries
Lecture 11 SQL Continued Nested Queries
• 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.
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
Output : 50,000
Write a SQL query to display maximum salary from the Emp table
Select Max(salary) from Emp;
Output : 50,000
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
• 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
• 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
• 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
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
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