• I didn't read the previous posts. The idea is really similar, but I'm not using a temp table (I probably should). This would be a lot easier on 2005+

    SELECT s.*

    FROM #student s

    JOIN (SELECT 1 priority,'IT' class UNION ALL SELECT

    2,'Science' UNION ALL SELECT

    3,'Arts' UNION ALL SELECT

    4,'Commerce' UNION ALL SELECT

    5,'Geography' UNION ALL SELECT

    6,class

    FROM #student

    WHERE class NOT IN( 'IT','Science','Arts','Commerce','Geography')

    GROUP BY class) p ON s.class = p.class

    JOIN (select rollno, MIN( priority) minpriority

    FROM #student s

    JOIN (SELECT 1 priority,'IT' class UNION ALL SELECT

    2,'Science' UNION ALL SELECT

    3,'Arts' UNION ALL SELECT

    4,'Commerce' UNION ALL SELECT

    5,'Geography' UNION ALL SELECT

    6,class

    FROM #student WHERE class NOT IN( 'IT','Science','Arts','Commerce','Geography')) p ON s.class = p.class

    GROUP BY rollno) o ON s.rollno = o.rollno AND p.priority = o.minpriority

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2