Use of Subquery in MS SQL Server

Example of using Subquery in MS SQL Server

Use of subquery with FROM Clause: Use alias name otherwise it gives an error.

SELECT * FROM (select * from [Person].[CountryRegion]) k 

Use of subquery with IN Clause in where condition:

SELECT * FROM  [Person].[Address] where 
StateProvinceID in (select StateProvinceID from  [Person].[StateProvince] where 
Name = 'Alberta')

Use Subquery as a column

select Addressline1, city, (select name from  [Person].[StateProvince] where 
StateProvinceID = a.StateProvinceID) as State from [Person].[Address]  a

Use SubQuery with IN Insert operation:

INSERT INTO temp SELECT * FROM [Person].[Address]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.