Daheh
Daheh

Reputation: 137

SQL Query, get a columns if another columns equal to x

Lets say I have a table that is like this

 x      y  
10    5  
10    8  
10    12  
11    9  
11    14  
11    12  
14    12  
14    5  
14    11    

I need to return all the x group that has the same value if y = 5 So I would need a query that would return me the x group that has the value 10 or 14. Query:

select x, y from table ...

Should return me something like this :

x y  
10 5  
10 8  
10 12  
14 12  
14 5  
14 11

Upvotes: 1

Views: 1240

Answers (2)

John Woo
John Woo

Reputation: 263833

SELECT *
FROM tableName
WHERE x in
    (
        SELECT DISTINCT x
        FROM tableName
        WHERE y = 5
    )

or a join can also solve it

SELECT  a.*
FROM    tableName a
        INNER JOIN
        (
            SELECT DISTINCT x
            FROM tableName
            WHERE y = 5
        ) b ON a.x = b.x

Upvotes: 1

juergen d
juergen d

Reputation: 204884

select x, y
from your_table
where x in 
(
  select distinct x
  from your_table
  where y = 5
)

Upvotes: 3

Related Questions