How do you write a query to find the employees who joined the company in the last 6 months?
Posted by OliviaWm
Last Updated: July 07, 2024
To write a query to find employees who joined the company in the last 6 months, you will generally use a SQL SELECT statement with a WHERE clause that filters the results based on the hire date (or equivalent field) of the employees. The specifics of the query can vary depending on the SQL dialect you are using (e.g., MySQL, PostgreSQL, SQL Server, etc.), but here’s a general example assuming you have a table named employees with a column hire_date:
Example SQL Query
SELECT *
FROM employees
WHERE hire_date >= DATEADD(MONTH, -6, GETDATE());
SQL Dialect Examples:
- MySQL:
SELECT *
    FROM employees
    WHERE hire_date >= NOW() - INTERVAL 6 MONTH;
- PostgreSQL:
SELECT *
    FROM employees
    WHERE hire_date >= CURRENT_DATE - INTERVAL '6 months';
- SQL Server:
SELECT *
    FROM employees
    WHERE hire_date >= DATEADD(MONTH, -6, GETDATE());
Explanation:
- SELECT * selects all columns from the employees table. - WHERE hire_date >= filters the results to only include employees whose hire_date is within the last 6 months. - DATEADD(MONTH, -6, GETDATE()) or corresponding functions in other SQL dialects calculates the date 6 months ago from the current date. Make sure to adjust the table name and column names if they are different in your database schema.
Related Content
Company Database using SQL
Company Database using SQL
Samath | Jan 18, 2024