SELECT requestID
FROM request
WHERE userId = (
SELECT userID
FROM department
WHERE desig = 'E'
AND dept = (
SELECT dept
FROM department
WHERE userId = it18
AND desig = 'FM'
)
);
Multiple row subquery returns one or more rows to the outer SQL statement. You may use the IN, ANY, or ALL operator in outer query to handle a subquery that returns multiple rows.
Multiple-row subqueries return more than one row of results. Operators that can be used with multiple-row subqueries include IN, ALL, ANY, and EXISTS.
Scalar Subquery Expressions If the subquery returns more than one row, then Oracle returns an error.
It would be much clearer to write this query using JOIN
:
select distinct r.requestID
from
request r
join department d
on d.userId = r.userID
and desig = 'E'
join department d2
on d2.dept = d.dept
and d2.desig = 'FM'
and d2.userId = 'it18'
Alternately, You could simply replace the =
with IN
:
SELECT requestID
FROM request
WHERE userId IN (
SELECT userID
FROM department
WHERE desig = 'E'
AND dept IN (
SELECT dept
FROM department
WHERE userId = it18
AND desig = 'FM'
)
);
They should return identical results, but try both to see if there's any difference in performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With