You are on page 1of 15

SQL Sub Query

SUB Query
“A query within a query embedded in the WHERE clause”

Identifies the exact value in the database

Also called as Nested query or Inner query

An alternative way to return data from multiple tables

Sub queries like SELECT, INSERT, UPDATE and DELETE can


be
used in SQL statements along with the comparison
operators
SUB Query

 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
SUB Query

 Single row subquery : Returns zero or one row.


 Multiple row subquery : Returns one or more rows.
 Multiple column subqueries : Returns one or more columns.
 Correlated subqueries : Reference one or more columns in the outer SQL
statement. The subquery is known as a correlated subquery because the
subquery is related to the outer SQL statement.
 Nested subqueries : Subqueries are placed within another subquery.
SUB Query with SELECT STATEMENT
SELECT column_name [,
column_name ] FROM table1 [,
table2]
WHERE column_name OPERATOR
(SELECT column_name [,
column_name ] FROM table1 [,
table2]
[WHERE])
SUB Query with INSERT
STATEMENT
INSERT INTO table_name [(column1 [, column2])
] SELECT [* | column1 [, column2]
FROM table1 [, table2]
[WHERE VALUE OPERATOR]
SUB Query with UPDATE STATEMENT
UPDATE table
SET column_name =
new_value [WHERE
OPERATOR [VALUE]]
(SELECT
COLUMN_NAM
[WHERE)]
E SUB Query with DELETE STATEMENT
FROM DELETE FROM TABLE_NAME
TABLE [WHERE OPERATOR
_NAM [VALUE]
E)
(SELECT
COLUMN_N
AME
FROM
TABLE_NAME) [WHERE)]
SETOperations
SET Operations in SQL
SET Operations in SQL
1. UNION Operation
 UNION is used to combine the results of two or more SELECT statements
 The SQL Union operation is used to combine the result of two or more SQL
SELECT queries
 However it will eliminate duplicate rows from its resultset.
 In case of union, number of columns and datatype must be same in both the
tables, on which UNION operation is being applied.
 Syntax:

SELECT column_name FROM table1  
UNION  
SELECT column_name FROM table2
SET Operations in SQL
2. Union All

 This operation is similar to Union. But it also shows the duplicate


rows.
 It returns the set without removing duplication and sorting the data
 Syntax:

SELECT column_name FROM table1  
UNION  ALL
SELECT column_name FROM table2 ;
SET Operations in SQL
3. Intersect
 It is used to combine two SELECT statements. The Intersect operation returns the
common rows from both the SELECT statements
 In the Intersect operation, the number of datatype and columns must be the same
 It has no duplicates and it arranges the data in ascending order by default.
 Syntax:

SELECT column_name FROM table1  
INTERSECT
SELECT column_name FROM table2 ;
SET Operations in SQL
4. Minus
 It combines the result of two SELECT statements. Minus operator is used to
display the rows which are present in the first query but absent in the second
query
 It has no duplicates and data arranged in ascending order by default.
 Syntax:

SELECT column_name FROM table1  
MINUS
SELECT column_name FROM table2 ;
Summary
References

 https://www.w3resource.com/sql/subqueries/understanding-sql-subqueries.php
 https://www.dofactory.com/sql/subquery

You might also like