1

I want to select from table where multiple columns selected from sub query using WHERE (COLUMN1, COLUMN2, COLUMN3) IN (SUB QUERY)

    SELECT * FROM ORDER_DETAIL
    WHERE (ORDER_ID, ACTION_SEQUENCE, DETAIL_SEQUENCE)
    IN (
        SELECT ORDER_ID, ACTION_SEQUENCE, DETAIL_SEQUENCE
        FROM ORDER_DETAIL
        WHERE ORDER_ID=314239027
    );

But an exception raises

cx_Oracle.DatabaseError: ORA-00920: invalid relational operator

3
  • This query runs without error in this db fiddle
    – GMB
    Sep 30, 2019 at 11:23
  • 1
    The syntax is correct: dbfiddle.uk/… there must be something else in the way you run it or the tool you are using. What tool do you use to run that?
    – user330315
    Sep 30, 2019 at 11:23
  • @a_horse_with_no_name The exception says "cx_Oracle" so I'm guessing python and the cx_Oracle module and have updated the tags to reflect this as the answer may revolve around the behaviour supported by that module rather than the allowed syntax for the database.
    – MT0
    Sep 30, 2019 at 11:55

1 Answer 1

0

As far as Oracle is concerned, that's OK. But, the query itself doesn't have much sense. It is equal to (with the help of my friends)

select *
from order_detail
where order_id = 314239027
  and action_sequence is not null     -- if those columns ...
  and detail_sequence is not null     -- ... can contain nulls

so I suggest you use it instead.

5
  • @MT0: as my last sentence says, it looks quite stupid. I don't know whether anyone writes queries this way. But, I don't know how cx_Oracle.DatabaseError works. Just as I (and GMB and a_horse_with_no_name and - I belive - you and others) said & know, query the OP posted is OK as far as Oracle is concerned. Maybe that "cx_Oracle" thing (what is it? Python?) has its own rules which find such queries invalid. As I said: I'd write it as my first query, it makes most sense (to me). My second query is (again & still) stupid, but - maybe it'll make cx_Oracle "believe" it is OK.
    – Littlefoot
    Sep 30, 2019 at 11:57
  • @MT0: ah, yes - exactly what you posted as a comment: "the answer may revolve around the behaviour supported by that module rather than the allowed syntax for the database". I was trying to "revolve the behaviour".
    – Littlefoot
    Sep 30, 2019 at 11:58
  • This querey my return rows where the order_id, action_sequence and detail_sequence values are found on different rows. Not the same as OP is trying to do.
    – jarlh
    Sep 30, 2019 at 12:04
  • Ah! Finally got it! Thanks, @jarlh & MT0. Sure, what I wrote really is stupid. Will remove it. Sorry.
    – Littlefoot
    Sep 30, 2019 at 12:07
  • where order_id = 314239027 and ACTION_SEQUENCE is not null and DETAIL_SEQUENCE is not null, if the columns can contain null values.
    – jarlh
    Sep 30, 2019 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.