1. What is a single-row subquery in Oracle?
A single-row subquery is a subquery that returns only a single row and one column. It's typically used in the main query to compare a value or return a single value for calculations.
Example:
SELECT employee_name
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
2. What happens if a single-row subquery returns more than one row?
If a single-row subquery returns more than one row, Oracle raises an error like ORA-01427: single-row subquery returns more than one row.
Example:
SELECT employee_name
FROM employees
WHERE salary = (SELECT salary FROM employees WHERE department_id = 10);
If multiple employees share the same salary in department 10, this query will fail because the subquery returns more than one row.
3. Can a single-row subquery return no rows?
Yes, a single-row subquery can return no rows. If this happens, the main query simply doesn’t return any rows because the condition is considered false.
Example:
SELECT employee_name
FROM employees
WHERE salary = (SELECT salary FROM employees WHERE department_id = 999); -- No employees in department 999
If the department 999 does not exist, the subquery returns no rows, and thus, no employee is selected.
4. How do I handle a situation where a single-row subquery might return no rows?
You can use the IS NULL condition or the COALESCE function to handle cases where the subquery might return no rows.
Example using IS NULL:
SELECT employee_name
FROM employees
WHERE salary = (SELECT salary FROM employees WHERE department_id = 999) OR salary IS NULL;
In this example, if the subquery returns no rows, the salary IS NULL condition ensures that employees are returned regardless of the subquery's outcome.
5. Can a single-row subquery be used in a SELECT clause?
Yes, a single-row subquery can be used in a SELECT clause to retrieve a single value derived from another table.
Example:
SELECT employee_name,
(SELECT department_name FROM departments WHERE department_id = employees.department_id) AS department
FROM employees;
Here, the subquery retrieves the department name for each employee based on their department_id.
6. Can I use a single-row subquery in an UPDATE statement?
Yes, a single-row subquery can be used in the SET clause of an UPDATE statement to update a value based on the result of the subquery.
Example:
UPDATE employees
SET salary = (SELECT MAX(salary) FROM employees)
WHERE employee_id = 100;
This query updates the salary of employee 100 to the highest salary found in the employees table.
7. Can a single-row subquery be used in an INSERT statement?
Yes, a single-row subquery can be used in the VALUES clause of an INSERT statement.
Example:
INSERT INTO employees (employee_name, salary)
VALUES ('John Doe', (SELECT MAX(salary) FROM employees));
Here, the subquery retrieves the highest salary from the employees table and inserts it as the salary for the new employee.
8. What comparison operators can be used with a single-row subquery?
A single-row subquery is typically used with the following comparison operators:
- =
- <> (not equal)
- <
- >
- <=
- >=
Example:
SELECT employee_name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
In this case, the query returns employees whose salary is greater than the average salary.
9. Can I use a single-row subquery with IN or NOT IN?
No, the IN and NOT IN operators are used with multi-row subqueries (i.e., subqueries that can return multiple rows). If you use these operators with a single-row subquery, it will not work because these operators are meant for comparing with multiple values, not a single value.
Example (invalid):
SELECT employee_name
FROM employees
WHERE salary IN (SELECT salary FROM employees WHERE department_id = 10);
If the subquery returns more than one salary, the IN operator will work, but if the subquery returns exactly one salary, you can simply use = instead of IN.
10. How can I limit a single-row subquery that might return multiple rows?
If you suspect that a subquery might return multiple rows but you only want the first row, you can use the ROWNUM or FETCH FIRST clause to limit the result.
Example using ROWNUM:
SELECT employee_name
FROM employees
WHERE salary = (SELECT salary FROM employees WHERE department_id = 10 AND ROWNUM = 1);
In this case, ROWNUM = 1 ensures that only the first row returned by the subquery is used.
11. What is the difference between a single-row subquery and a correlated subquery?
- A single-row subquery is independent of the outer query and returns a single value (one row and one column).
- A correlated subquery depends on the outer query and references columns from the outer query. It is executed once for each row processed by the outer query.
Example of a correlated subquery:
SELECT employee_name
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id);
Here, the subquery refers to e.department_id from the outer query, making it a correlated subquery.
12. What happens if I use a single-row subquery with NULL values?
If a single-row subquery returns a NULL value, the comparison in the main query will behave as follows:
- Any comparison with NULL (such as = NULL or <> NULL) results in unknown in SQL, which typically means no rows will be returned.
- You may need to handle NULL explicitly in your subqueries using IS NULL or COALESCE.
Example:
SELECT employee_name
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees WHERE salary IS NOT NULL);
This query avoids the issue of NULL values by explicitly filtering them out.
No comments:
Post a Comment