I am picking up some essential SQL query skills, and working with dates. I was successful in getting the following.
SELECT
[FirstName],
[LastName],
[JobTitle],
[JoiningDate]
DATEDIFF(day, StartDate, '2021/03/20') AS DateDifference
FROM
[GenericITCompany].[dbo].[Employees]
WHERE
JobTitle = 'UnityDeveloper';
This returns the number of days, every developer from the Unity team, has been employed with the company.
Now, I wish to find out who has been with the company the longest.
I tried this:
SELECT
[FirstName],
[LastName],
[JobTitle],
[JoiningDate]
DATEDIFF(day, StartDate, '2021/03/20') AS DateDifference
MAX(DateDifference) AS LongestServingEmployee
FROM
[GenericITCompany].[dbo].[Employees]
WHERE
JobTitle = 'UnityDeveloper';
That is not working. I am possibly missing something very obvious.
Note1 : I understand basic usage of Max. For example,
SELECT
MAX(StartDate) AS MaximumStartDate
FROM
[GenericITCompany].[dbo].[Employees]
But, I am primarily having challenges applying MAX to a query generated table. I believe that is my main problem.
Note2 : I have looked at some existing questions with similar issues
Fetch the row which has the Max value for a column
How can I SELECT rows with MAX(Column value), DISTINCT by another column in SQL?
But I am not able to understand from them.