MySQL Forums
Forum List  »  MySQL Query Browser

SELECTING AVG FROM A TABLE QUESTION
Posted by: C R
Date: February 11, 2012 10:12AM

I'm a beginner to writing SQL queries and am currently trying to write a
"QUERY TO LIST EMPLOYEES WHO MAKE MORE THAN AVERAGE FROM "EMP" TABLE" in my test database. I've tried writing both queries below, but I'm not sure either is correct? Can someone please give me feedback on what I'm doing wrong?

mysql> DESC EMP;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| empno | smallint(6) | NO | PRI | 0 | |
| empname | varchar(25) | YES | | NULL | |
| empsalary | decimal(8,2) | YES | | NULL | |
| deptname | varchar(30) | YES | | NULL | |
| bossno | smallint(6) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+


1ST ATTEMPT:

select empname, AVG(empsalary) from emp WHERE empsalary in (Select empsalary from emp where empsalary > AVG(empsalary);


ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
mysql>




2ND ATTEMPT:

select empname, AVG(empsalary) from emp WHERE empsalary in (Select AVG(empsalary)

;+---------+----------------+
| empname | AVG(empsalary) |
+---------+----------------+
| NULL | NULL |
+---------+----------------+

Options: ReplyQuote


Subject
Written By
Posted
SELECTING AVG FROM A TABLE QUESTION
C R
February 11, 2012 10:12AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.