What is sub-query in SQL Server? Explain its Properties?

Asked 06-Dec-2019
Updated 06-Apr-2023
Viewed 562 times

1 Answer


0

In SQL Server, a subquery is a query that is embedded within another query. A subquery can be used to retrieve data that will be used as input for the main query, allowing for more complex queries to be constructed. In this article, we will explore the properties of subqueries in SQL Serverand how they can be used to enhance query functionality.

What is subquery in SQL Server Explain its Properties

Properties of Subqueries:

Syntax: A subquery is enclosed within parentheses and is placed within the WHERE clause of the main query. The subquery can be written using any valid SELECT statement.

Purpose: The purpose of a subquery is to provide a set of values that can be used as input for the main query. The subquery can be used to filter, sort, or group data before it is used in the main query.

Execution: A subquery is executed first, and the result set is then used as input for the main query. The result set of a subquery can be a single value, a single row, or multiple rows.

Relationship: A subquery can be related to the main query in various ways. A subquery can be used to filter data based on a condition in the main query, or it can be used to retrieve data that is related to the main query.

Types: There are two types of subqueries in SQL Server: correlated and non-correlated subqueries. A correlated subquery is executed for each row of the main query, while a non-correlated subquery is executed only once.

Performance: Subqueries can impact performance, especially if the subquery returns a large result set. Careful consideration should be given to the use of subqueries to ensure that they do not impact query performance.

Examples of Subqueries:
To better understand subqueries, let's take a look at some examples:

Subquery to Retrieve Data:
SELECT FirstName, LastName
FROM Employees
WHERE EmployeeID IN (
SELECT EmployeeID
FROM Orders
WHERE OrderDate BETWEEN '2022-01-01' AND '2022-12-31'
)
In this example, the subquery retrieves all EmployeeIDs from the Orders table where the OrderDate is between '2022-01-01' and '2022-12-31'. The result set of the subquery is then used as input for the main query to retrieve the FirstNames and LastNames of the employees who have placed orders during that time period.

Correlated Subquery:
SELECT FirstName, LastName
FROM Employees e
WHERE EXISTS (
SELECT *
FROM Orders o
WHERE o.EmployeeID = e.EmployeeID
AND o.OrderDate BETWEEN '2022-01-01' AND '2022-12-31'
)
In this example, the subquery is correlated to the main query by the EmployeeID column. The subquery is executed once for each row of the main query to retrieve all orders placed by the employee in the main query during the time period specified. The EXISTS operator is used to check whether there is at least one row returned by the subquery, and the result set of the main query is the FirstName and LastName of employees who have placed orders during that time period.

In conclusion, subqueries are a powerful tool in SQL Server that allows for more complex queries to be constructed. They can be used to retrieve data, filter data, and group data before it is used in the main query. However, care should be taken to ensure that subqueries are used appropriately to avoid impacting query performance. Understanding the properties of subqueries and how they can be used will enable developers to write more efficient and effective SQL queries.