Answer: Sorting and Calculations
Find a detailed explanation of using advanced sorting techniques.
We'll cover the following...
We'll cover the following...
Solution
The solution is given below:
MySQL
/* Query to retrieve top 3 students who received highest cumulative marks in Science */SELECT StudentName, TheoryMarks, PracticalMarks, (TheoryMarks + PracticalMarks) AS CumulativeMarksFROM StudentGradesWHERE Subject = 'Science'ORDER BY (COALESCE(TheoryMarks,0) + COALESCE(PracticalMarks,0)) DESCLIMIT 3;
Explanation
The explanation of the solution code is given below:
Line 2: The
SELECTquery selectsStudentName,TheoryMarks,PracticalMarks, andCumulativeMarks.Line 3: The
FROMclause specifies the table name asStudentGrades.Line 4: The
WHEREclause selects the subject in hand.Line 5: The
ORDER BYclause is applied to the addition ofTheoryMarksandPracticalMarksand sorts the data in descending order. TheCOALESCE()function ensures that if there is aNULLvalue in the column, it is replaced with0...