'How to count the occurrence of distinct values in a column where respective values in the other column is same?

It has been a few days since I am studying SQL, and right now I am trying to solve this scenario. Let us assume we have this table.

Click to see the table1

And what I want is to get the number of distinct loc's each user has. More specifically, user ana has 2 distinct locs (2,4), ben has only 1(3).

Click to see the table2

sql


Solution 1:[1]

Standard grouping in SQL:

SELECT COUNT(DISTINCT loc) FROM t1 GROUP BY user

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 PrinsEdje80