Subquery with SELECT, INSERT, UPDATE & DELETE
Learn how subqueries can be used with SELECT, INSERT, UPDATE and DELETE statements.
Introduction
A subquery can be used not only with SELECT statements, but also with INSERT, UPDATE and DELETE statements.
The subquery provides the required data or condition that is used by the main SQL statement.
1. SELECT with Subquery
A subquery can be used inside a SELECT statement to filter records based on the result of another query.
SELECT EmployeeName, Salary
FROM Employees
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employees
);
The inner query calculates the average salary and the outer query returns employees whose salary is greater than the average.
2. INSERT with Subquery
A subquery can be used with INSERT to insert the result of a SELECT query into another table.
Suppose we have an archive table:
CREATE TABLE EmployeeArchive
(
EmployeeId INT,
EmployeeName VARCHAR(100),
Salary DECIMAL(10,2)
);
We can insert selected employees using a subquery:
INSERT INTO EmployeeArchive
(EmployeeId, EmployeeName, Salary)
SELECT EmployeeId, EmployeeName, Salary
FROM Employees
WHERE DepartmentId IN
(
SELECT DepartmentId
FROM Departments
WHERE Location = 'Pune'
);
The inner query finds departments located in Pune. The SELECT query then gets employees belonging to those departments and inserts them into EmployeeArchive.
3. UPDATE with Subquery
A subquery can be used in an UPDATE statement to determine which records should be updated.
Example: Increase the salary of employees who belong to the IT department.
UPDATE Employees
SET Salary = Salary + 5000
WHERE DepartmentId =
(
SELECT DepartmentId
FROM Departments
WHERE DepartmentName = 'IT'
);
The subquery finds the department ID of the IT department. The UPDATE statement then increases the salary of employees belonging to that department.
4. DELETE with Subquery
A subquery can also be used with DELETE to identify the records that should be removed.
Example: Delete employees who belong to a department located in Mumbai.
DELETE FROM Employees
WHERE DepartmentId IN
(
SELECT DepartmentId
FROM Departments
WHERE Location = 'Mumbai'
);
The inner query finds the departments located in Mumbai. The DELETE statement removes employees belonging to those departments.
Subquery with SQL Statements
| Statement | Purpose | Example Use |
|---|---|---|
| SELECT | Retrieve records based on another query. | Find employees above average salary. |
| INSERT | Insert data obtained from another query. | Copy selected employees to an archive. |
| UPDATE | Update records selected using a subquery. | Update employees of a particular department. |
| DELETE | Delete records identified by a subquery. | Delete employees from a particular location. |
Important Points
- A subquery can be used with SELECT, INSERT, UPDATE and DELETE.
- Always verify the result of the subquery before performing UPDATE or DELETE operations.
- INSERT ... SELECT is useful for copying selected records between tables.
- UPDATE with a subquery can be used to modify records based on related data.
- DELETE with a subquery can remove records based on conditions from another table.
- Be especially careful with UPDATE and DELETE because they can modify or remove multiple records.
Summary
Subqueries can be used with SELECT, INSERT, UPDATE and DELETE statements. They allow one query to provide data or conditions for another SQL operation and are useful when working with related data across tables.