In this blog, I will show step by step how to use SQL Sub-Query in SQL Query. explain how its works and why we need to do this. Let’s Go.

What is Sub-Query?

A Sub-Query is a query inside a query like SELECT, INSERT, UPDATE, or DELETE statement, or inside another subquery.

A subquery can be used anywhere an expression is allowed. In this example, a subquery is used as a column expression named MaxUnitPrice in a SELECT statement.

Subquery fundamentals

A subquery is also called an inner query or inner select, while the statement containing a subquery is also called an outer query or outer select.

Many Transact-SQL statements that include subqueries can be alternatively formulated as joins. Other questions can be posed only with subqueries. In Transact-SQL, there is usually no performance difference between a statement that includes a subquery and a semantically equivalent version that does not. For architectural information on how SQL Server processes queries, see SQL statement processing. However, in some cases where existence must be checked, a join yields better performance. Otherwise, the nested query must be processed for each result of the outer query to ensure the elimination of duplicates. In such cases, a join approach would yield better result

Execute Query using Sub-Query

Let us proceed to the main topic by executing the SQL query using SubQuery. I am assuming that you are connected and open your SQL Server Database and currently you are facing SQL Query Tab.

given example;

Table: users


Now I try to get all MALE users.


Now I try to get all Female users.


note: This is only an example and you can use sub-query in your own style. and hope you learn something this article. please do subscribe for more updates.

One Reply to “use Sub-Query in SQL Query”

Leave a Reply

Your email address will not be published. Required fields are marked *