By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE person (
id INTEGER,
name VARCHAR(10),
friends VARCHAR(10)
);
INSERT INTO person
VALUES
(1, 'matt', 'jim'),
(1, 'matt', 'ray'),
(1, 'matt', 'ray'),
(2, 'tim', 'fry'),
(3, 'sally', 'jack'),
(3, 'sally', 'tim'),
(4, 'matt', 'harold');
SELECT id, name, COUNT(DISTINCT friends) AS count_friends
FROM person
GROUP BY id, name;
id | name | count_friends |
---|---|---|
1 | matt | 2 |
2 | tim | 1 |
3 | sally | 2 |
4 | matt | 1 |