Filter records and find those with minimal date I have the following tables in Microsoft SQL Server:

Users: Includes uID (primary key), birthDate, name
Conversations: Includes convID (primary key), hostID (foreign key for uID in Users)
Participants: Includes uID (foreign key for uID in Users), convID (foreign key for convID in Conversations).

I need to write a query which finds the name, ID and birth date of the oldest user who didn't participate in any conversation, and that his name contains the letter 'b'. If there is more than one user, I need to return all of them.
I don't know how to both filter the users, and than find those with the minimal birth date (the oldest).
This query should obtain what you need (assuming &quot;didn't participate in any conversation&quot; means no records exist for that user in the Participants table.)
WITH CTE_Users_SortedByBirthDate AS -- Sort Users by their birthdate
(
    SELECT [uID], birthDate, [name], ROW_NUMBER() OVER (ORDER BY birthDate) AS OldestUser -- Note if two users have the same birthDate they'll both be returned, if you add uID after birthDate in the ORDER BY clause, then it'll guarantee you only one user: the oldest user by birthDate and by user ID
    FROM Users 
    WHERE [name] LIKE '%b%' -- User's name contains the letter &quot;b&quot;
)

SELECT U.[uID], U.birthDate, U.[name]
FROM CTE_Users_SortedByBirthDate AS U
LEFT JOIN Participants AS P
    ON U.[uID] = P.[uID]
WHERE P.[uID] IS NULL -- Only users who have no Participant records
    AND U.OldestUser = 1 -- Returns only the oldest user(s) by birthDate