returns one row for each Job_ID with the following columns:
β’ J ob ID
β’ Average length of service for women in this job in years rounded to the tenth of a year
β’ The average salary of women for this job
β’ Average length of service for men in this job in years rounded to the tenth of a year
β’ The average salary of men for this job
β’ The difference in the average salaries of men and women for this job, so a positive number means that the average salary of women is higher, and a negative number means that the average salary of men is higher.
Table
HR.EMPLOYEES
Name Null Type -------------- -------- ------------ EMPLOYEE_ID NOT NULL NUMBER(6) FIRST_NAME VARCHAR2(20) LAST_NAME NOT NULL VARCHAR2(25) EMAIL NOT NULL VARCHAR2(25) PHONE_NUMBER VARCHAR2(20) HIRE_DATE NOT NULL DATE JOB_ID NOT NULL VARCHAR2(10) SALARY NUMBER(8,2) COMMISSION_PCT NUMBER(2,2) MANAGER_ID NUMBER(6) DEPARTMENT_ID NUMBER(4) GENDER CHAR(1)
What I have so far , but this returns a row M and F for each job_id I need columns M and F
SELECT gender, job_id, ROUND(AVG(salary),0) as avg_job_salary, (SELECT ROUND(AVG(salary),0) FROM hr.employees WHERE gender = 'M') AS avg_m_salary, (SELECT ROUND(AVG(salary),0) FROM hr.employees WHERE gender = 'F') AS avg_f_salary, ROUND(AVG(days_of_svc/365),1) AS avg_years_svc FROM (SELECT job_id, salary, gender, (SYSDATE-hire_date) AS days_of_svc FROM hr.employees) GROUP BY job_id,gender ORDER BY job_id, gender;
or version 2
SELECT gender, job_id, ROUND(AVG(salary),0) as avg_job_salary, ((SELECT ROUND(AVG(salary),0) FROM hr.employees WHERE gender = 'F') - (SELECT ROUND(AVG(salary),0) FROM hr.employees WHERE gender = 'M')) as diff, ROUND(AVG(days_of_svc/365),1) AS avg_years_svc FROM (SELECT job_id, salary, gender, (SYSDATE-hire_date) AS days_of_svc FROM hr.employees) GROUP BY job_id,gender ORDER BY job_id, gender;
example of expected result lines
JOB_ID F_AVG_LENGTH F_AVG_SAL M_AVG_LENGTH M_AVG_SAL DIFFERENCE ------ ------------ --------- ------------ --------- ---------- MAILCLERK 24.1 48000 23.4 47000 1000 CASHIER 4.6 12000 4.4 13500 -1500