Sql Subqueries


A subquery is a SQL query or statement or expression within a query. Subqueries are called nested queries or inner queires. It must be enclosed with parenthesis.

Subqueries are provide data or result to the enclosing query. It can return individual values or a list of rows to outer query or main query.

It can be nested into any valid sql statement, expression, or inside another subquery.


Search Keys

  • sql subquery example
  • sql subquery join
  • sql subquery exercises
  • sql subquery alias
  • sql subquery in where clause
  • sql subquery tutorial
  • sql subquery interview questions and answers
  • sql subquery types
  • sql subquery examples pdf
  • sql subquery return multiple columns
  • sql subquery count
  • sql subquery comma separated list
  • sql subquery column
  • sql subquery case
  • sql subquery count group by
  • sql subquery correlated
  • sql subquery condition from main query
  • sql subquery condition
  • sql subquery count distinct
  • sql subquery example in select
  • sql subquery exists
  • sql subquery explained
  • sql subquery efficiency
  • sql subquery execution order
  • sql subquery exists vs in
  • sql subquery join with outer query
  • sql subquery join two tables
  • sql subquery join main query

Sql Subquery Syntax

There is no general syntax; subqueries are normal sql queries placed inside parenthesis. Subqueries can be used in many ways and at many locations inside a query or expression:

A subquery usually take one of these below formats:

  1. WHERE expression [NOT] IN (subquery)
  2. WHERE expression comparison_operator [ANY | ALL] (subquery)
  3. WHERE [NOT] EXISTS (subquery)

Sample Database Table - Employee

ID EmpName Designation Dept JoinYear Salary
1 Geetha SQL Database Oracle 2013 10820
2 Bala Murugan Cloud Database Oracle 2015 11660
3 Hanumanthan Cloud Database SQL Server 2014 20270.8
4 Ranjani Mai Sql Head PHP 2013 8510.8
5 Pandurengan Administrator MySQL 2015 19850.1
6 Varshini Kutty Relational DBMS MySQL 2012 12710.1
7 Padmavathi SQL Mining SQL Server 2013 3470.4
8 Vinoth Kumar Database Designer SQL Server 2014 14600

SQL Subquery With IN Operator

The following SQL statement will display records from employee table which is matched in the IN list.


SELECT * FROM Employee 
WHERE Dept IN (SELECT Dept FROM Employee WHERE Salary < 10000)

In the above query, "SELECT Dept FROM Employee WHERE Salary < 10000" is a subquery and it return list of values to outer query.

The sub-query returns "PHP" and "Sql Server" for which employee "Salary" is less than "10000".

The result of above query is:

ID EmpName Designation Dept JoinYear Salary
3 Hanumanthan Cloud Database SQL Server 2014 20270.8
4 Ranjani Mai Sql Head PHP 2013 8510.8
7 Padmavathi SQL Mining SQL Server 2013 3470.4
8 Vinoth Kumar Database Designer SQL Server 2014 14600

Search Keys

  • sql subquery join performance
  • sql subquery join multiple columns
  • sql subquery join table
  • sql subquery join same table
  • sql subquery join count
  • sql subquery multiple tables
  • sql subquery multiple columns
  • sql subquery multiple tables example
  • sql subquery multiple rows
  • sql subquery max date
  • sql subquery max
  • sql subquery min
  • sql subquery mysql
  • sql subquery multiple conditions
  • sql subquery more than one value
  • sql subquery in select
  • sql subquery in select clause
  • sql subquery in select return multiple values
  • sql subquery in from
  • sql subquery in join
  • sql subquery with example
  • sql subquery with join
  • sql subquery with aggregate function
  • sql subquery with group by
  • sql subquery with multiple columns
  • sql subquery with where clause
  • sql subquery where
  • sql subquery with count