2 ms·
You can use ORDER BY in connection with TOP n, for example if you want to use the result of a subquery as a column: SELECT user_name, (SELECT TOP 1 l
by pfg 13y ago
You can use ORDER BY in connection with TOP n, for example if you want to use the result of a subquery as a column:
SELECT
user_name,
(SELECT TOP 1 last_action FROM actions WHERE actions.user_id=users.user_id ORDER BY action_timestamp)
FROM
users
(TOP is T-SQLs version of LIMIT)
Also, thanks for making me aware of CROSS APPLY, haven't seen that before!
- chris_mahan 13y agoSure. Note also that CROSS APPLY allows you to retrieve multiple columns, unlike an in-SELECT subquery (unless you want to have that query written multiple times)