0% found this document useful (0 votes)
5 views

DBMS SQL Sub Queries - Javatpoint

Uploaded by

ISAAC SICHALWE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

DBMS SQL Sub Queries - Javatpoint

Uploaded by

ISAAC SICHALWE
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Home DBMS SQL PL/SQL SQLite MongoDB Cassandra MySQL Oracle CouchDB Neo4j DB2 C Java Projects Interview Q

⇧ SCROLL TO TOP
SQL Sub Query
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.

Subqueries are on the right side of the comparison operator.

A subquery is enclosed in parentheses.

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.

1. Subqueries with the Select Statement


ADVERTISEMENT

ADVERTISEMENT

SQL subqueries are most frequently used with the Select statement.

Syntax

SELECT column_name
FROM table_name
WHERE column_name expression operator
( SELECT column_name from table_name WHERE ... );

Example

Trade responsibly.
62% of retail CFD accounts lose money

Consider the EMPLOYEE table have the following records:

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

6 Harry 42 China 4500.00

7 Jackson 25 Mizoram 10000.00

The subquery with a SELECT statement will be:


SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
WHERE SALARY > 4500);

This would produce the following result:

ID NAME AGE ADDRESS SALARY

4 Alina 29 UK 6500.00

5 Kathrin 34 Bangalore 8500.00

7 Jackson 25 Mizoram 10000.00

2. Subqueries with the INSERT Statement

SQL subquery can also be used with the Insert statement. In the insert statement, data returned from the subquery is
used to insert into another table.

In the subquery, the selected data can be modified with any of the character, date functions.

Syntax:

INSERT INTO table_name (column1, column2, column3....)


SELECT *
FROM table_name
WHERE VALUE OPERATOR

Example

Consider a table EMPLOYEE_BKP with similar as EMPLOYEE.

Now use the following syntax to copy the complete EMPLOYEE table into the EMPLOYEE_BKP table.

INSERT INTO EMPLOYEE_BKP


SELECT * FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE);

3. Subqueries with the UPDATE Statement

The subquery of SQL can be used in conjunction with the Update statement. When a subquery is used with the Update
statement, then either single or multiple columns in a table can be updated.

Syntax
UPDATE table
SET column_name = new_value
WHERE VALUE OPERATOR
(SELECT COLUMN_NAME
FROM TABLE_NAME
WHERE condition);

Example

Let's assume we have an EMPLOYEE_BKP table available which is backup of EMPLOYEE table. The given example updates
the SALARY by .25 times in the EMPLOYEE table for all employee whose AGE is greater than or equal to 29.

UPDATE EMPLOYEE
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 29);

This would impact three rows, and finally, the EMPLOYEE table would have the following records.

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

4 Alina 29 UK 1625.00

5 Kathrin 34 Bangalore 2125.00

6 Harry 42 China 1125.00

7 Jackson 25 Mizoram 10000.00

4. Subqueries with the DELETE Statement

The subquery of SQL can be used in conjunction with the Delete statement just like any other statements mentioned above.

Syntax

DELETE FROM TABLE_NAME


WHERE VALUE OPERATOR
(SELECT COLUMN_NAME
FROM TABLE_NAME
WHERE condition);

Example
Let's assume we have an EMPLOYEE_BKP table available which is backup of EMPLOYEE table. The given example deletes
the records from the EMPLOYEE table for all EMPLOYEE whose AGE is greater than or equal to 29.

DELETE FROM EMPLOYEE


WHERE AGE IN (SELECT AGE FROM EMPLOYEE_BKP
WHERE AGE >= 29 );

This would impact three rows, and finally, the EMPLOYEE table would have the following records.

ID NAME AGE ADDRESS SALARY

1 John 20 US 2000.00

2 Stephan 26 Dubai 1500.00

3 David 27 Bangkok 2000.00

7 Jackson 25 Mizoram 10000.00

← Prev Next →

Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

Send your Feedback to [email protected]

Help Others, Please Share

Learn Latest Tutorials


Splunk SPSS tutorial Swagger T-SQL Tumblr
Preparation
tutorial tutorial tutorial tutorial
SPSS
Splunk Swagger Transact-SQL Tumblr
Aptitude Logical Verbal Interview Company
Aptitude
Reasoning Ability Questions Interview
Reasoning Verbal Ability Interview
Questions
React tutorial Regex R
Questions RxJS tutorial
Company
tutorial Reinforcement Programming Questions
ReactJS RxJS
learning tutorial
Regex
tutorial
R Programming
Reinforcement
Learning
Trending Technologies
React Native Python Python Python Keras
tutorial Design Patterns Pillow tutorial Turtle tutorial tutorial
Artificial
React Native AWS Tutorial
Python Design Selenium
Python Pillow Cloud
Python Turtle Hadoop
Keras
Intelligence Patterns
AWS
tutorial Computing tutorial
Artificial Selenium Cloud Computing Hadoop
Intelligence

ReactJS Data Science Angular 7 Blockchain Git Tutorial


Tutorial Tutorial Tutorial Tutorial Git
ReactJS Data Science Angular 7 Blockchain

Machine DevOps
Learning Tutorial
Tutorial DevOps
Machine
Learning

B.Tech / MCA

DBMS Data DAA tutorial Operating Computer


tutorial Structures DAA
System Network
DBMS
tutorial Operating
tutorial
Data Structures System Computer
Network

Compiler Computer Discrete Ethical Computer


Design tutorial Organization Mathematics Hacking Graphics
Compiler Design
and Tutorial Ethical Hacking
Tutorial
Architecture Discrete Computer
Computer Mathematics Graphics
Organization
Software html tutorial Cyber Automata C Language
Engineering Web Technology
Security Tutorial tutorial
Software
tutorial Automata C Programming
Engineering Cyber Security

C++ tutorial Java tutorial .Net Python List of


C++ Java
Framework tutorial Programs
tutorial Python Programs
.Net

Control Data Mining Data


Systems Tutorial Warehouse
tutorial Data Mining
Tutorial
Control System Data Warehouse

You might also like