Skip to content
Advertisement

Does SQL’s EXISTS function select duplicates? (Oracle SQL)

t1 has all the credits of each person in a given ORACLE database. Each person is identified by ID. t2 contains the people I need to select from t1.

To do this, I tried the following:

select
    ID,
    capital,
    balance
from
    t1
where
    exists (
        select ID
        from t2
        where t1.ID = t2.ID);

My problem is that t2 has duplicate IDs. So for example, if ID = 2 has 3 credits and she is repeated 4 times in t2, will the above query select each of her credits 4 times? That is, will I have 3 x 4 credits for ID = 2?

Typically, I would count the duplicate credits from the result, but I currently do not have a unique identifier for the selected credits. This is the reason I am asking this here rather than trying a grouped by count from the query’s result.

Advertisement

Answer

No. The query that you have written only returns rows from t1. You cannot multiply rows using a where clause (well, almost never and not in Oracle).

One reason for using exists or in is so you don’t have to worry about duplicates, the way you would need to worry with a join.

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