CTE exercise 1
I am getting a different value by using this query can you please check
I have used a slightly different approach
First I have created a table emp_no | gender | salary and used a cross join with avg_sal with a filter criteria of male employees who have signed a contract above the average salary.
After that I used a left join with employees table to identify male employees with salary below average.
SELECT COUNT(e.emp_no) as num_of_emp_below_avg from employees e
LEFT JOIN (SELECT s.emp_no, e.gender, s.Salary, c.avg_sal
FROM salaries s join employees e on s.emp_no = e.emp_no
CROSS JOIN (SELECT AVG(SALARY) AS avg_sal FROM SALARIES) AS c
WHERE e.gender = "M" AND s.salary > c.avg_sal) AS B on e.emp_no = B.emp_no
where e.gender = "M";
0 answers ( 0 marked as helpful)