Switch to Linear ModeSwitch to Hybrid ModeSwitch to Threaded Mode
Printer Friendly View | Email this page | Register Now to enjoy user benefits! | Share on Share on Facebook
mjzsakae Registered User


Join Date: Feb 2006
Posts: 303
Trade rep: 21 (100%)
Thanked 1 Times in 1 Post
SQL how? mjzsakae Dec 11th, 08, 03:11 PM #1 (permalink)
Click the image to open in full size.

My qn is at the bottom of the pic..

Its from an exam paper.. Really no ideal how to write the SQL statement.. Pls help.. Thanks..

Additional:
1. Write a query to display the name, job and salary for all employees who work in the ACCOUNTING department.

My ans: (pls check for me.. thanks guys)

SELECT e.ename, e.job, sal
FROM emp e, dept d
WHERE d.deptno = e.deptno
AND d.deptno IN (10)

2. Write a query to display the employee number, name, salary for all employees who earn more than the average salary of the ACCOUNTING department.

My ans:

SELECT e.empno, e.ename, e.sal
FROM emp e
WHERE e.sal >
(
SELECT AVG(sal)
FROM emp e, dept d
WHERE e.deptno = d.deptno
)

 
Last edited by mjzsakae; Dec 11th, 08 at 07:55 PM..
icyheart
icyheart's Avatar
태니 ♥


Join Date: Sep 2007
Location: Singapore (East)
Posts: 908
Trade rep: 9 (100%)
Thanked 1 Times in 1 Post
icyheart Dec 11th, 08, 03:26 PM #2 (permalink)
SELECT e.name, d.loc
FROM emp e, dept d
WHERE e.deptno = d.deptno

no idea how to get managers' name. im still learning =p

Please click Thanks if I've helped you in anyway
 
bangbangbogi
bangbangbogi's Avatar
Registered User


Join Date: Jul 2008
Posts: 744
Trade rep: 5 (100%)
bangbangbogi Dec 11th, 08, 04:07 PM #3 (permalink)
u need to use a join statement
if(computer.fail==true){
background.setColor(blue);
user.frown();
sys.shutdown();
user.scream("OH, F#CK YOU");}
 
mjzsakae Registered User


Join Date: Feb 2006
Posts: 303
Trade rep: 21 (100%)
Thanked 1 Times in 1 Post
mjzsakae Dec 11th, 08, 06:50 PM #4 (permalink)
lOlx.. getting is the manager name is the problem i am facing too -.-"
the employee table is self referencing..

bangbangbogi.. please provide solution.. the query statements.. thanks.. =x
 
SaLvAtoOo
SaLvAtoOo's Avatar
Registered User


Join Date: Nov 2004
Location: Tampines
Posts: 1,402
Trade rep: 17 (100%)
Thanked 1 Times in 1 Post
SaLvAtoOo Dec 11th, 08, 07:07 PM #5 (permalink)
select e.ename, d.loc, e1.ename
from emp as e left outer join emp as e1 on e.empno = e1.mgr, dept d
where e.deptno = d.deptno

should b something like that..
u need outer join to include the president in the results...
 
mjzsakae Registered User


Join Date: Feb 2006
Posts: 303
Trade rep: 21 (100%)
Thanked 1 Times in 1 Post
mjzsakae Dec 11th, 08, 07:59 PM #6 (permalink)
Quote:
Originally Posted by SaLvAtoOo View Post
select e.ename, d.loc, e1.ename
from emp as e left outer join emp as e1 on e.empno = e1.mgr, dept d
where e.deptno = d.deptno ...
thanks salvatooo.. nice one but chim.. haha

i added 2 more qns with my own answers.. can please check for me? thanks.. shld be much more easier compared to the first qn.. haha
 
programmer Registered User


Join Date: May 2006
Posts: 4
Trade rep: 0 (0%)
programmer Dec 11th, 08, 08:44 PM #7 (permalink)
I did not check if my syntax is correct but I think the structure is right. For your reference...

my ans for q1)
SELECT E.ENAME, E.JOB, E.SAL
FROM emp E, dept D
WHERE E.DEPTNO = D.DEPTNO && D.DNAME = 'ACCOUNTING'


for q2)
SELECT e.empno, e.ename, e.sal
FROM emp e
WHERE e.sal >
(
SELECT AVG(sal)
FROM emp e, dept d
WHERE e.deptno = d.deptno && D.DNAME = 'ACCOUNTING'
)
 
mjzsakae Registered User


Join Date: Feb 2006
Posts: 303
Trade rep: 21 (100%)
Thanked 1 Times in 1 Post
mjzsakae Dec 11th, 08, 10:21 PM #8 (permalink)
programmer.. qn1 i tink both yr way and my way is applicable ba..
qn2 u r correct, i forgot to indicate AND d.deptno IN (10)

thanks.. =)
 
FanaticX
FanaticX's Avatar
Ruby Fanatic


Join Date: Oct 2007
Posts: 106
Trade rep: 0 (0%)
FanaticX Dec 15th, 08, 08:29 PM #9 (permalink)
hmm for question 1 i thought should be

SELECT emp.ENAME, emp.JOB, emp.SAL FROM emp WHERE emp.DEPTNO = 10

cause DEPTNO 10 is refering as ACCOUNTING as DNAME, so i assume its a foreign to primary key thing. Correct me if i'm wrong cause i not expert in SQL.
 
Last edited by FanaticX; Dec 15th, 08 at 08:34 PM..
Thread Tools Display Modes
Linear Mode Linear Mode
Featured Retailer
OCZ Fan Club!
OCZ Fan Club 35 OCZ Fans!
Kingston Fan Club!
Kingston Fan Club Join Now!