Mom Mom - 2 months ago 5
SQL Question

how to sort id records of data in a table of SQL server from high to low

I have a problem to sort data by ID , I tried order by ASC and DESC but non of them did not meet my requirement.
normally records registered into table from low to high , for example :

ID column1 Coulumn2
1 test1 test1
2 test2 test2
3 test3 test3


but I would like to records data to tables from high to low as follow

ID column1 Coulumn2
3 test1 test1
2 test2 test2
1 test3 test3


your help would be appreciated.
as I said I would like to see the records in table of database from high to low.

Answer

I guess you want the ID's in reversed order but the rest of the columns in the normal order:

SELECT ROW_NUMBER() OVER (ORDER BY ID DESC) AS ID,
       column1,
       Coulumn2
FROM dbo.Table1 
ORDER BY ID DESC

DEMO

If you actually want the latest 7 records but those must be in reverse order:

SELECT x.* FROM (
   SELECT TOP 7 t.*
   FROM dbo.Table1 t
   ORDER BY t.ID DESC
) x
ORDER BY x.ID DESC

DEMO