---
title: "Subqueries in Database"  
description: "A sub-query is a query embedded in another query. The subquery can be embedded in another SELECT, INSERT, UPDATE, or DELETE statement,or inside anothe"  
author: "Amit Singh"  
published: 2010-12-13  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/74/subqueries-in-database  
category: "database"  
tags: ["database"]  
reading_time: 2 minutes  

---

# Subqueries in Database

A sub-query is a query [embedded](https://www.mindstick.com/articles/332094/what-is-embedded-software-engineering) in another query. The [subquery](https://www.mindstick.com/forum/1279/using-alias-in-subquery) can be embedded in another SELECT, INSERT, UPDATE, or DELETE statement,or inside another sub query.

##### Properties:-

1. The [SELECT query](https://www.mindstick.com/articles/1858/sqlite-select-query) of a subquery is alwaysenclosed in parentheses

2. View created by using a subquery cannot beupdated.

3. The ntext, text, and image data types cannot beused in the select list of [sub queries](https://www.mindstick.com/forum/33678/sub-queries-in-sql-server)

4. If a table appears only in a subquery and not inthe outer query, then columns from that table cannot be included in the output

Example related to Subqueries

· Subquerieswith IN and NOT IN:

Example 1: SELECT city FROMtblCity WHERE ID NOTIN (SELECT CityID FROMtblState WHERE StateId ='25')

Example 2: SELECT city FROMtblCity WHERE ID IN(SELECT CityID FROM tblState WHEREStateId = '25')

· Subquerieswith [comparison](https://www.mindstick.com/articles/23182/comparison-maruti-suzuki-celerio-v-s-tata-tiago) [operators](https://www.mindstick.com/articles/716/php-operators).

Comparison operators can be used (like <, >, =,!> etc). Sub queries used with comparison operators must return a singlevalue rather than a list to avoid error. Hence the nature of the [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) mustbe knows before executing such sub queries.

Example : SELECT sName FROMtblEmployee WHERE nSalary > '5000'

##### · Subquerieswith Exists and NOT Exists.

A subquery with Exist does not really [return any data](https://answers.mindstick.com/qa/31861/can-main-method-in-java-return-any-data); itreturns [TRUE or FALSE](https://www.mindstick.com/forum/160329/how-can-you-convert-a-value-to-a-boolean-true-or-false-in-javascript).

**Example 1:** SELECT sName FROMtblEmployee WHERE NOTEXISTS(SELECT

\

* FROM tblCompany WHERE nID = 'emp0004'AND Name = 'E Vergis')\
\
**Example 2:** SELECT sName FROMtblEmployee WHERE EXISTS(SELECT *

\

FROM tblCompany WHEREnID = 'emp0004'AND Name = 'E Vergis')

---

Original Source: https://www.mindstick.com/blog/74/subqueries-in-database

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
