简体   繁体   中英

Can I do pairwise and non-pairwise subquery together in SQL

I am trying to run a query that uses one pairwise and one non-pairwise subquery. When the condition on main query is "and" (both subqueries need to match), it is returning zero rows where it should return two rows (checked graphically). The main query is, however, working when the condition is "or". Is this type of transaction prohibited in SQL?

I cannot give the actual query for it is too long thanks to the columnnames and such but this example one does represent the situation -

select ...
from <tablename>
-- tuple (pairwise) comparison
where (columnA, columnB) in (select columnA, columnB 
                             from <tablename> 
                             where <some_condition_1>)
-- single column comparison
AND columnC in (select columnC 
                from <tablename> 
                where <some_condition_2>);

When executing this query, I am getting no results (none; blank table) but when I am replacing the AND with OR, I am getting result (though not desired one). It is not a query I MUST perform but I am trying to learn if this actually works or not. Maybe one day, such a query may coe in handy for me or someone else.

I am using Oracle 11g XE. However, if solutions are different for different RDBMS (like Postgres or SQL Server), please mention them too if possible. I am also learning those.

I've tried a similar query using the HR schema in version 12c and it seems to work.

SELECT *
FROM   employees
WHERE  (manager_id, department_id) IN (SELECT manager_id,
                                                department_id
                                         FROM   employees
                                         WHERE  first_name = 'John')
AND job_id IN (SELECT job_id
               FROM   employees
               WHERE  last_name = 'Faviet')
; 

This should return 5 rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM