This is my SQL table.
+-------+------+------+------+------+
| name | q1 | q2 | q3 | q4 |
+-------+------+------+------+------+
| Alex | 5 | 4 | 10 | 7 |
| Brown | 7 | 6 | 4 | 1 |
| Chris | 10 | 10 | 9 | 10 |
| Dave | 8 | 4 | 6 | 0 |
+-------+------+------+------+------+
I'd like to sum the top 2 scores from each user in my SQL query above.
For example, the top 2 scores of Alex are 10
and 7
, and so the sum is 10 + 7 = 17
I have tried the following query:
SELECT NewStudents.name, SUM(q1+q2+q3+q4) FROM NewStudents
GROUP BY NewStudents.name;
To sum all q1, q2, q3, q4
but this query sums all q1
to q4
, not the top 2 scores among q1
to q4
.
How can I construct the statement that I want to do in mySQL?