I have an employee table where the fields are:
first_name, last_name, hire_date, salary, department_id, department_name, and so on.
I intend to find the hire date difference between EMPLOYEE1 and EMPLOYEE2, then EMPLOYEE2 and EMPLOYEE3, and so on.
I have to write a query in sql to display the first name and hire date difference of employee
We can use DATEDIFF to calculate the date difference.
e.g
SELECT DATEDIFF(SELECT DATE_ADD(start_date,INTERVAL 1 DAY),end_date);
hope it will help you
also there is also way of using to_days function. click here for more detail
Since you've still not mentioned what RDBMS you are using i'll start with SQL-Server:
WITH x
AS (SELECT first_name,
last_name,
hire_date,
salary,
department_id,
department_name,
hireNum=Row_number()
OVER(
ORDER BY hire_date)
FROM dbo.employee)
SELECT DiffDays=Datediff(day, x.hire_date, x2.hire_date),
first_name,
last_name,
hire_date,
salary,
department_id,
department_name
FROM x
INNER JOIN x x2
ON x.hirenum = x2.hirenum + 1
To find the difference between dates in Microsoft SQL 2012 using days (substitute day with year, hour, etc.):
Select datediff(day, HireDate, EndDate)
From Employee1
Related
There is a task: using the HR.EMPLOYEES table, get a list of departments in which the average work experience is above the average for the entire company.
I tried to implement it this way. I know that the request is not correct, but I don’t understand how to distribute it to the entire company
select department_id
from hr.employees
where avg(MONTHS_BETWEEN(sysdate, hire_date)) > (select hire_date from hr.employees where avg(MONTHS_BETWEEN(sysdate, hire_date))
The database looks like this:
EMPLOYEE_ID FIRST_NAME LAST_NAME EMAIL PHONE_NUMBER HIRE_DATE JOB_ID SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID
100 Steven King SKING 515.123.4567 17-JUN-03 AD_PRES 24000
This is one way of doing it:
USE [tempdb]
IF OBJECT_ID('hr') IS NOT NULL DROP TABLE hr
CREATE TABLE hr (department_id int, hire_date datetime)
INSERT INTO hr VALUES (1,'20210101'),(2,'20211001'),(1,'20210801')
SELECT * FROM hr
SELECT department_id, AVG(DATEDIFF(DAY, hire_date, getdate())) AS "days"
from hr
group by department_id
SELECT department_id, AVG(DATEDIFF(DAY, hire_date, getdate())) AS "days"
from hr
group by department_id
having AVG(DATEDIFF(DAY, hire_date, getdate())) > (SELECT AVG(DATEDIFF(DAY, hire_date, getdate())) AS "days" from hr)
I am getting 'not a valid month' error for the following code:
SELECT last_name, employee_id, hire_date
FROM employees
WHERE EXTRACT(YEAR FROM TO_DATE(hire_date, 'DD-MON-RR')) > 1998
ORDER BY hire_date;
I don’t see the point for using extract() here. It is suboptimal, because the database needs to apply the function to all values in the column before it is able to filter. I would recommend direct filtering against a literal date:
where hire_date >= date '1999-01-01'
This predicate would take advantage of an index on hire_date. You can even add more columns to the index to entirely cover the query like: (hire_date, last_name, employee_id).
Assuming this is the employees table from Oracle's tutorial, hire_date is already a date column. You don't need to use to_date on it:
SELECT last_name, employee_id, hire_date
FROM employees
WHERE EXTRACT(YEAR FROM hire_date) > 1998
ORDER BY hire_date;
I have an employees table:
I am trying to determine the length of time certain employees - e.g. Luke and Helen - have been working since the start date up to now, along with the Team they belong to.
So far I can show the table based on their name and team eg:
SELECT EmployeeName, Team
FROM employees
WHERE EmployeeName in ('Luke', 'Helen');
But I am having issues trying to show the table with the length of time each employee has been in the company:
SELECT EmployeeName, Team, count (*) AS Tenure
FROM
(
SELECT EmployeeName, Team
FROM employees
WHERE dateddiff(mm, StartDate, getdate()) AND EmployeeName in ('Luke', 'Helen')
)a
GROUP BY EmployeeName, department;
Any help would be greatly appreciated
You need to select the columns that you want. These appear to be:
SELECT EmployeeName, Team, DATEDIFF(month, StartDate, getdate())
FROM employees
WHERE EmployeeName IN ('Luke', 'Helen');
The DATEDIFF() goes in the SELECT, not the WHERE.
I have an employees table with their name, hire_date and salary on it. Now what I am trying to get is the each year's spending of the company on salaries, but every time I try to do it, it gives me each hire_date in the output and respective salary.
select
hire_date, dateadd(year, 1, hire_date), sum(salary)
from
employees
where
hire_date between hire_date and dateadd(year, 1, hire_date)
group by
hire_date
The answer is in your requirements. If you need the total salary cost per year it is implied you use SUM on salary and GROUP BY year:
select
YEAR(hire_date), SUM(salary)
from
employees
group by
YEAR(hire_date)
I have a question that says Write a SELECT statement to get a list of employees hired in the period from July 1998 to December 1999 . The list must contain the employee id the last and first name and the monthly salary – given that the table contains the yearly salary and that employees get 12 salaries per year.
I wrote this,but does not work:
SELECT FIRST_NAME, LAST_NAME, EMPLOYEE_ID, SALARY/12 as MONTHLY_SALARY
FROM HR.EMPLOYEES
WHERE HIRE_DATE BETWEEN JULY-1998 AND DECEMBER-1999 ;
Could be you have a wrong date format (for mysql)
SELECT FIRST_NAME, LAST_NAME, EMPLOYEE_ID, SALARY/12 as MONTHLY_SALARY
FROM HR.EMPLOYEES
WHERE HIRE_DATE BETWEEN STR_TO_DATE('07-01-1998, '%m-%d-%Y)
AND STR_TO_DATE('12-31-1999, '%m-%d-%Y) D ;
For sqlserver
SELECT FIRST_NAME, LAST_NAME, EMPLOYEE_ID, SALARY/12 as MONTHLY_SALARY
FROM HR.EMPLOYEES
WHERE HIRE_DATE BETWEEN Cast('07-01-1998'as datetime)
AND Cast('12-31-1999' as datetime) ;
And for ORACLE
SELECT FIRST_NAME, LAST_NAME, EMPLOYEE_ID, SALARY/12 as MONTHLY_SALARY
FROM HR.EMPLOYEES
WHERE HIRE_DATE BETWEEN TO_DATE('07-01-1998' , 'MM-DD-YYYY')
AND TO_DATE('12-31-1999' , 'MM-DD-YYYY') ;