Nested_Query_SQL_Explanation
Nested_Query_SQL_Explanation
A nested query (or subquery) is a query inside another query. The inner query runs first and its result is used
[Bullet] Syntax:
SELECT column_name(s)
FROM table_name
SELECT column_name
FROM another_table
WHERE condition
);
Table: Students
| ID | Name | Marks |
|----|--------|-------|
| 1 | Aditya | 75 |
| 2 | Rohan | 55 |
| 3 | Sneha | 82 |
| 4 | Priya | 60 |
| 5 | Kavita | 90 |
Query:
FROM Students
SELECT AVG(Marks)
FROM Students
);
Nested Queries in SQL - Detailed Explanation
[Explanation] Explanation:
1. Inner Query:
2. Outer Query:
[Check] Output:
| Name | Marks |
|--------|-------|
| Aditya | 75 |
| Sneha | 82 |
| Kavita | 90 |
FROM ClassA
WHERE Marks IN (
SELECT Marks
FROM ClassB
);
[Summary] Summary:
- Nested queries are useful for filtering or calculations based on another query.