- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 −
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.
- How can we nest a subquery within another subquery?
- How to correctly enclose subquery in MySQL?
- Subquery to exclude a particular row in MySQL
- Difference between SQL and PL/SQL
- Difference Between SQL and T-SQL
- Difference between SQL(Structured Query Language) and T-SQL(Transact-SQL).
- Difference between Open SQL, Native SQL in SAP HANA
- Difference between Static SQL and Dynamic SQL
- Difference Between T-SQL and PL-SQL
- Sort data in SQL using Dynamic SQL in SAP HANA
- Special Operators in SQL
- String Functions in SQL
- Mathematical Functions in SQL
- Conversion Functions in SQL
- What is SQL?