I’m new to SQL and am trying to learn it. A problem that occurred to me was how to find the maximum number of specific occurrences for a person in a table. I’m not sure how the query would compare each ID with another ID and keep track of the count. Poor explanation, but here is a dataset that will hopefully make it more clear.
players:
+--------+--------+
| pid | name |
+--------+--------+
| 1 | Tom |
| 2 | Sam |
| 3 | Dan |
+--------+--------+
scores:
+--------+--------+--------+
| sid | pid | result |
+--------+--------+--------+
| 1 | 1 | miss |
| 2 | 1 | hit |
| 3 | 3 | hit |
| 4 | 2 | miss |
| 5 | 3 | hit |
| 6 | 3 | hit |
+--------+--------+--------+
The expected answer would be pid = 3, name = Dan because he had the most hits out of all the other players.
http://sqlfiddle.com/#!4/03ba0/22