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)
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 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') ;
I'm trying to write a sql query to figure out by how much have salaries increased in the last year for each department due to new employees.
Table structure is Employees (empno, deptno, msal, hiredate)
I can figure out how to get all the salaries by departments
SELECT sum(msal) FROM employees GROUP BY deptno;
and how to get the salaries from people who were hired in the past year
SELECT sum(msal) FROM employees WHERE hiredate > (DATEADD(year, -1, GETDATE())) GROUP BY deptno;
But whatever way I try to subtract the result of these two queries I only get errors.
Here is what you might do. In this case I'm using a CASE statement to filter new employees:
SELECT SUM(msal) - SUM(CASE WHEN hiredate > ADD_MONTHS(SYSDATE, -12) THEN msal ELSE 0 END)
FROM employees
GROUP BY deptno;
FYI, Oracle doesn't have a DATEADD() function nor does it have a GETDATE() function. Note that I used ADD_MONTHS() and SYSDATE (you could also use CURRENT_DATE) in place of these.
Why not just change the direction of the where?
SELECT sum(msal)
FROM employees
WHERE hiredate <= DATEADD(year, -1, GETDATE())
GROUP BY deptno;
Also, normally when you aggregate by a field, then the field (deptno) is included in the select clause.
I'm trying to create a simple function that will allow me to determine the salary of an employee depending on the year they were hired, and then insert the salary they have into a column called "Salary" on that table.
If the employee was hired in 2011 his salary would be 5,000
If the employee was hired in 2012 his salary would be 6,000
If the employee was hired in 2013 his salary would be 7,000
The date they were hired in is stored in a column called "Hired", I can't figure out how to extract the data and use it as a number to define the salary with an IF statement. I know I probably need a TO_char function too, but I don't know where to use it to get the date converted into a string.
Thanks for any help/ideas on how to do this.
You would use a case statement. Here is one way:
select (case when to_char(DateHired, 'YYYY') = '2011' then 5000
when to_char(DateHired, 'YYYY') = '2012' then 6000
when to_char(DateHired, 'YYYY') = '2013' then 7000
end) as Salary
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