I have an employee table that looks like this:
| id | name | q1 | q2 | q3 | q4 |
+----+------+----+----+----+----+
| 1 | John | 20 | 30 | 10 | 4 |
| 2 | Ram | 07 | 10 | 03 | 4 |
| 3 | John | 05 | 03 | 15 | 40 |
| 4 | Sree | 12 | 05 | 20 | 25 |
I needed to get the minimum value and maximum value of questions where the id is equal to 4. In this case, I needed 5 and 25 returned. I acheived that using the following query:
SELECT id, name,
LEAST(q1, q2, q3, q4) AS minValue,
GREATEST(q1, q2, q3, q4) AS maxValue
FROM employee
WHERE id = 4;
But what this doesn't return is the question id. How can I adjust my query to show that q2 is the minimum and q4 is the maximum? I know I could write a big case statement, but I also feel like it could be accomplished using a join but I can't figure it out.
Note: This is for a postgresql database, but I tagged MySQL as well because I know it also supports the LEAST
and GREATEST
functions. If the solution is very different for both, then I will remove this note and make a separate question.
EDIT
I have an SQL Fiddle already.