'Row with max amount of many-to-many links

I have table_1 and table_2 with many to many relationship through t1_t2:

CREATE TABLE `table_1` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `Data` int NOT NULL,
  PRIMARY KEY (`ID`)
);

CREATE TABLE `table_2` (
  `ID` int NOT NULL AUTO_INCREMENT,
  `Data` int NOT NULL,
  PRIMARY KEY (`ID`)
);

CREATE TABLE `t1_t2` (
  `T1_ID` int NOT NULL,
  `T2_ID` int NOT NULL,
  PRIMARY KEY (`T1_ID`,`T2_ID`),
  CONSTRAINT `fk_t1_id` FOREIGN KEY (`T1_ID`) REFERENCES `table_1` (`ID`),
  CONSTRAINT `fk_t2_id` FOREIGN KEY (`T2_ID`) REFERENCES `table_2` (`ID`)
);

How can I select a row from table_1 with max amount of linked table_2 rows?



Sources

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

Source: Stack Overflow

Solution Source