To find all employees who joined in the last 6 months, I use a date function to compare the employee’s joining date with the current date minus six months. The exact syntax can vary slightly depending on the SQL database, but the logic is the same — filter rows where the joining date is within the last six months from today.
For example, in SQL Server,
SELECT *
FROM Employees
WHERE JoinDate >= DATEADD(MONTH, -6, GETDATE());
Here, DATEADD(MONTH, -6, GETDATE()) calculates the date that’s six months before the current date. So this query returns all employees whose JoinDate is greater than or equal to that date — meaning they joined within the last six months.
In MySQL, the equivalent would be:
SELECT *
FROM Employees
WHERE JoinDate >= DATE_SUB(CURDATE(), INTERVAL 6 MONTH);
And in Oracle, it would look like:
SELECT *
FROM Employees
WHERE JoinDate >= ADD_MONTHS(SYSDATE, -6);
One challenge I faced was ensuring time zone consistency — for example, if JoinDate includes timestamps and the system stores data in UTC but reports are generated in local time, the date comparison can slightly vary. To avoid that, I usually standardize dates using functions like CAST(JoinDate AS DATE) to compare only the date portion.
So in short — the query finds all employees whose joining date is within the past 6 months by subtracting 6 months from the current date using date functions like DATEADD(), DATE_SUB(), or ADD_MONTHS() depending on the database.
