Skip to content
Advertisement

how to select set of records is ID is present in one of them

Here is the table where ORGID/USERID makes unique combination:

I need to select all records (organizations and users) wherever USERID 1 is present. So USERID=1 is present in ORGID 1 and 2 so then select all users for these organizations including user 1 itself, i.e.

Is it possible to do it with one SQL query rather than SELECT *.. WHERE USERID IN (SELECT…

Advertisement

Answer

Yes, you can do it with a single select statement – no in or exists conditions, no analytic or aggregate functions in a subquery, etc. Why you want to do it that way is not clear; in any case, it is possible that the solution below is also more efficient than the alternatives. You will have to test on your real-life data to see if that is true.

The solution below has two potential disadvantages: it only works in Oracle (it uses a proprietary extension of SQL, the match_recognize clause); and it only works in Oracle 12.1 or higher.

Output:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement