创建子查询时遇到问题

trouble with creating subquery

我在使用子查询创建查询时遇到了一些问题。我必须使用子查询,因为这是学校的作业。

我有两张表,一张员工一张,一张部门一张。从这些表中,我必须 return 收入超过部门平均工资的员工。

这就是我目前所拥有的:

SELECT 
    ename,
    salary,
    (SELECT 
            department.depname
        FROM
            department
        WHERE
            department.depno = employee.depno) AS depname,
    (SELECT 
            AVG(salary)
        FROM
            employee
        WHERE
            employee.depno IN (SELECT 
                    department.depno
                FROM
                    department)) avgSalary
FROM
    employee
WHERE
    salary > (SELECT 
            AVG(salary)
        FROM
            employee
        WHERE
            employee.depno IN (SELECT 
                    department.depno
                FROM
                    department))

这个return秒:

'JONES', '2975.00', 'RESEARCH', '2073.214286'
'BLAKE', '2850.00', 'SALES', '2073.214286'
'CLARK', '2450.00', 'ACCOUNTING', '2073.214286'
'SCOTT', '3000.00', 'RESEARCH', '2073.214286'
'KING', '5000.00', 'ACCOUNTING', '2073.214286'
'FORD', '3000.00', 'RESEARCH', '2073.214286'

但平均工资 return 是所有工资的平均值。但我需要每个部门都这样做。

我知道如何通过执行以下查询来执行此操作:

SELECT 
    department.depname,
    (SELECT 
            AVG(salary)
        FROM
            employee
        WHERE
            department.depno = employee.depno) avgSalary
FROM
    department

其中 return 个:

ACCOUNTING  2916.666667
RESEARCH    2175.000000
SALES       1566.666667
OPERATIONS  

但我不知道如何组合它们,或者如果不使用 join 是否可行。

任何帮助将不胜感激

编辑:

Table: employee
Columns:
empno decimal(4,0) PK 
ename varchar(10) 
efunction varchar(10) 
boss decimal(4,0) 
employed date 
salary decimal(7,2) 
commission decimal(7,2) 
depno decimal(2,0)

Table: department
Columns:
depno decimal(2,0) PK 
depname varchar(14) 
location varchar(13)

有点像这样...?

mysql> select   *
    -> from     employee e
    -> where    e.employee_salary > (
    ->             select   avg(employee_salary) avg
    ->             from     employee ee
    ->             -- this is how you join them without a join...
    ->             where    ee.department_id=e.department_id
    ->          );
+-------------+---------------+---------------+-----------------+
| employee_id | department_id | employee_name | employee_salary |
+-------------+---------------+---------------+-----------------+
|           2 |             1 | employee 2    |              40 |
|           6 |             1 | employee 6    |              50 |
+-------------+---------------+---------------+-----------------+
2 rows in set (0.04 sec)

哪里....

mysql> select * from employee;
+-------------+---------------+---------------+-----------------+
| employee_id | department_id | employee_name | employee_salary |
+-------------+---------------+---------------+-----------------+
|           1 |             1 | employee 1    |              20 |
|           2 |             1 | employee 2    |              40 |
|           3 |             1 | employee 3    |              30 |
|           4 |             1 | employee 4    |              30 |
|           5 |             1 | employee 5    |              30 |
|           6 |             1 | employee 6    |              50 |
+-------------+---------------+---------------+-----------------+
6 rows in set (0.00 sec)

没有加入...

mysql> select   e.*,
    ->
    ->          (select  department_name
    ->           from    department d
    ->           where   d.department_id=e.department_id) as department_name,
    ->
    ->          (select   avg(employee_salary) avg
    ->             from     employee ee
    ->             where    ee.department_id=e.department_id) as department_average
    ->
    -> from     employee e
    ->
    -> where    e.employee_salary > (
    ->             select   avg(employee_salary) avg
    ->             from     employee ee
    ->             where    ee.department_id=e.department_id
    ->          );
+-------------+---------------+---------------+-----------------+-----------------+--------------------+
| employee_id | department_id | employee_name | employee_salary | department_name | department_average |
+-------------+---------------+---------------+-----------------+-----------------+--------------------+
|           2 |             1 | employee 2    |              40 | department 1    | 33.333333333333336 |
|           6 |             1 | employee 6    |              50 | department 1    | 33.333333333333336 |
+-------------+---------------+---------------+-----------------+-----------------+--------------------+
2 rows in set (0.00 sec)

你真的应该避免像 select 那样的连接,除非真的有必要,因为在现实生活中最好只使用连接(对于 efficiency/performance 和更大的数据集)。不过,where 中的子查询是完全合适的。