
- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Create Table
- SQL - Drop Table
- SQL - Insert Query
- SQL - Select Query
- SQL - Where Clause
- SQL - AND & OR Clauses
- SQL - Update Query
- SQL - Delete Query
- SQL - Like Clause
- SQL - Top Clause
- SQL - Order By
- SQL - Group By
- SQL - Distinct Keyword
- SQL - Sorting Results
- Advanced SQL
- SQL - Constraints
- SQL - Using Joins
- SQL - Unions Clause
- SQL - NULL Values
- SQL - Alias Syntax
- SQL - Indexes
- SQL - Alter Command
- SQL - Truncate Table
- SQL - Using Views
- SQL - Having Clause
- SQL - Transactions
- SQL - Wildcards
- SQL - Date Functions
- SQL - Temporary Tables
- SQL - Clone Tables
- SQL - Sub Queries
- SQL - Using Sequences
- SQL - Handling Duplicates
- SQL - Injection
Subquery in SQL
A subquery is a query within a query i.e a nested query. It is placed inside a query and its result is used to further evaluate the outer query.
There are some rules that a subquery must follow in SQL. Some of these are −
- The subquery should be placed within parenthesis.
- The subquery can be used with different operators like <,>,<=,>=, IN,BETWEEN etc. Also operators like SELECT, INSERT, DELETE, UPDATE etc. be used.
- The ORDER BY operator cannot be used in the subquery. However, it can be there in the main query.
- A subquery cannot be written with a BETWEEN operator. But the subquery can contain the BETWEEN operator.
- The subquery that returns more than one row cannot be used with all the operators. It can only be used with operators that accept multiple values like IN.
An example of subqueries in SQL is −
<Student>
Student_Number | Student_Name | Student_Phone | Student_Marks |
---|---|---|---|
1 | Andrew | 6615927284 | 95 |
2 | Sara | 6583654865 | 65 |
3 | Harry | 4647567463 | 48 |
4 | Megan | 7554764844 | 88 |
5 | John | 3436446454 | 25 |
Select * from student where student_marks IN( select student_marks from student where student_marks>50)
This query will return details about all the students who have more than 50 marks i.e. Andrew, Sara and Megan.
Student_Number | Student_Name | Student_Phone | Student_Marks |
---|---|---|---|
1 | Andrew | 6615927284 | 95 |
2 | Sara | 6583654865 | 65 |
4 | Megan | 7554764844 | 88 |
- Related Articles
- How can we nest a subquery within another subquery?
- How to correctly enclose subquery in MySQL?
- Subquery to exclude a particular row in MySQL
- Sort data in SQL using Dynamic SQL in SAP HANA
- Difference between Open SQL, Native SQL in SAP HANA
- Difference between SQL(Structured Query Language) and T-SQL(Transact-SQL).
- Difference between SQL and PL/SQL
- Difference Between SQL and T-SQL
- SQL Query to Convert Rows to Columns in SQL Server
- Difference between Static SQL and Dynamic SQL
- Difference Between T-SQL and PL-SQL
- How do I return multiple results in a MySQL subquery with IN()?
- String Functions in SQL
- Mathematical Functions in SQL
- Conversion Functions in SQL

Advertisements