This problem is somewhat called Relational Division
SELECT userID
FROM user_action
WHERE action IN (3,5)
GROUP BY userID
HAVING COUNT(DISTINCT action) = 2
if action
is unique for every userID
, you can directly COUNT()
the records without DISTINCT
SELECT userID
FROM user_action
WHERE action IN (3,5)
GROUP BY userID
HAVING COUNT(*) = 2
and lastly, if you want to get the whole details of the user, you need to join it with itself,
SELECT a.*
FROM user_action a
INNER JOIN
(
SELECT userID
FROM user_action
WHERE action IN (3,5)
GROUP BY userID
HAVING COUNT(*) = 2
) b ON a.userID = b.userID