all groups > sql server programming > september 2007 >
You're in the

sql server programming

group:

SQL query question


SQL query question stevenleongusa NO[at]SPAM gmail.com
9/20/2007 10:11:23 PM
sql server programming:
I am new to SQL query. Can someone help me to solve this question?

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.
Re: SQL query question amish
9/21/2007 4:20:29 AM
[quoted text, click to view]

create table employees (
empid integer primary key, -- employee id number*
dept varchar(100), -- the employee's department
number*
salary float, -- salary of the employee*
mgrid integer -- employee id number of employee's
)


insert into employees values (1,'CEO',10000,null)
insert into employees values (2,'dep1',5000,1)
insert into employees values (3,'dep1',5000,1)
insert into employees values (4,'dep2',6000,2)
insert into employees values (5,'dep2',4000,2)
insert into employees values (6,'dep2',7000,3)
insert into employees values (7,'dep2',4000,3)


select e.* from employees e inner join employees e1 on e.mgrid =
e1.mgrid and e.salary > e1.salary
Re: SQL query question amish
9/21/2007 4:22:29 AM
[quoted text, click to view]

minor change in the query

select e.* from employees e inner join employees e1 on e.mgrid =
e1.empid and e.salary > e1.salary

Re: SQL query question stevenleongusa NO[at]SPAM gmail.com
9/21/2007 9:14:04 AM
Thanks. It works.

[quoted text, click to view]

Re: SQL query question --CELKO--
9/22/2007 1:16:41 AM
[quoted text, click to view]

I seem to remember this question from intro Database textbook that
used the Scott/Tiger sample DB. If this is a homework assignment, we
will track you down and get you expelled.

You did not post DDL but some weird personal narrative and it was
wrong in so many ways. Invalid data types; non-existent data types;
data element names that violate ISO-11179 rules; etc.

CREATE TABLE Personnel -- set of entities
(emp_id INTEGER NOT NULL PRIMARY KEY,
dept_name CHAR(8) NOT NULL,
salary_amt DECIMAL(8,2) NOT NULL, -- never use FLOAT for money!!
/* the following is a redundant design and stinks, but you wrote it
mgr_emp_id INTEGER
REFERENCES Personnel(emp_id),
CHECK (emp_id <> mgr_emp_id), --assumption */
);

If you had a relational design, then there would be another table:

CREATE TABLE OrgChart -- a relationship!
(emp_id INTEGER NOT NULL -- might be PK, might not
REFERENCES Personnel (emp_id),
..);

There are several models for the Organizational chart;I like the
Nested Sets model, but you will also find Path Enumeration and
Adjacency List models. You were trying to shove an adjacency list
into an entity table -- a common design error.

[quoted text, click to view]
Each employee has a manager (except the CEO), and the mgr_id in the
employee's row is the emp_id of the employee's manager. <<

[quoted text, click to view]

How many levels of management are involved? Two? Hundreds? Assume
only two levels:

SELECT P1.dept_name, COUNT (P1.emp_id)
FROM Personnel AS P1, Personnel AS P2
WHERE P1.dept_name = P2.dept_name
AND P1.emp_id = P2.mgr_emp_id
AND P1.salary_amt > P2.salary_amt ;

Re: SQL query question stevenleongusa NO[at]SPAM gmail.com
9/23/2007 9:26:40 PM
The answer is not right. I add Group by

SELECT P1.dept, COUNT( P1.empid )
FROM employees AS P1, employees AS P2
WHERE P1.dept = P2.dept
AND P1.empid = P2.mgrid
AND P1.salary > P2.salary
GROUP BY P1.dept

This one is much accurate.
select e.dept, count(e.empid) from employees e inner join employess e1
on e.mgrid =
e1.empid and e.salary > e1.salary group by e.dept


[quoted text, click to view]

AddThis Social Bookmark Button