Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 1st, 2007, 12:58 PM
Authorized User
 
Join Date: Jan 2007
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default Do i have to use a subquery, if yes then how?

I have an emp table in scott schema and i have to find out the names of employees whose annual salary is lesser than the average salary in their department.
The structure of the table is as follows:

EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)


__________________
MAXOOD!

Life is an endless journey towards perfection
 
Old January 2nd, 2007, 12:25 PM
Authorized User
 
Join Date: Oct 2005
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try the following query:

SELECT * FROM scott.emp emp INNER JOIN
(SELECT DEPTNO, AVG(SAL) AS AvgSal FROM scott.emp GROUP BY DEPTNO) sal
ON emp.DEPTNO = sal.DEPTNO
WHERE emp.SAL < sal.AvgSal

SQL Server Helper
http://www.sql-server-helper.com

Got a SQL Server Question? Ask us here: http://www.sql-server-helper.com/forums/default.asp
 
Old January 2nd, 2007, 02:22 PM
Authorized User
 
Join Date: Jan 2007
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default

It is the average annual salary and the average salary for which we have to do the comparision.Your query has given me a lot of help by the way.I am further looking in to it.
Can you just modify it to retrieve employees who have annual salaries lesser than the average annual salaries of their departmnents.

The comparision has to be between the following 2 queries:

select sal*12
from emp;

select avg(sal*12)
from emp
group by deptno;

Please help!





Similar Threads
Thread Thread Starter Forum Replies Last Post
trouble with subquery ghall202 Access 1 July 9th, 2008 01:55 PM
Subquery debbiecoates SQL Server 2000 4 June 25th, 2008 03:49 AM
Subquery as table brettk_1 Oracle 1 January 22nd, 2007 02:10 PM
Help...subquery problem Twistdmojo Classic ASP Databases 4 September 14th, 2006 11:45 AM
subquery khansa MySQL 1 February 21st, 2006 02:49 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.