Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

1 Answer
David Meador

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:

  1. The subquery should be placed within parenthesis.
  2. The subquery can be used with different operators like &lt,&gt,&lt=,&gt=, IN,BETWEEN etc. Also operators like SELECT, INSERT, DELETE, UPDATE etc. be used.
  3. The ORDER BY operator cannot be used in the subquery. However, it can be there in the main query.
  4. A subquery cannot be written with a BETWEEN operator. But the subquery can contain the BETWEEN operator.
  5. 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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.