Here is a common structure I use in SQL Server that allows me, for example, to select a specific service record in a SERVICE table for each client in a CLIENT table. In this scenario, a client record can have 0, 1 or many service records. I want the most recent service record for each client (according to the start date of the service). And if a client doesn't have a service record, I still want the client's record in the result set (with NULLs to be displayed in the service record fields).
SELECT
A.client_id,
A.client_name,
A.client_city,
B.service_id,
B.service_type,
B.service_start_date,
B.service_end_date
FROM
client AS A
LEFT JOIN
service AS B
ON
B.service_id = (SELECT TOP 1 X.service_id FROM service AS X
WHERE X.client_id = A.client_id
ORDER BY X.service_start_date DESC)
I have tried several variants of the solutions I've found on-line, but nothing appears to work for me