I have a single MySQL table with this data:
CREATE TABLE job_history(
id INT PRIMARY KEY AUTO_INCREMENT,
employee VARCHAR(50),
company VARCHAR(50)
);
INSERT INTO job_history(employee, company) VALUES
('John', 'IBM'),
('John', 'Walmart'),
('John', 'Uber'),
('Sharon', 'IBM'),
('Sharon', 'Uber'),
('Matt', 'Walmart'),
('Matt', 'Starbucks'),
('Carl', 'Home Depot');
SELECT * FROM job_history;
+----+----------+------------+
| id | employee | company |
+----+----------+------------+
| 1 | John | IBM |
| 2 | John | Walmart |
| 3 | John | Uber |
| 4 | Sharon | IBM |
| 5 | Sharon | Uber |
| 6 | Matt | Walmart |
| 7 | Matt | Starbucks |
| 8 | Carl | Home Depot |
+----+----------+------------+
Here's the corresponding SQL Fiddle
I want to create a SQL query to count the number of common companies between a given employee and other employees on the table.
For example, if I wanted to target employee 'John', I expect this result:
Sharon: 2
Matt: 1
Carl: 0
because Sharon has 2 common companies with John (IBM and Uber), Matt has 1 common company with John (Walmart), and Carl has 0 companies common with John.
How can I do that?