'Check if a pair of records belong to multiple group IDs

I have a table that contains 2 IDs - UserID and GroupID. I need to pull a list of all UserIDs that "share" the same GroupID at least 4 times

So, based on the following data set:

CREATE TABLE IF NOT EXISTS `tableA` (
  `UserID` int(11) unsigned NOT NULL,
  `GroupID` int(11) unsigned NOT NULL
) DEFAULT CHARSET=utf8;

INSERT INTO `tableA` (`UserID`, `GroupID`) VALUES
  (1, 1),
  (2, 1),
  (3, 1),
  (4, 1),
  
  (1, 2),
  (2, 2),
  (3, 2),
  
  (1, 3),
  (2, 3),
  (3, 3),
  
  (1, 4),
  (2, 4),
  (3, 4),
  
  (1, 5),
  (3, 5);

I'm trying to generate the following result:

UserID A UserID B NumberOfOccurrences
1 2 4
2 3 4
1 3 5

I've created an SQLFiddle for it. I've tried to achieve this via JOINs and sub-queries, but I'm not entirely sure how to properly proceed with something like this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source