Sub queries are one of advance queries in oracle.
Types of advance queries:
• Sub Queries
• Hierarchical Queries
• Set Operators
Sub queries are know as the query called from another query or another subquery. It can be nested. Rather than having two or more queries to produce a result, we can solve this by combining the two queries, placing one query inside the other query called inner query or sub query.
Few points that help us for using Subqueries:
• Subqueries should always be enclosed by parentheses.
• It should be placed on the right side of the comparison condition.
• If we are performing Top-N analysis then ORDER BY clause is required in the subquery.
• We should use single-row operators with single-row subqueries and multiple-row operators with multiple-row subqueries.
Syntax:
SELECT column names
,(SELECT ….QUERY)
, (SELECT …QUERY)
FROM table
WHERE x IN
(SELECT … FROM .. WHERE y IN
( SELECT …));
In the above syntax you can see that in select we have subquery and in where clause we have nested sub queries as one sub query is calling another subquery.
A query is equivalent within another SQL statement of an expression. It can be called as an expression because it gives a result /value/set of values.
It is basically used to break down the complexity of the codes. It can also be used as SQL code tuning tool which can increase the performance and database access speed.
Types of Subqueries:
Single Row Subquery: The subquery which must return single row or a single element.
Example:
Now in the above syntax you can see that the query calling the sub query used for an exact match for REGION_ID and returns a single element. For example if we have written like the below code then we shall be getting ORA-01427 because the query calling the sub query is having more than one row which get error out.
Example:
Multiple Row Subquery: In this type of query returns one or more rows.
Here we will see the query calling the subquery having a membership with it and fetches the correct result.
Example:
.
So from the above query you can see that multiple rows are returned when a subquery is been called from the first query.
Multiple Column Subquery: In this type of subquery it selects more than one column and verify it for getting the correct result.
Syntax for multiple subqueries :
Example:
There can be two types of column comparisons like:
• Pair wise comparisons
• Non pair wise comparisons
Pair wise comparisons:
Here there query is written in such a way that the two columns are checked simultaneously within a single query.
Example:
Non pair wise comparisons:
Here there query is written in such a way that each column are checked individually with two queries.
Other types of subqueries are Regular and Correlated:
Regular Subqueries: These can be defined as having no relationship between the calling query and the subquery. Here in the below example “IN” is used for literal values only which is constructed on the subquery first as an expression list so the whole result of the expression or subquery is applied to the calling query. It is also called as normal or regular or standard subqueries.
For example:
Scalar Subquery Expressions:
Subquery that returns exactly one column value from one row.
The subquery which return a scalar values or single value within the SQL statements are know to be Scalar subqueries.It is also know as selecting a select inside the statement. It can use all types of clauses but except GROUP BY clause and we can also use conditions/expressions of scalar subqueries as a part of DECODE and CASE.
Example:
Correlated Subqueries: These types of queries have a distinct relationship between the calling query and the subquery. The relationship can be in one or more tables in the calling query using exists clause. In the below example you can see that the department ID is been matched from the subquery to the calling query by using exists clause.
The most important part is the EXIST clause is much faster then IN clause. If we ask why then “IN” always per executes the expressions in the subquery as what we saw in the above example and in “EXISTS” will actually pass the index value to one of those expression if it is present as per the below example so where it matched the indexes for the department ID between the calling query and the subquery where as “IN” do not do this but executes the entire the subquery first, “IN” is very good to use in literal values and it can also use the indexes when you file is small and if you table data is larger then it’s better to use EXISTS rather than IN.
Example for correlated subquery:
Correlated UPDATE in subquery:
To update rows in one table based on rows from another table
Syntax:
Example:
Similarly Correlated Delete can be used
Conditional Comparisons with Subqueries:
This includes all type of conditional operators used by the calling query to the subquery like [ = , != , > , < , >=, <= ] operators or using “LIKE ” condition , “[NOT ] IN” etc
Syntax:
(Subquery) [ = , != , > , < , >=, <= ] (Subquery)
(Subquery) LIKE (Subquery)
(Subquery) [NOT ] IN (Subquery)
[NOT] EXISTS (Subquery)
(Calling query) BETWEEN (Subquery) AND (Subquery) : In this the subquery must be single row subquery.
(Subquery) [ = , != , > , < , >=, <= ] ANY or SOME or ALL (Subquery): this is a multiple row subquery.
NULL values in Subquery:
The below example will shows how null values are been used in a subquery:
Syntactically the above query is correct, but one value in the subquery is having a null value and hence the whole subquery returns null as result . Hence this query results/outputs nothing.
To return the correct values we need to write the query as below: