Nested subqueries in SQL from hr.employees - sql

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)

Related

I am trying to filter an alias column, which I know it wont work in the WHERE clause. I need to filter the YEARS >15

select department_id, last_name, round(((sysdate-hire_date)/30)/12, 0) as YEARS
from employees
where YEARS >15;
You can't refer to the alias in the WHERE clause. One option is to simply repeat the entire round expression:
SELECT department_id, last_name,
ROUND(((SYSDATE - hire_date) / 30) / 12, 0) AS YEARS
FROM employees
WHERE ROUND(((SYSDATE - hire_date) / 30) / 12, 0) > 15;
One option is to repeat the round expression and also you can use DATEDIFF which is another option.
SELECT department_id, last_name,
ROUND(((SYSDATE - hire_date) / 30) / 12, 0) AS YEARS
FROM employees
Where DATEDIFF(year, hire_date, getdate()) > 15
Thank you guys, I came up with a resolution this morning
select department_id, sum(salary) as "Summary"
from employees
where round(((sysdate-hire_date)/30)/12, 0)>15
group by department_id
order by department_id;
this is the right code
select department_id, sum(salary) as "Summary"
from employees
where round(((sysdate-hire_date)/30)/12, 0)>15
group by department_id
order by department_id;

How to get year wise spending on salaries using SQL Server

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)

ORA-00904: "DAYS": invalid identifier

Here is the code that I have:
SELECT (first_name || ' ' || last_name) AS Name, hire_date, days FROM EMPLOYEES WHERE days > 365*6;
This is what I am supposed to do:
Create a SQL query that shows the first_name, last_name, hire_date, and the number of days that the employee has worked from the EMPLOYEES table. Use the concatenation operator to put the first_name and the last_name together in the same column in the result set with a space between the two names. Use date arithmetic to show only employees that have worked longer than 6 years (365 * 6).
Can someone help me figure out what my error is?
To correct your query you need to perform date arithmetic.
SELECT
(first_name || ' ' || last_name) AS name,
hire_date,
ROUND(sysdate - hire_date) AS days
FROM hr.employees
WHERE sysdate - hire_date > 6*365;
But if you analyse -> hr schema, you find out, that there is job_history table relevant for this task. In my opinion correct solution should be like this:
SELECT a1.name,
a1.hire_date,
a1.days
FROM
(SELECT employees.employee_id,
(employees.first_name || ' ' || employees.last_name) AS name,
employees.hire_date,
SUM(NVL(job_history.end_date, TRUNC(sysdate)) - NVL(job_history.start_date, hire_date)) AS days
FROM hr.employees
LEFT JOIN hr.job_history
ON job_history.employee_id = employees.employee_id
GROUP BY employees.employee_id,
(employees.first_name || ' ' || employees.last_name),
employees.hire_date
) a1
WHERE days > 365*6;
You may try to put filter on the hire day instead
WHERE hire_date > sysdate - 365*6
this will work;
WHERE sysdate-hire_date >= 365*6

Query- SQL Developer

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') ;

Find hire date difference between two employees

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