5.2 KiB
Sub Queries
A subquery is a SQL query nested within a larger query. This powerful technique allows you to build complex data operations, treating the output of the inner query as input for the outer one.
Subqueries are crucial for filtering, aggregation, and data manipulation tasks that simple queries cannot address effectively.
Subqueries can appear in several key clauses:
- SELECT clause
- FROM clause
- WHERE clause
- HAVING clause
The inner query executes first, its results informing the execution of the parent query. This execution order is fundamental to understanding subquery behavior.
Employ subqueries within SELECT, INSERT, UPDATE, or DELETE statements to:
- Compare an expression against the result of another query.
- Ascertain if an expression is present within a query's results.
- Verify if a query returns any rows.
Subqueries with SELECT
Subqueries frequently appear in the WHERE clause of SELECT statements to filter data based on criteria derived from another query's result set.
Consider the following CUSTOMERS table:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 35 | Ahmedabad | 2000.0 0 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Example: Retrieve customers with a salary greater than 4500.
SELECT *
FROM CUSTOMERS
WHERE ID IN (
SELECT ID
FROM CUSTOMERS
WHERE SALARY > 4500
);
This query first identifies the IDs of customers earning more than 4500. The outer query then selects all information for customers whose ID is in that list.
Result:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500 .00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Subqueries with UPDATE
Subqueries can update single or multiple columns based on criteria determined by the subquery's result.
Assuming a CUSTOMERS_BKP table exists as a backup. The following example updates the SALARY in the CUSTOMERS table by multiplying it by 0 .25 for customers whose AGE is 27 or greater, based on the CUSTOMERS_BKP table.
UPDATE CUSTOMERS
SET SALARY = SALARY * 0.25
WHERE AGE IN (
SELECT AGE
FROM CUSTOMERS_BKP
WHERE AGE >= 27
);
This statement identifies ages from CUSTOMERS_BKP that are 27 or greater. The outer UPDATE then applies the salary adjustment to rows in CUSTOMERS with matching ages.
Initial CUSTOMERS table:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 15 00.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000. 00 |
After executing the UPDATE, the CUSTOMERS table will contain:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 35 | Ahmedabad | 500.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 65 00.00 |
| 5 | Hardik | 27 | Bhopal | 2125.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Note: Ramesh (Age 35) and Hardik (Age 27) are affected as their age is >= 27.
Subqueries with DELETE
Subqueries enable deleting rows based on conditions derived from the subquery's output .
Assuming a CUSTOMERS_BKP table is available. The following example deletes records from the CUSTOMERS table for customers whose AGE is 27 or greater, as found in the CUSTOMERS_BKP table.
DELETE FROM CUSTOMERS
WHERE AGE IN (
SELECT AGE
FROM CUSTOMERS_BKP
WHERE AGE >= 27
);
This query identifies ages from CUSTOMERS_BKP that meet the condition. The outer DELETE then removes rows from CUSTOMERS with those corresponding ages.
Initial CUSTOMERS table:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 1 | Ramesh | 35 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2 000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
After executing the DELETE, the CUSTOMERS table will contain:
| ID | NAME | AGE | ADDRESS | SALARY |
|---|---|---|---|---|
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | Kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.0 0 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
Rows with Age 35 (Ramesh) and 27 (Hardik) are removed.