sql cross apply vs join 1

sql cross apply vs join

SELECT M.ID,M.NAME,D.PERIOD,D.QTY
FROM MASTER M
INNER JOIN
(
    SELECT TOP 2 ID, PERIOD,QTY 
    FROM DETAILS D      
    ORDER BY CAST(PERIOD AS DATE)DESC
)D
ON M.ID=D.ID

Here is what the above code is Doing:
1. Selecting the top 2 rows from the details table, ordered by the period column as a date, in descending order.
2. Joining the master table to the above result set, on the ID column.

Similar Posts