These two queries seem to return the same results. Is that coincidental or are they really the same?
1.
SELECT t.ItemNumber,
(SELECT TOP 1 ItemDes
Based on the data & simple queries, both will return the same results. However, the fundamental operations are very different.
DISTINCT
, as AakashM beat me to pointing out, is applied to all column values, including those from subselects and computed columns. All DISTINCT
does is remove duplicates, based on all columns involved, from visibility. This is why it's generally considered a hack, because people will use it to get rid of duplicates without understanding why the query is returning them in the first place (because they should be using IN
or EXISTS
rather than a join, typically). PostgreSQL is the only database I know of with a DISTINCT ON
clause, which does work as the OP probably intended.
A GROUP BY
clause is different - it's primary use is for grouping for accurate aggregate function use. To server that function, column values will be unique values based on what's defined in the GROUP BY clause. This query would never need DISTINCT, because the values of interest are already unique.
This is a poor example, because it portrays DISTINCT and GROUP BY as equals when they are not.