I am in a very complicated problem. Let me explain you first what I am doing right now:

I have a table name `feedback` in which I am storing grades against course id. The table looks like this:

```
+-------+-------+-------+-------+-----------+--------------
| id    | cid   | grade |g_point| workload  | easiness
+-------+-------+-------+-------+-----------+--------------
| 1     |  10   |  A+   |   1   |      5    |   4
| 2     |  10   |  A+   |   1   |      2    |   4
| 3     |  10   |  B    |   3   |      3    |   3
| 4     |  11   |  B+   |   2   |      2    |   3
| 5     |  11   |  A+   |   1   |      5    |   4
| 6     |  12   |  B    |   3   |      3    |   3
| 7     |  11   |  B+   |   2   |      7    |   8
| 8     |  11   |  A+   |   1   |      1    |   2

```

`g_point` has just specific values for the grades, thus I can use these values to show the user courses sorted by grades. 

Okay, now first my task is to print out the `grade` of each course. The `grade` can be calculated by the maximum occurrence against each course. For example from this table we can see the result of `cid = 10` will be `A+`, because it is present two times there. This is simple. I have already implemented this query which I will write here in the end.

The main problem is when we talk about the course `cid = 11` which has two different grades. Now in that situation client asks me to take the average of workload and easiness of both these courses and whichever course has the greater average should be shown. The average would be computed like this:

```
  all workload values of the grade against course
+ all easiness values of the grade against course
/ 2 

```

From this example `cid = 11` has four entries,have equal number of grades against a course

**`B+` grade average**

```
avgworkload(2 + 7)/2=x 
avgeasiness(3 + 8)/2 = y

```

answer x+y/2 = 10

**`A+` grade average**

```
 avgworkload(5 + 1)/2=x 
avgeasiness(4 + 2)/2 = y

  answer x+y/2 = 3

```

so the grade should be `B+`.

This is the query which I am running to get the max occurrence grade 

```
SELECT
    f3.coursecodeID cid,
    f3.grade_point p,
    f3.grade g
FROM (
    SELECT
        coursecodeID,
        MAX(mode_qty) mode_qty
    FROM (
        SELECT
            coursecodeID,
            COUNT(grade_point) mode_qty
        FROM feedback
        GROUP BY
            coursecodeID, grade_point
    ) f1
    GROUP BY coursecodeID
) f2
INNER JOIN (
    SELECT
        coursecodeID,
        grade_point,
        grade,
        COUNT(grade_point) mode_qty
    FROM feedback
    GROUP BY
        coursecodeID, grade_point
) f3
ON
    f2.coursecodeID = f3.coursecodeID AND
    f2.mode_qty = f3.mode_qty
GROUP BY f3.coursecodeID
ORDER BY f3.grade_point

```