DBMS SQL Sub Queries
DBMS SQL Sub Queries
A Subquery is a query within another SQL query and embedded within the WHERE clause.
Important Rule:
A subquery can be placed in a number of SQL clauses like WHERE clause, FROM clause, HAVING clause.
You can use Subquery with SELECT, UPDATE, INSERT, DELETE statements along with the operators like =, <, >,
>=, <=, IN, BETWEEN, etc.
A subquery is a query within another query. The outer query is known as the main query, and the inner query is
known as a subquery.
In the Subquery, ORDER BY command cannot be used. But GROUP BY command can be used to perform the
same function as ORDER BY command.
Syntax
SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );
Example
4 Alina 29 UK 6500.00
SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT
ID FROM EMPLOYEE
WHERE SALARY > 4500);
4 Alina 29 UK 6500.00
In the subquery, the selected data can be modified with any of the
character, date functions.
Syntax:
FROM table_name
WHERE VALUE
OPERATOR
Example
Now use the following syntax to copy the complete EMPLOYEE table into
the EMPLOYEE_BKP table.
Syntax
UPDATE table
Example
UPDATE EMPLOYEE
SET SALARY = SALARY * 0.25
This would impact three rows, and finally, the EMPLOYEE table would have the
following records.
1 John 20 US 2000.00
4 Alina 29 UK 1625.00
Syntax
https://www.javatpoint.com/dbms-sql-sub-queries 3/
Example
This would impact three rows, and finally, the EMPLOYEE table would have the
following records.
1 John 20 US 2000.00
https://www.javatpoint.com/dbms-sql-sub-queries 4/