mattgcon mattgcon - 3 months ago 10
SQL Question

SQL query need to get names where count(id) = 2

I have a table

programparticipants
. I am currently successfully querying the IDs where
count(name) > 1
. What I need now is to query the names that belong to those IDs where
count(name) > 1
.

Example, data result currently being returned:

ID count(name)
1 2
3 4
4 3


Example, data result needed:

ID name
1 nm1
1 nm3
3 nm2
3 nm3
3 nm4
3 nm7
4 nm5
4 nm8
4 nm9

Answer

You may use this:

SELECT 
   (SELECT name FROM participants WHERE id=p.participantid) AS name
FROM
   programparticipants AS p
WHERE
   .... (the part where you find count(name)>1)