§ Query with in another Query is known as subquery.
§ If you want to fetch data from one table but based on condition of another table then we should go for Subquery.
§ Types of Subquery: -
1. Single row Subquery (getting single value) (using = operator)
2. Multi Sub Query (if query generating more than output value is called [In operator uses]
v A subquery may occur in:
§ A subquery is usually added within the WHERE Clause of another SQL SELECT statement.
Execution Process-
§ Inner most Query To outermost Query except in the case of co-related query.
§ The inner query executes first before its parent query so that the results of an inner query can be passed to the outer query.
Que-
In which case joins required and In Which case Subquery is required?
Ans-
§ If you want to fetch data from multiple table get placed one column from table and another column from second table i.e. if you want to fetch two columns from two table definitely, we should use joins.
§ Subquery said if you are expecting the data from one table but based on condition of another table it is better to use subquery.
select DEPARTMENTNAME from tbldepartment where id=(select DEPARTMENTID from tblemployee where id=1 )
Write a query to display senior most employee details from emp table.
from emp
Write an query to display junior most employee details from emp table.
Write a query to display highest sal details emp table
select *
from empWrite a query to display Second highest sal details emp table.
from emp
Post a Comment