Display entire row in last

Viewed 26

I don't know this question already asked or not but i have not found anywhere. Please I am not on advanced level SQL so apologies if this is too easy!!

We have a table for student in where the students enrolled in different courses. What i want is to make a query in such a way that the entire row of student name and selected course like Mathematics and Gym show in last. Table is below in the picture.

enter image description here

I have tried this but it only shows the course in descending order. Any help or an idea would be appreciated!

SELECT *
FROM student_birthday
WHERE student_fullname = 'Alpha'
ORDER BY student_course DESC, student_fullname;

enter image description here

1 Answers

You can use booleans for a single value:

SELECT sb.*
FROM student_birthday sb
WHERE sb.student_fullname = 'Alpha'
ORDER BY (student_course = 'Gym');

If you want more control over the ordering, then you might use a CASE expression:

ORDER BY (case when student_course = 'Mathematics' then 1
               when student_course = 'Gym' then 3
               else 2
          end),
         student_course

Oh, that puts Mathematics first. Personal bias.

Related