The Sub Query in SQL

Sub query in SQL is basically referred to a query inside a query, to restrict the data further depending upon certain conditions set by us.
In SQL, sub queries are used with commands like SELECT, UPDATE, DELETE or INSERT along with comparison operators like Equals to, Less than or Greater than Equal to.

Step 1- Query Structure
Suppose a scenario in which we want to see the name and salary of those employee who are getting more than what Blake is earning, we will first write the column name with the Select keyword, which in our case will be “ename” and “SAL”. Then with a From keyword, a table name has to be given in which these columns are present. Here, we will choose the employee table.

Now after that, we will write a Where clause and use the Salary column as an argument, a greater than sign and then within a pair of parenthesis we will write our condition.

This condition will basically act as a sub query in SQL, so for that we will write Select alongwith Salary. Next, we will define from which table the data is to be tabulated from and define ename as BLAKE with a Where clause.

Hence the query would be:

select ename,SAL
from EMP
where SAL >
select SAL from EMP

Writing Sub query in SQL

And this is how in SQL, sub queries can be written.