Sei sulla pagina 1di 2

SQL Subquery

Subquery or Inner query or Nested query is a query in a query. SQL subquery is usually added in
the WHERE Clause of the SQL statement. Most of the time, a subquery is used when you know how to
search for a value using a SELECT statement, but do not know the exact value in the database.
Subqueries are an alternate way of returning data from multiple tables.
Subqueries can be used with the following SQL statements along with the comparision operators like =,
<, >, >=, <= etc.

SELECT

INSERT

UPDATE

DELETE

SQL Subquery Example:


1) Usually, a subquery should return only one record, but sometimes it can also return multiple records
when used with operators LIKE IN, NOT IN in the where clause. The query syntax would be like,

SELECT

first_name,

last_name,

FROM

student_details

WHERE games NOT IN ('Cricket', 'Football');

Subquery output would be similar to:

first_name

last_nam
subject
e

-------------

------------- ----------

Shekar

Gowda

Badminto
n

Priya

Chandra

Chess

A subquery is a SQL query nested inside a larger query.

subject

A subquery may occur in :


- A SELECT clause
- A FROM clause
- A WHERE clause

The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or
inside another subquery.
A subquery is usually added within the WHERE Clause of another SQL SELECT statement.
You can use the comparison operators, such as >, <, or =. The comparison operator can also be
a multiple-row operator, such as IN, ANY, or ALL.
A subquery can be treated as an inner query, which is a SQL query placed as a part of another
query called as outer query.
The inner query executes first before its parent query so that the results of inner query can be
passed to the outer query.

A Subquery or Inner query or Nested query is a query within another SQL query and embedded within the WHERE
clause.
A subquery is used to return data that will be used in the main query as a condition to further restrict the data to be
retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the operators like
=, <, >, >=, <=, IN, BETWEEN etc.
There are a few rules that subqueries must follow:

Subqueries must be enclosed within parentheses.

A subquery can have only one column in the SELECT clause, unless multiple columns are in the main query
for the subquery to compare its selected columns.
An ORDER BY cannot be used in a subquery, although the main query can use an ORDER BY. The

GROUP BY can be used to perform the same function as the ORDER BY in a subquery.
Subqueries that return more than one row can only be used with multiple value operators, such as the IN

operator.

The SELECT list cannot include any references to values that evaluate to a BLOB, ARRAY, CLOB, or

NCLOB.

A subquery cannot be immediately enclosed in a set function.

The BETWEEN operator cannot be used with a subquery; however, the BETWEEN operator can be used
within the subquery

Potrebbero piacerti anche