Skip to content

Commit 3accd49

Browse files
committed
feat(sql-subquery): add explanation of SQL Subqueries
What - Introduced documentation on Subqueries (nested queries inside another SQL statement). - Covered key types: - Scalar subquery → returns a single value. - Row subquery → returns a single row. - Table subquery → returns multiple rows/columns. - Explained placement of subqueries: - In WHERE clause → filtering based on results of another query. - In FROM clause → using subquery as a derived table. - In SELECT clause → computing values dynamically. - Provided examples: - Find employees earning more than the average salary. - List departments with more than 5 employees. - Retrieve top 3 highest-paid employees using nested queries. Why - Subqueries allow modular, readable, and powerful SQL by embedding logic instead of writing overly complex joins. - Frequently used in analytics, reporting, and conditional filtering. - Helps break down complex queries into smaller, more manageable parts. How to use - WHERE with subquery: ```sql SELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees); • FROM with subquery: SELECT dept, COUNT(*) FROM (SELECT * FROM employees WHERE active=1) AS active_emp GROUP BY dept; • SELECT with subquery: SELECT name, (SELECT MAX(salary) FROM employees) AS max_salary FROM employees; Real-life Applications • HR systems: Find employees above company average salary. • E-commerce: Identify products priced above category average. • Finance: Retrieve customers with account balances higher than the median. • Education: List students scoring above class average. Notes • Subqueries can often be replaced with JOINs for performance. • Correlated subqueries re-evaluate per row → can be expensive; use carefully. • Aliases are mandatory when subqueries are used in FROM clause. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 8f27469 commit 3accd49

File tree

7 files changed

+11
-12
lines changed

7 files changed

+11
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Definitions:
2-
32
- Query: A query is a request to retrieve or manipulate data from a database. It can be a simple or complex
43
statement written in SQL.
54

@@ -12,20 +11,21 @@ Definitions:
1211

1312
- Nested Query: A nested query is another term for a subquery, as it is placed inside another SQL query.
1413

15-
Subqueries are commonly used for filtering, calculations, or comparisons within the main query. 🚀
14+
Subqueries are commonly used for filtering, calculations, or comparisons within the main query.
1615

1716

18-
### Usage of IN, =, and > in SQL
17+
### Usage of IN, =, and > in SQL
1918

2019
- IN → Used when checking if a value exists within a list or the result of a subquery.
2120
It is useful when expecting multiple possible values.
2221

23-
Example: Find students who live in any of the given cities.
22+
Example: Find students who live in any of the given cities.
2423

2524
- = → Used for exact matches, typically in comparisons with a single value.
2625
It is used when the subquery or condition returns only one value.
27-
Example: "Find students whose department number is exactly the same as Smith’s.
26+
27+
Example: "Find students whose department number is exactly the same as Smith’s.
2828

2929
- > → Used for comparing numerical values to check if one is greater than the other.
3030
It is used when filtering records based on conditions involving numerical magnitude.
31-
Example: Find students with marks greater than 50.
31+
Example: Find students with marks greater than 50.
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ SELECT * FROM students
66
WHERE city = (SELECT city FROM students WHERE name = 'Smith');
77

88
Explanation:
9-
109
1. The subquery (inner query) executes first:
1110

1211
SELECT city FROM students WHERE name = 'Smith';
@@ -17,16 +16,16 @@ This retrieves the city where Smith is living (e.g., 'Delhi').
1716
3. As a result, all students from the same city as 'Smith' will be displayed.
1817

1918

20-
### Meaning of the Query:
19+
Meaning of the Query:
2120

2221
The query retrieves all students who belong to the same department (deptno) as Smith.
2322

24-
### Statement:
25-
23+
Statement:
24+
2625
Find all students who are in the same department as the student named Smith.
2726

28-
### Explanation:
27+
Explanation:
2928

3029
1. The subquery first finds the deptno of Smith.
3130
2. The outer query then selects all students who have the same deptno.
32-
3. As a result, all students who share a department with Smith are displayed
31+
3. As a result, all students who share a department with Smith are displayed

0 commit comments

Comments
 (0)