找到薪资最少的公司

时间:2013-03-22 10:36:10

标签: mysql sql sql-server

您好我有以下数据库scheema。

employee (employee-name, street, city)
works (employee-name, company-name, salary)
company (company-name, city)
manages (employee-name, manager-name)

我需要找到工资总额最小的公司。

我写了以下查询,但我的教练告诉我错了。

SELECT company-name
FROM works
GROUP BY company-name
HAVING sum(salary) < ( SELECT sum(salary)
FROM works
GROUP BY company-name)

3 个答案:

答案 0 :(得分:2)

您的子查询

SELECT company-name, sum(salary)
FROM works
GROUP BY company-name

将为您提供每家公司的累计工资(工资单) 如果您按升序订购这些数据,则第一条记录将是工资单最小的公司。

SELECT company-name, sum(salary)
FROM works
GROUP BY company-name
ORDER BY sum(salary)

您可以使用MySQL的LIMIT子句将该记录集限制为单个记录,使用

SELECT company-name, sum(salary)
FROM works
GROUP BY company-name
ORDER BY sum(salary)
LIMIT 1

答案 1 :(得分:1)

我会再给你一个提示。 <是错的(仔细考虑子查询返回的内容)。

答案 2 :(得分:0)

我们可以在这种情况下使用< All吗?

e.g。 :

SELECT company-name
FROM works
GROUP BY company-name
HAVING sum(salary) < ALL( SELECT sum(salary)
FROM works
GROUP BY company-name)