so I want to make data where the difference of years from hiredate to the current date (wearing sysdate) has a difference of 0 to 2 years then it will be junior, and if 3 to 5 then go to senior
i use code like this
select first_name, trunc(months_between(sysdate,hire_date) /12) as information from employees;
desc from employees -
employee_id,first_name,hire_date;
Related
I have a table called employees and I I need to get the age of the youngest employee in years.
For example if I look at the table the youngest employee is "57 years old"
The columns:
EmployeeID, Lastname, Title, Birthdate, Hiredate, City, Country
The code I was trying was this:
SELECT MAX(birthdate)
FROM employees;
With that I can get the date of birth of the youngest employee, but now I need to somehow compare it with the current date that would be using "sysdate" and then change it to numbers so that it shows that he is 57 years old, but I have not succeeded
You can use:
SELECT TRUNC(MONTHS_BETWEEN(SYSDATE, MAX(birthdate))/12) AS age
FROM employees;
Which, for the sample data:
CREATE TABLE employees ( id, birthdate ) AS
-- First employee will be 20 tomorrow
SELECT 1, ADD_MONTHS(TRUNC(SYSDATE), -20*12) + INTERVAL '1' DAY FROM DUAL UNION ALL
-- Second employee is 25 today
SELECT 2, ADD_MONTHS(TRUNC(SYSDATE), -25*12) FROM DUAL;
Outputs:
AGE
19
fiddle
You can subtract your MAX(birthdate) from SYSDATE - the result is number of days so you should convert days to years.
WITH
tbl AS
(
select
to_date('09-09-1965', 'dd-mm-yyyy') birthdate,
SYSDATE my_sysdate
from dual
)
SELECT FLOOR((my_sysdate - MAX(birthdate))/ 365) "YEARS_OLD" From tbl
YEARS_OLD
-----------
57
If you don't care much about months and days, a simple option is to extract year from sysdate and youngest birthdate and subtract them:
Sample data:
SQL> with employees (employeeid, lastname, birthdate) as
2 (select 1, 'Little', date '2015-08-25' from dual union all --> youngest
3 select 2, 'Foot' , date '2000-11-13' from dual
4 )
Query:
5 select extract(year from sysdate) - extract(year from max(birthdate)) as age
6 from employees;
AGE
----------
8
SQL>
SELECT LAST_NAME
,DEPARTMENT_ID
,ROUND(MONTHS_BETWEEN (SYSDATE, hire_date)) MONTHS_WORKED
FROM EMPLOYEES
WHERE DEPARTMENT_ID = 90
ORDER BY MONTHS_WORKED;
This select is for "hr" schema in Oracle.
The question is how to make it to show the years and months worked?
We could build on your current attempt with months_between. We can divide the result by 12 to get the number of years; the modulo (ie the remainder) represents the number of months:
select
last_name,
department_id,
floor(months_between(sysdate, hire_date)/12) years_worked,
floor(mod(months_between(sysdate, hire_date), 12)) months_worked
from employees
where department_id = 90
order by years_worked, months_worked;
I have a employee table with DOB column so, now I want to display all employees current age.
To find out the age I knew the query as below
SELECT *
FROM (SELECT ename,
job,
dob,
TRUNC(MONTHS_BETWEEN(SYSDATE,DOB)/12,1) AGE
FROM employee);
by using this query I can get all employees age but my requirement is, if a employee age is displayed as 2.5 then I need to display that age as 2 years 6 months.
Try this ---
SELECT ename,
job,
dob, --TRUNC(MONTHS_BETWEEN(SYSDATE, DOB) / 12, 1) AGE
trunc(months_between(sysdate, DOB) / 12) || ' years ' ||
trunc(mod(months_between(sysdate, DOB), 12)) || ' months ' AGE
FROM employee;
This question already has answers here:
How to query for a specific day of the month in Oracle
(3 answers)
Closed 6 years ago.
This is the schema of my table EMP :
CREATE TABLE EMP (
EMPNO NUMBER(4) NOT NULL,
ENAME VARCHAR2(10),
JOB VARCHAR2(9),
MGR NUMBER(4) CONSTRAINT EMP_SELF_KEY REFERENCES EMP (EMPNO),
HIREDATE DATE,
SAL NUMBER(7,2),
COMM NUMBER(7,2),
DEPTNO NUMBER(2) NOT NULL);
I have to list the employees those who joined in company before 15th of the month , so here month can be any month and we have to list name of all employees. I am unable to extract the month from hiredate and year from hiredate along with where clause as moth can be any month and year ranges from 1980 to 1990 so I will also have to check whether the year lies in the range of 1980-1990.
Use EXTRACT on the HIREDATE and check if it is less than 15:
SELECT * FROM EMP WHERE EXTRACT(day FROM HIREDATE) < 15
More about EXTRACT function: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm
SELECT * FROM EMP
WHERE EXTRACT(DAY FROM DATECOL) < 15
AND EXTRACT(YEAR FROM DATECOL) BETWEEN 1980 AND 1990;
Try this
select * from EMP where to_char(hiredate,'DD') < 15 and to_char(hiredate,'YYYY') between 1980 and 1990
Can anyone help me , i need to write query for next case .
It is necessary to draw from the table asking the average salary of employees in the company for more than 5 years, and the average salary of employees in the company up to 5 years.
SELECT name,avg(salary) FROM employees WHERE HIREDATE < (19-july-09) AND
HIREDATE > (19-JUL-09) ''
Is this good?
That's certainly not right - you're asking for employees that have been employed before 19-july-09 and also employed after 19-july-14, so no-one will fulfill both (in fact, no one will fulfil the second condition, since you won't have people you haven't hired yet in the database.
You don't say what database you are using. Also, what do you want out of this exactly? Do you want something like:
Avg salary of employees hired at least 5 years ago = xxxxx
Avg salary of employees hired less than 5 years ago = yyyyy
Just very roughly, this would look something like this (I think this syntax is MySQL compliant):
SELECT CASE WHEN HIREDATE < '2009-07-09' then 1 else 0 END AS five_years,
avg(salary)
FROM employees
GROUP BY CASE WHEN HIREDATE < '2009-07-09' then 1 else 0 END
or alternatively, I think this might work in MySQL to avoid the constants:
SELECT CASE WHEN HIREDATE < date_add(NOW(), INTERVAL -5 YEAR) then 1
ELSE 0 END AS five_years,
avg(salary)
FROM employees
GROUP BY CASE WHEN HIREDATE < date_add(NOW(), INTERVAL -5 YEAR) then 1
ELSE 0 END