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

Table:user_gender

Now I try to get all MALE users.

Query:
OUTPUT:

Now I try to get all Female users.

Query:
OUTPUT:

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.

4 Replies to “use Sub-Query in SQL Query”

  1. I抦 impressed, I need to say. Actually rarely do I encounter a weblog that抯 both educative and entertaining, and let me let you know, you’ve gotten hit the nail on the head. Your thought is excellent; the issue is something that not enough persons are talking intelligently about. I am very happy that I stumbled throughout this in my search for something referring to this.

  2. Hi there, I discovered your blog by the use of Google whilst searching for a similar topic, your site came up, it seems great. I’ve bookmarked it in my google bookmarks.

  3. I do trust all of the ideas you have offered on your post. They’re very convincing and can definitely work. Nonetheless, the posts are too quick for novices. May you please lengthen them a bit from next time? Thanks for the post.

Leave a Reply

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