SQL max table row

Which one is better in SQL Server 2008 or it does not matter as the database is optimized.

SELECT * FROM table
WHERE datecolumn = (SELECT max(datecolumn) FROM table);


SELECT TOP 1 * FROM table ORDER BY datecolumn DESC;
+5
source share
3 answers

Considering the actual SQL execution plan for these queries, it gives exactly the same execution plan and the same execution cost. However, this may vary depending on the index setting and statistics collected, so the best thing to do is check.

One thing to be aware of is that two queries may not return the same result. The second SQL will always return a single record, where the first record can return multiple rows, if datecolumnnot unique.

+9
source

.

0

SELECT * FROM table WHERE datecolumn = (SELECT max (datecolumn) FROM table);

SELECT TOP 1 * FROM table ORDER BY datecolumn DESC;

. sql server . datcolumn =..... , .

, .

: datacolumn , . ,

0

All Articles