Unix Technical Forum

SQL query question

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 ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Sql

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-19-2008, 05:55 PM
stevenleongusa@gmail.com
 
Posts: n/a
Default SQL query question

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-19-2008, 05:57 PM
=?iso-8859-1?q?Rodrigo_De_Le=F3n?=
 
Posts: n/a
Default Re: SQL query question

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

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-19-2008, 05:58 PM
Lew
 
Posts: n/a
Default Re: SQL query question

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:32 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com