Multiple table rows in one row
Try this query with 2 self joins:

select 
a.class
,a.total_marks as 1st_Position_Marks
,b.total_marks as 2nd_Position_Marks
,c.total_marks as 3rd_Position_Marks
,abs (a.total_marks - b.total_marks) as Difference

from MyTable a
join MyTable b on a.class = b.class and a.position + 1 = b.position
join MyTable c on a.class = c.class and a.position + 2 = c.position

where 
a.position= 1 and b.position=2 and c.position =3 and
c.total_marks &gt; abs(a.total_marks - b.total_marks)


UPDATE. Some explaination.

Basically you are doing calculations on columns, so you need to put data in two columns alongside each other. To do than you need a join, not union. You need to join two rows, that would contain data for your calcultaion. As we need difference between position 1 and 2 we need to put this data on the same row. A.position +1 = B.position does exactly that. as you do not need other permutations of position, I added the filter.