Skip to content
Advertisement

How to get the SQL result in reverse from the columns in SQL

I have been asked a question in an interview.

Sample table named test:

A|| B
---------- -
1 | a
2 | b
3 | c
4 | d

Desired output:

A || B
------- -
1 | d
2 | c
3 | b
4 | a

How to to get the desired output?

Advertisement

Answer

You just need to use order by on column B ASC|DESC

SELECT ROW_NUMBER() over (order by Testcol DESC) as A, B
FROM Table
ORDER BY B DESC;
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement