CROSS APPLY is the Ferrari of SQL join techniques.
It looks fancy, and it really is. 🏎️
⚡ Why CROSS APPLY Is Amazing
-
🧠 Lets you join custom row-by-row logic
-
🚀 Runs faster than correlated subqueries
-
🔍 Works great with TOP, aggregates & ranking
-
🪄 Cleaner than temp tables
SELECT u.Id, x.LastOrder
FROM Users u
CROSS APPLY (
SELECT TOP 1 Amount AS LastOrder
FROM Orders o
WHERE o.UserId = u.Id
ORDER BY o.Date DESC
) x;
💡 Bonus
Perfect for pagination, scoring, and “latest record” lookups.
