From: | "Bill Boxall" <bboxall(at)landover(dot)net> |
---|---|
To: | pgsql-general(at)postgresql(dot)org |
Subject: | Got stumped.. Need assistance with a sql query |
Date: | 2001-04-13 10:29:59 |
Message-ID: | HyAB6.567374$f36.16906178@news20.bellglobal.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Not sure if this is the forum for this.. If it isn't, I'm sure I'll hear
about it!
I'm looking for help.. Just learning sql, and I'm stuck on this one
exercise.. A query joining 3 tables.. Here is the question:
List the employee name, job, salary, grade and department name for everyone
in the company except clerks. Sort on salary, displaying the highest salary
first.
Here is what I've come up with:
select distinct ename, job, sal, grade, dname from emp, salgrade, dept where
emp.job != 'CLERK' and emp.deptno = dept.deptno order by sal desc;
This query gives me the following output..
ENAME JOB SAL GRADE DNAME
---------- --------- ---------- ---------- --- -----------
KING PRESIDENT 5000 5 ACCOUNTING
KING PRESIDENT 5000 4 ACCOUNTING
KING PRESIDENT 5000 3 ACCOUNTING
KING PRESIDENT 5000 2 ACCOUNTING
KING PRESIDENT 5000 1 ACCOUNTING
FORD ANALYST 3000 1 RESEARCH
FORD ANALYST 3000 2 RESEARCH
FORD ANALYST 3000 3 RESEARCH
FORD ANALYST 3000 4 RESEARCH
FORD ANALYST 3000 5 RESEARCH
SCOTT ANALYST 3000 5 RESEARCH
SCOTT ANALYST 3000 4 RESEARCH
SCOTT ANALYST 3000 3 RESEARCH
SCOTT ANALYST 3000 2 RESEARCH
SCOTT ANALYST 3000 1 RESEARCH
JONES MANAGER 2975 5 RESEARCH
JONES MANAGER 2975 4 RESEARCH
JONES MANAGER 2975 3 RESEARCH
JONES MANAGER 2975 2 RESEARCH
JONES MANAGER 2975 1 RESEARCH
BLAKE MANAGER 2850 5 SALES
BLAKE MANAGER 2850 4 SALES
BLAKE MANAGER 2850 3 SALES
BLAKE MANAGER 2850 2 SALES
BLAKE MANAGER 2850 1 SALES
CLARK MANAGER 2450 1 ACCOUNTING
CLARK MANAGER 2450 2 ACCOUNTING
CLARK MANAGER 2450 3 ACCOUNTING
CLARK MANAGER 2450 4 ACCOUNTING
CLARK MANAGER 2450 5 ACCOUNTING
ALLEN SALESMAN 1600 5 SALES
ALLEN SALESMAN 1600 4 SALES
ALLEN SALESMAN 1600 3 SALES
ALLEN SALESMAN 1600 2 SALES
ALLEN SALESMAN 1600 1 SALES
TURNER SALESMAN 1500 5 SALES
TURNER SALESMAN 1500 4 SALES
TURNER SALESMAN 1500 3 SALES
TURNER SALESMAN 1500 2 SALES
TURNER SALESMAN 1500 1 SALES
MARTIN SALESMAN 1250 5 SALES
MARTIN SALESMAN 1250 4 SALES
MARTIN SALESMAN 1250 3 SALES
MARTIN SALESMAN 1250 2 SALES
MARTIN SALESMAN 1250 1 SALES
WARD SALESMAN 1250 5 SALES
WARD SALESMAN 1250 4 SALES
WARD SALESMAN 1250 3 SALES
WARD SALESMAN 1250 2 SALES
WARD SALESMAN 1250 1 SALES
50 rows selected.
I've narrowed the problem down to GRADE, which is in the salgrade table.
desc salgrade;
Name Null? Type
------------------------------- -------- ----
GRADE NUMBER (grade assigned to a
salary range: 1 to 5)
LOSAL NUMBER (Low salary range in
the grade)
HISAL NUMBER (High salary range in
the grade)
There is no common field in salgrade and the other tables.. The only common
field is deptno in the emp and dept tables.
If I remove any reference to the GRADE field, and insert DISTINCT, it
works.. I get:
SQL> select distinct ename, job, sal, dname from emp, salgrade, dept where
emp.job != 'CLERK' and emp.deptno = dept.deptno order by sal desc;
ENAME JOB SAL DNAME
---------- --------- ---------- --------------
KING PRESIDENT 5000 ACCOUNTING
FORD ANALYST 3000 RESEARCH
SCOTT ANALYST 3000 RESEARCH
JONES MANAGER 2975 RESEARCH
BLAKE MANAGER 2850 SALES
CLARK MANAGER 2450 ACCOUNTING
ALLEN SALESMAN 1600 SALES
TURNER SALESMAN 1500 SALES
MARTIN SALESMAN 1250 SALES
WARD SALESMAN 1250 SALES
So I know the problem is with grade.. I guess I just don't understand why
grade is making each record print out 5 times. I need to print the
particular grade each person's salary range is in, and I guess I don't know
how to do that.
For the heck of it, here are the salary ranges from the salgrade table:
GRADE LOSAL HISAL
---------- ---------- ----------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
This problem may seem basic.. I can see that I haven't managed to limit the
number of instances of GRADE that the query generates. Any and all help
would be much appreciated! I've been hours and hours at this..
Thank you!
Bill Boxall
bboxall(at)landover(dot)net
From | Date | Subject | |
---|---|---|---|
Next Message | Peter Eisentraut | 2001-04-13 11:19:54 | Re: Address already in use? |
Previous Message | Justin Clift | 2001-04-13 09:20:27 | New look for the techdocs website... is it alright? |