good
next stage is to join the original table onto this query
when a query is used in the FROM clause, as a subquery, it is known as an
inline view or
derived table, because you can think of the results it produces as thought they were a table, which they are
so the rows would be one row per employee and the columns would be emp_id and latest date
Code:
SELECT ...
FROM ( previous query goes here ) AS q
INNER
JOIN emp_monthly_sales AS t
ON t.emp_id = q.emp_id
AND t.dt = q.latest_date
you are joining each employee's latest date
now, as a result of the join, you can show the value for t.sales_amount, and you are now sure that it is the correct amount for the latest date