1. What is the difference between a single-row subquery and a multi-row subquery?
·
Single-row subquery: This
subquery returns only one row (typically one value) and is
used when comparing a single value in the outer query to a result from the
subquery. It is usually used with operators like =
, <
, >
, etc.
·
Multi-row subquery: This
subquery returns multiple rows and is used when comparing a
value in the outer query to multiple values returned by the subquery. Multi-row
subqueries often use operators like IN
, ANY
, or ALL
.
2. What are the common operators used with multi-row subqueries?
· IN: Used to check if a value matches any of the values returned by the subquery.
·
ANY: Used to compare a value
against any value returned by the subquery using a comparison operator like =
, <
, >
,
etc.
· ALL: Compares a value against all values returned by the subquery.
· EXISTS: Checks if the subquery returns any rows (though not strictly a multi-row subquery, it is often used with related subqueries).
3. What does the IN
operator do in a multi-row
subquery?
·
The IN
operator is used to compare a value in the
outer query with a list of values returned by the subquery. If the outer query
value matches any of the values returned by the subquery, the condition is
satisfied.
Example:
SELECT employee_id, name
FROM employees
WHERE department_id IN (SELECT department_id FROM departments WHERE location_id = 1400);
In this example, the IN
operator checks if
an employee's department_id
is part of the list returned by the subquery.
4. What is the difference between ANY
and ALL
in a subquery?
· ANY: Compares the outer query value to at least one value from the subquery.
· ALL: Compares the outer query value to every value from the subquery.
Example using ANY
:
SELECT product_name, price
FROM products
WHERE price > ANY (SELECT price FROM products WHERE category = 'Electronics');
Example using ALL
:
SELECT product_name, price
FROM products
WHERE price > ALL (SELECT price FROM products WHERE category = 'Electronics');
In the ANY
example, the
condition is true if the product's price is greater than any one price
in the Electronics category. In the ALL
example, the product's price must be
greater than all prices in the Electronics category.
5. When should I use a NOT IN
subquery?
·
The NOT IN
subquery is used when you want to find
values that do not match any of the values returned by the
subquery.
Example:
SELECT employee_id, name
FROM employees
WHERE department_id NOT IN (SELECT department_id FROM departments WHERE location_id = 1400);
In this example, the query returns
employees who are not in any department located at 1400
.
6. Can a multi-row subquery be correlated?
· Yes, a correlated subquery can be multi-row. A correlated subquery refers to a column from the outer query within the subquery itself. It is executed once for each row in the outer query.
Example of a correlated multi-row subquery:
SELECT employee_id, name
FROM employees e
WHERE e.salary > ALL (SELECT salary FROM employees WHERE department_id = e.department_id);
In this example, the subquery references
e.department_id
from the outer query, making it a correlated subquery.
7. What is the purpose of the EXISTS
operator in a subquery?
·
The EXISTS
operator checks whether a subquery
returns any rows. If the subquery returns at least one row, the condition is
true. It's often used in situations where the existence of a record in a
related table is the main concern, rather than the actual values.
Example:
SELECT employee_id, name
FROM employees e
WHERE EXISTS (SELECT 1 FROM departments d WHERE e.department_id = d.department_id);
In this example, the query returns all
employees who belong to a department that exists in the departments
table.
8. What happens if the subquery returns NULL values in an IN
or NOT IN
query?
·
When a subquery returns NULL
values, it can
cause unexpected results. Specifically:
o If
the subquery returns NULL
values with IN
, the condition may behave
unpredictably because NULL
is neither equal to nor unequal to any value.
o With
NOT IN
,
if the subquery includes a NULL
, it will likely return no rows, because NULL
cannot be compared directly to other values.
Example with potential issues:
SELECT employee_id
FROM employees
WHERE department_id NOT IN (SELECT department_id FROM departments WHERE location_id IS NULL);
If the subquery returns any NULL
values in the department_id
,
it may prevent the query from returning any results.
9. Can multi-row subqueries improve query performance?
· Multi-row subqueries can sometimes be inefficient, especially when dealing with large datasets. Oracle may need to evaluate the subquery multiple times, which can affect performance.
·
Alternative: In many cases,
using JOIN
operations (like INNER JOIN
or LEFT JOIN
) instead of subqueries can lead to
better performance because they are often optimized by the database engine.
10. Can I use a multi-row subquery in the FROM
clause?
·
Yes, you can use a multi-row subquery in the FROM
clause, though this is often referred to as a derived table or
inline view. It can be used to create a temporary table of
results that the outer query can then refer to.
Example:
SELECT dept_name, AVG(salary)
FROM (SELECT department_id, salary FROM employees WHERE salary > 5000) subquery
JOIN departments d ON subquery.department_id = d.department_id
GROUP BY dept_name;
In this case, the subquery in the FROM
clause returns a filtered list of employees, and the outer query calculates the
average salary by department.
No comments:
Post a Comment