Oracle
Oracle
By Arul Christo
What is Subquery?
• Oracle subquery is a SELECT statement that is written inside another
statement or query.
• In Short, We can say it a nested query or a query which is present within
a query
• Example:-
• select column/columns from t1 where column1 = (select column1 from
t2);
• t1: It refers to table from where we want to extract the data
• t2: It refers to the table from where the inner query gets the column2
value.
• Column1: Column on which WHERE condition is applied.
Working of Oracle Subquery
• The subquery should be enclosed within the brackets or parentheses ().
Oracle executes the subquery and retrieves the result and then uses the
same result in the outer query.
• When we use the subquery within the FROM clause of the SELECT query
Oracle refers it as inline view and when the subquery is present in the
WHERE clause of the SELECT statement, Oracle refers to it as a nested
subquery.
• One important point to know is that there is no restriction in the number
of subquery levels one can have in case of the FROM clause or inline
view.
• The limit for nested subquery is 255.
Subquery with SELECT Clause
• In this case, as the name suggests the subquery will be present in the
SELECT clause. In this case, Oracle first evaluates the subquery and then
executes the outer query. One thing we should keep in mind is that we
should use aggregate functions like COUNT, MAX, MIN in the select
clause subquery so that the subquery returns a single value.
Subquery in FROM clause
• In this case the subquery is written in the FROM keyword. This is also called as inline view
because it replaces a table in the query. If we see the below example, we can see that the inner
query first returns the age which is greater than 26 from the employee table and then the outer
query then retrieves the first 5 rows of the inner query result set.
• SELECT employee_id, vehicle_name,age
• FROM (
• SELECT e1.employee_id,e1.vehicle_name,e1.age
• FROM
• employee e1
• WHERE
• age >28
• )
• WHERE rownum <=5; As we can see the output shows the first five rows of the result set we got from the inner query.
Subquery with IN Operator
• In this case we use IN operator with the subquery, the subquery can return one or more values in its
result set due to which we use IN operator. The same result set is then used by the outer query.
• FROM employee
• WHERE vehicle_id IN (
• SELECT VEHICLE_ID
• FROM vehicle
• );
• If we look at the above example the inner query executes first, and it gets the vehicle_ids of the
vehicles which are made by TATA from the vehicle table and then the outer query uses the vehicle_ids
returned by the inner query to query data from the employee table.
Subquery with NOT IN Operator
• In this case we use NOT IN operator with the subquery, The subquery can return one or more
values in its result. The same result set is then used by the outer query.
• SELECT EMPLOYEE_ID, name
• FROM employee
• SELECT VEHICLE_ID
• FROM vehicle
• );
• If we look it works the same way as IN operator discussed earlier. The inner query executes first and it gets
the vehicle_ids of the vehicles which are NOT made by TATA from the vehicle table and then the outer query
uses the vehicle_ids returned by the inner query to query data from the employee table.
Pictorial representation of Subquery
Guidelines to follow for writing
subquery
• A subquery must be enclosed in parentheses.
• If a subquery (inner query) returns a null value to the outer query, the
outer query will not return any rows when using certain comparison
operators in a WHERE clause.
Types of Subqueries
• Single row subquery : Returns zero or one row. It is used with a comparison
operator in a WHERE, or HAVING clause.
• Multiple row subquery : Returns one or more rows. Subqueries that can return
more than one row (but only one column) to the outer statement are called
multiple-row subqueries. Multiple-row subqueries are subqueries used with an IN,
ANY, or ALL clause.
• Multiple column subqueries : Returns one or more columns.
• FROM Products
• FROM SalesOrderItems
• );
• In the WHERE clause, subqueries help select the rows from the tables listed in the FROM clause that appear in
the query results. In the HAVING clause, they help select the row groups, as specified by the main query's
GROUP BY clause, that appear in the query results.
Example of Single row subquery
• The following example of a single-row subquery calculates the average price of
the products in the Products table. The average is then passed to the WHERE
clause of the outer query. The outer query returns the ID, Name, and UnitPrice of
all products that are less expensive than the average:
• FROM Products
• WHERE UnitPrice <
• ( SELECT ID
• FROM Products
• In this example, the subquery makes a list of all values in the ID column in the
Products table, satisfying the WHERE clause search condition. The subquery then
returns a set of rows, but only a single column. The IN keyword treats each value
as a member of a set and tests whether each row in the main query is a member
of the set.
Multiple-row subqueries comparing
use of IN, ANY, and ALL
• Two tables in the SQL Anywhere sample database contain
financial results data. The FinancialCodes table is a table holding
the different codes for financial data and their meaning. To list the
revenue items from the FinancialData table, execute the following
query:
• SELECT * FROM FinancialData
• WHERE Code IN
• ( SELECT Code FROM FinancialCodes WHERE type =
'revenue' );
To be continued:-
• The ANY and ALL keywords can be used in a similar manner. For
example, the following query returns the same results as the previous
query, but uses the ANY keyword:
• SELECT * FROM FinancialData
• WHERE FinancialData.Code = ANY
• ( SELECT FinancialCodes.Code
• FROM FinancialCodes
• WHERE type = 'revenue' );
To be continued:-
• While the =ANY condition is identical to the IN condition, ANY can also be
used with inequalities such as < or > to give more flexible use of
subqueries.
• The ALL keyword is similar to the word ANY. For example, the following
query lists financial data that is not revenue:
• SELECT * FROM FinancialData
• WHERE FinancialData.Code <> ALL ( SELECT FinancialCodes.Code
• FROM FinancialCodes
• WHERE type = 'revenue' );
To be continued:-
• This is equivalent to the following command using NOT IN:
• SELECT * FROM FinancialData
• WHERE FinancialData.Code NOT IN
• ( SELECT FinancialCodes.Code
• FROM FinancialCodes
• WHERE type = 'revenue' );