Exp 7
Exp 7
Aim: To perform Nested and Complex queries on the given schema for a specified
system
Theory:
Subquery:
A Subquery or Inner query or a Nested query is a query within another SQL query and
embedded within the WHERE clause.
A subquery is used to return data that will be used in the main query as a condition to further
restrict the data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along
with the operators like =, <, >, >=, <=, IN, BETWEEN, etc.
Subqueries are most frequently used with the SELECT statement. The basic syntax is as
follows:
[WHERE])
A nested SELECT is a query within a query, i.e. when you have a SELECT statement within
the main SELECT. To make the concept clearer, let’s go through an example together.
Students
Teachers
1 10 3 21
2 11 4 25
3 12 1 28
SELECT AVG(number_of_students)
FROM classes
WHERE teacher_id IN (
SELECT id
FROM teachers
Conclusion: