Home > Blockchain >  Write a Query in SQL server that prints a list of employee names who have been employed for less tha
Write a Query in SQL server that prints a list of employee names who have been employed for less tha

Time:12-18

I want a SQL query that prints a list of employee names who have been employed for less than 10 months having salary>2000. Sort this result by ascending emp_id.

I tried this but gave me an error stating extract is no longer used.:

select * from emp where 
salary>2000 and (months_between(date, hire_date))<=10
order by emp_no asc ;

How do I form the query?

CodePudding user response:

SQL will automatically order by ascending, so you don't have to add that but you can if you want. The query should look something like this:

SELECT * 
FROM emp
WHERE salary >2000
AND hire_date >= DATEADD(Month, -10, getdate())
ORDER BY emp_no

CodePudding user response:

SELECT *
FROM Emp 
WHERE Salary > 2000
  AND DATEDIFF(month, Hire_Date, GetDate()) < 30
ORDER BY emp_no ASC;
Emp_No Employee_Name Job ManagerID Hire_Date Salary Comm DepartmentID
7566 JONES MANAGER 7839 2021-04-02 2975.00 null 20
7698 BLAKE MANAGER 7839 2021-05-01 2850.00 null 30
7782 CLARK MANAGER 7839 2021-06-09 2450.00 null 10
7788 SCOTT ANALYST 7566 2021-07-13 3000.00 null 20
7839 KING PRESIDENT null 2021-11-17 5000.00 null 10
7902 FORD ANALYST 7566 2021-12-03 3000.00 null 20

db<>fiddle here

  •  Tags:  
  • sql
  • Related