This is a discussion on SQL query question within the pgsql Sql forums, part of the PostgreSQL category; --> I am new to this question. Can someone help me to get the query result? 1. Given the following ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| I am new to this question. Can someone help me to get the query result? 1. Given the following schema of an employees table: *employees (* * empid integer primary key, -- employee id number* * dept string, -- the employee's department number* * salary float, -- salary of the employee* * mgrid integer -- employee id number of employee's manager* *)* Write the query (or queries if necessary) needed to count the number of employees in each employee's department who are paid more than their manager. All employees (including managers) appear in this table. Each employee has a manager (except the CEO), and the mgrid in the employee's row is the empid of the employee's manager. - Thanks |
| |||
| On Sep 21, 12:09 am, stevenleong...@gmail.com wrote: > Write the query (or queries if necessary) needed to count the number > of employees in each employee's department who are paid more than > their manager. SELECT e.dept, COALESCE (SUM (1), 0) AS n FROM employees e JOIN employees m ON (e.empid = m.mgrid AND e.salary > m.salary) GROUP BY e.dept ORDER BY e.dept |
| ||||
| stevenleong...@gmail.com wrote: >> Write the query (or queries if necessary) needed to count the number >> of employees in each employee's department who are paid more than >> their manager. Doesn't that smell like a homework problem? Is it a homework problem? Rodrigo De León wrote: > SELECT e.dept, COALESCE (SUM (1), 0) AS n > FROM employees e JOIN employees m > ON (e.empid = m.mgrid AND e.salary > m.salary) > GROUP BY e.dept > ORDER BY e.dept I like the idea of posting an answer after the homework is due. That way the student can learn without jeopardizing their homework experience. Good job. -- Lew |