0

I have a table orders with many orders in it,every order has an unique ID. I need to get the column with highest ID,if you know how to do that please help. thx!

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
G.D
  • 181
  • 7
  • What means you need to get the "column" with highest ID? Show your query. – Tim Schmelter Nov 18 '15 at 09:23
  • I mean to get column that has highest ID in Table Orders "select * from Orders where ID is highest" – G.D Nov 18 '15 at 09:27
  • @G.D: so you want the row with the highest ID not the column? Have you searched before you have asked this? There are plenty of duplicates like [this](http://stackoverflow.com/questions/6881424/how-can-i-select-the-row-with-the-highest-id-in-mysql) or [this](http://stackoverflow.com/questions/7604893/sql-select-row-from-table-where-id-maxid) or [this](http://stackoverflow.com/questions/7745609/sql-select-only-rows-with-max-value-on-a-column). – Tim Schmelter Nov 18 '15 at 09:29
  • 1
    Which DBMS are you using? Postgres? Oracle? –  Nov 18 '15 at 09:30

2 Answers2

3
SELECT top 1 yourColumn FROM yourTable ORDER BY yourId DESC

They key points here are TOP and ORDER BY

Depending on the RDMS you are using you may have to use different flavours of TOP but the principle is the same....

http://www.tutorialspoint.com/sql/sql-top-clause.htm

AntDC
  • 1,807
  • 14
  • 23
2

I think this is what you're looking for

select yourcolumn from yourtable where id=(select max(id) from yourtable )
Thanos Darkadakis
  • 1,669
  • 2
  • 18
  • 30