SQL Sub Query

A subquery, also known as an inner query or nested query, is a query nested inside another query. It allows you to retrieve data from one table based on the results of another query. Subqueries can be used in various parts of an SQL statement, such as the SELECT, FROM, WHERE, and HAVING clauses.

Here’s an example of how a subquery can be used in an SQL statement:

SELECT column1, column2
FROM table1
WHERE column3 IN (SELECT column4 FROM table2 WHERE condition);

In the above example, the subquery (SELECT column4 FROM table2 WHERE condition) is used to retrieve values from column4 in table2. These values are then used in the main query to filter the results of table1 based on the condition column3 IN (...).

Subqueries can also be used in other parts of an SQL statement. For example, you can use a subquery to calculate an aggregate value and then use that value in the main query:

SELECT column1, column2,
       (SELECT AVG(column3) FROM table2 WHERE condition) AS average_value
FROM table1;

In this case, the subquery (SELECT AVG(column3) FROM table2 WHERE condition) calculates the average value of column3 in table2 based on the specified condition. The calculated average value is then included as a column (average_value) in the main query’s result set.

Subqueries provide a powerful way to combine and manipulate data from multiple tables or apply complex conditions to retrieve specific information. They can be used to solve a variety of problems in SQL programming.

1. Subqueries with the Select Statement:

Subqueries can be used within the SELECT statement to retrieve data based on the results of another query. Here’s an example of using a subquery in the SELECT statement:

SELECT column1, column2, (SELECT MAX(column3) FROM table2) AS max_value
FROM table1;

In this example, the subquery (SELECT MAX(column3) FROM table2) retrieves the maximum value from column3 in table2. The subquery is placed within the SELECT statement and aliased as max_value. The result is a query that selects column1, column2, and the maximum value from table2 as max_value for each row in table1.

Subqueries within the SELECT statement can also be used to perform calculations or apply conditions on individual rows. Here’s an example:

SELECT column1, column2, (SELECT column3 * 2 FROM table2 WHERE table2.column4 = table1.column4) AS calculated_value
FROM table1;

In this case, the subquery (SELECT column3 * 2 FROM table2 WHERE table2.column4 = table1.column4) multiplies the value of column3 by 2 from table2 where column4 matches between table1 and table2. The calculated value is aliased as calculated_value in the result set.

Subqueries within the SELECT statement are useful when you need to retrieve data from another table or perform calculations on individual rows as part of your main query. They can provide flexibility and allow you to retrieve specific information based on the results of the subquery.

2. Subqueries with the INSERT Statement:

Subqueries can also be used within the INSERT statement to insert data into a table based on the results of another query. Here’s an example of using a subquery in the INSERT statement:

INSERT INTO table1 (column1, column2)
SELECT column3, column4
FROM table2
WHERE condition;

In this example, the subquery (SELECT column3, column4 FROM table2 WHERE condition) retrieves data from column3 and column4 in table2 based on a specified condition. The subquery is then used within the INSERT statement to insert the selected data into table1 with corresponding columns column1 and column2.

Subqueries within the INSERT statement can be used to populate one table with data from another table or perform transformations before inserting the data. Here’s an example:

INSERT INTO table1 (column1, column2)
SELECT column3 * 2, column4
FROM table2
WHERE condition;

In this case, the subquery (SELECT column3 * 2, column4 FROM table2 WHERE condition) multiplies the values of column3 by 2 and retrieves the value of column4 from table2 based on the specified condition. The resulting values are then inserted into table1 with corresponding columns column1 and column2.

Subqueries within the INSERT statement can be useful when you need to insert data into a table based on the results of another query or perform transformations on the data being inserted. They provide flexibility in populating tables with data from different sources or applying complex conditions before inserting the data.

3. Subqueries with the UPDATE Statement:

Subqueries can be used within the UPDATE statement to update data in a table based on the results of another query. Here’s an example of using a subquery in the UPDATE statement:

UPDATE table1
SET column1 = (SELECT column2 FROM table2 WHERE condition)
WHERE condition2;

In this example, the subquery (SELECT column2 FROM table2 WHERE condition) retrieves the value of column2 from table2 based on the specified condition. The subquery is then used within the UPDATE statement to update column1 in table1 with the retrieved value. The UPDATE statement also includes a WHERE clause (WHERE condition2) to specify additional conditions for updating the rows.

Subqueries within the UPDATE statement can be used to update data in a table based on values from another table or perform calculations before updating the data. Here’s an example:

UPDATE table1
SET column1 = column1 + (SELECT column2 FROM table2 WHERE condition)
WHERE condition2;

In this case, the subquery (SELECT column2 FROM table2 WHERE condition) retrieves the value of column2 from table2 based on the specified condition. The retrieved value is then added to the current value of column1 in table1, and the updated value is assigned back to column1. The UPDATE statement includes a WHERE clause (WHERE condition2) to specify the rows to be updated.

Subqueries within the UPDATE statement are useful when you need to update data in a table based on the results of another query or perform calculations before updating the data. They provide flexibility in modifying data in a targeted manner based on specific conditions or values from related tables.

4. Subqueries with the DELETE Statement:

Subqueries can be used within the DELETE statement to delete rows from a table based on the results of another query. Here’s an example of using a subquery in the DELETE statement:

DELETE FROM table1
WHERE column1 IN (SELECT column2 FROM table2 WHERE condition);

In this example, the subquery (SELECT column2 FROM table2 WHERE condition) retrieves values from column2 in table2 based on the specified condition. The subquery is then used within the DELETE statement to delete rows from table1 where the value of column1 matches any of the values retrieved from the subquery.

Subqueries within the DELETE statement can be used to selectively delete rows based on values from another table or specific conditions. Here’s an example:

DELETE FROM table1
WHERE column1 NOT IN (SELECT column2 FROM table2 WHERE condition);

In this case, the subquery (SELECT column2 FROM table2 WHERE condition) retrieves values from column2 in table2 based on the specified condition. The subquery is then used within the DELETE statement to delete rows from table1 where the value of column1 does not match any of the values retrieved from the subquery.

Subqueries within the DELETE statement provide flexibility in deleting rows based on specific conditions or values from related tables. They allow you to selectively remove data from a table based on the results of another query, enabling targeted deletion operations.