Query SUBQUERY child in SQL Server

The following article will show you how to use sub-queries in SQL Server along with syntax and examples.

What is subquery in SQL (subquery)?

In SQL Server, a subquery is a query that resides in another query. You can create queries in SQL commands. These subqueries are in the WHERE, FROM or SELECT clause.

Note:

  1. Subqueries are also called INNER QUERY or INNER SELECT.
  2. The main query that contains a subquery is called OUTER QUERY or OUTER SELECT.

WHERE clause

Usually the subquery is in the WHERE clause. These subqueries are called nested queries or subqueries.

 SELECT s.sanph am_id, s.sanpham_ten 
FROM sanpham s
WHERE s.sanpham_id IN
(SELECT htk.sanpham_id
FROM hangtonkho htk
WHERE htk.soluong > 10);

This is the sub-query segment in the SELECT statement above:

 (SELECT htk.sanp ham_id 
FROM hangtonkho htk
WHERE htk.soluong > 10);

The subquery allows to find sanpham_id values ​​from the hangtonkho table with a number greater than 10. Then the subquery is used to filter the result from the main query using the IN condition.

Subqueries can be written as INNER JOIN as shown below.

 SELECT s.sanpham_id, s. sanpham_ten 
FROM sanpham s
INNER JOIN hangtonkho htk
ON s.sanpham_id = htk.sanpham_id
WHERE htk.soluong > 10;

The INNER JOIN will return more efficiently than the original subquery. It is also important to note that no queries are rewritten by JOIN.

The FROM clause

 SELECT nhacu ng.nhacung_ten, truyvancon1.tong_sl 
FROM nhacung,
(SELECT nhacung_id, SUM(donhang.soluong) AS tong_sl
FROM donhang
GROUP BY nhacung_id) truyvancon1
WHERE truyvanco n1.nhacung_id = nhacung.nhacung_id;

In this example we have created the subquery in the FROM clause as follows:

 (SELECT nhacung _id, SUM(donhang.soluong) AS tong_sl 
FROM donhang
GROUP BY nhacung _id) truyvancon1

This subquery is also associated with the alias truyvancon1. This will be the name used to refer to this subquery or any of its information fields.

SELECT clause

Usually the subquery is set in the SELECT clause to calculate by the sum functions such as SUM, COUNT, MIN or MAX but do not want to count in the main query.

 SELECT n1.ho, n1 .ten, 
(SELECT MAX(luong)
FROM nhanvien n2
WHERE n1.nhanvien_id = n2.nhanvien_id) truyvancon2
FROM nhanvien n1 ;

Below is the subquery created in the above example

 (SELECT MAX ong) 
FROM nhanvien n2
WHERE n1.nhanvie n_id = n2.nhanvien_id) truyvancon2

This subquery is also associated with the alias truyvancon2. This will be the name used to refer to this subquery or any of its information fields.

The trick of placing subqueries in the SELECT clause is used because the subquery returns a single value. That's why the sum functions such as SUM, COUNT, MIN or MAX are often used in subqueries.

Previous article: EXCEPT Operator in SQL Server

The following article: PIVOT clause in SQL Server

3.3 ★ | 9 Vote

May be interested

  • Action Query in Action 2016Action Query in Action 2016
    action queries are queries that perform actions on data, can add, change or delete records.
  • Capture execution diagrams with SQL Server 2005 ProfilerCapture execution diagrams with SQL Server 2005 Profiler
    executable diagrams are one of the best tools for diagnosing query errors in order to adjust the execution of queries on sql server. in previous versions of sql server 2005, the only option for query performance was text diagrams or graph execution diagrams for manual queries. on sql server 2005, you can capture execution plans in two ways
  • Multiple choice questions have a Query optionMultiple choice questions have a Query option
    query is a language used in databases and information systems. the following is an invitation to test your knowledge with network administrator through multiple-choice questions below.
  • Adjust performance in SQL Server: find slow queriesAdjust performance in SQL Server: find slow queries
    adjusting sql performance is a never-ending battle. this article will provide some tips for you to find slow sql queries and perform performance tuning in sql server.
  • New points in SQL Server 2017New points in SQL Server 2017
    the sql server 2017 version is primarily connected to linux, bringing the power of sql to linux. in short, you can install sql server 2017 on linux, using sql server 2017 on linux-based docker containers. sql server 2017 also allows you to choose development languages, develop it on-premise or cloud-based.
  • How to Blog Respectfully About Your Disabled ChildHow to Blog Respectfully About Your Disabled Child
    blogging can be a wonderful way for parents to find community and encourage acceptance when it comes to their child's disability. but it can also lead to over-sharing that embarrasses the child or violates their right to privacy. there are...
  • Set up Query Criteria in Access 2016Set up Query Criteria in Access 2016
    the query criteria (query criteria) are filter conditions that help you retrieve specific items from an access database, used when you want to limit results based on values ​​in a field.
  • What do you know about data queries?What do you know about data queries?
    query is a way to search and compile data from one or more tables. query execution is like asking a detailed question about databases. when creating a query in access, you are defining specific search conditions to find exactly the data you need. in the quiz below, go with the network administrator to learn about this topic.
  • How to Get Your Child to Share What Happened During the DayHow to Get Your Child to Share What Happened During the Day
    to get your child comfortable sharing anything with you, it's important to show them you want to listen, and there are lots of great ways to do that.
  • The FROM clause in SQL ServerThe FROM clause in SQL Server
    the sql server from clause (t-sql) is used to list the necessary tables in the sql server query.