0

i am trying to teach myself SQL and of course I would like to follow best practices.

I have created two querys to find the latest record :

select * from AppSurvey where 
DateLastUsed >= ( SELECT MAX(DateLastUsed) FROM AppSurvey)

and

select top 1 * from AppSurvey order by DateLastUsed desc

is one of these methods more efficent than the other or does it really matter

fishhead
  • 5,829
  • 7
  • 32
  • 43
  • 1
    These are actually two different queries, if DateLastUsed isn't unique. So, apples to oranges. The top one can select many surveys while the bottom one will only sample one. – Mark Canlas Jan 15 '10 at 00:16
  • The first query could return more than 1 record, so they actually don't do the same thing. In MS-SQL, the second query is more efficent – Sparky Jan 15 '10 at 00:17

2 Answers2

1

The first one could get more than one row, if your DateLastUsed column isn't unique.

Matt Hamilton
  • 200,371
  • 61
  • 386
  • 320
1

There is a similiar post on this site to what you are trying to get at.

For autoincrement fields: MAX(ID) vs TOP 1 ID ORDER BY ID DESC

The preferred answer seems to be: "In theory, they will use same plans and run almost same time"

Community
  • 1
  • 1
Jeremy Samuel
  • 790
  • 6
  • 15