---
title: "subquery in sql with example"  
description: "In this article we are going to explain what is subquery in SQL or how to create subquery in SQL or how to use subquery in SQL server with example."  
author: "Anonymous User"  
published: 2018-07-12  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/13006/subquery-in-sql-with-example  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# subquery in sql with example

## Introduction:

In this article, we are going to explain what is subquery in SQL or how to create subquery in SQL or how to use a subquery in SQL server with the example.

## Description:

A **subquery** is a SQL query within a query. Subqueries are nested queries that provide data to the enclosing query. Subqueries can return individual values or a list of records, Subqueries must be enclosed with round brackets.

There is no general syntax; **subqueries** are regular queries placed inside round brackets. Subqueries can be used in different ways and at different locations inside a query:

Here is a subquery with the IN operator.

## Let’s see simply subquery in SQL Server

## Customer Table

![subquery in sql with example](https://www.mindstick.com/mindstickarticle/17797cd1-7acd-4654-acf2-1d21b3c0ac3e/images/0f9a87a9-5e90-4266-8479-b5c1b813237f.png)

## OrderDetails Table

![subquery in sql with example](https://www.mindstick.com/mindstickarticle/17797cd1-7acd-4654-acf2-1d21b3c0ac3e/images/1c4550aa-ac99-446b-9624-232f0fbc3e42.png)

**Subquery with the IN operator.** \

```
SELECT ID,NAME,ADDRESS1 FROM CUSTOMER
     WHERE ID IN (SELECT ID FROM orderDetails WHERE AMOUNT=10000)
```

In above example, we have a customer table and orderdetails table, we have created a subquery which is return customer details, which customer order amount equal 10000.

## OutPut:

![subquery in sql with example](https://www.mindstick.com/mindstickarticle/17797cd1-7acd-4654-acf2-1d21b3c0ac3e/images/aac63343-831c-4932-ba1c-11e13d27d7e7.png)**\**

i hope it will help you after reading it.

you might also like to read next topic [create a simple cursor in](https://www.mindstick.com/Articles/13003/create-a-simple-cursor-in-sql-server) [sql](https://www.mindstick.com/Articles/13003/create-a-simple-cursor-in-sql-server) [server](https://www.mindstick.com/Articles/13003/create-a-simple-cursor-in-sql-server), [how to import excel data in](https://www.mindstick.com/Articles/12937/how-to-import-excel-data-in-sql-server-2014) [sql](https://www.mindstick.com/Articles/12937/how-to-import-excel-data-in-sql-server-2014) [server database](https://www.mindstick.com/Articles/12937/how-to-import-excel-data-in-sql-server-2014), [Types Of Join in](https://www.mindstick.com/Articles/12231/types-of-join-in-sql-server) [Sql](https://www.mindstick.com/Articles/12231/types-of-join-in-sql-server) [Server](https://www.mindstick.com/Articles/12231/types-of-join-in-sql-server), [Union Example in SQL Server](https://www.mindstick.com/Articles/1510/union-example-in-sql-server), [Trigger in SQL](https://www.mindstick.com/Articles/1440/trigger-in-sql)

\

---

Original Source: https://www.mindstick.com/articles/13006/subquery-in-sql-with-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
