Employee(person-name, age, street, city)
Work(person-name, company-name, salary)
Company(company-name, city)
Manage(person-name, manager-name)
我需要做的是为以下内容创建一个SQL查询:Write a query in SQL to nd the names of such companies that all of their employees have salaries higher than $100000
这就是我得到的:
Select company-name
From Company
Where (Select salary
From Work
Where Salary > 100000)
我有点困惑为什么这是错的。我还是初学者,所以如果有人可以指导我,那真的很感激
答案 0 :(得分:1)
这有点令人困惑,因为SQL中的一种方式是双重否定:
Select c.companyname
From Company c
Where not exists (select 1
from Work w
where w.companyname = c.companyname and w.salary < 100000
);
答案 1 :(得分:0)
在这里你必须连接两个表来获得这个结果,但如果你想使用子查询,你必须选择相同的列或参考列
即
Select company-name
From Company
Where company-name in (Select company-name From Work Where Salary > 100000)
使用join查询。
select
company-name,person-name,salary
from
Company c join work w using(company-name)
where
salary > 100000;
答案 2 :(得分:0)
select c.company-name,w.salary [higher than $100000]
from Company as c
inner join Work as w
on c.company-name = w.company-name
where w.salary > $100000;
答案 3 :(得分:0)
我认为您正在尝试在查询中执行此操作:
Select company-name
From Company
Where company-name in (Select company-name
From Work
Where Salary > 100000)
从我所看到的;这里使用IN子句毫无意义。你也可以这样做:
Select company-name
From Work
Where Salary > 100000
实际上,您需要IN子句或者INNER JOIN来获取公司名称,因为公司名称将存储在公司表中,而表(公司和工作)将使用密钥(主键和外键)连接因此,您可以从公司表中获取公司名称。
答案 4 :(得分:0)
问题中的查询问题是它的语法无效。你不能说
SELECT something
FROM some_table
WHERE (another query)
那是无效的。 WHERE子句中的元素必须是一系列逻辑条件,而不是查询。
我会做以下事情:
WITH COMPANY_MIN_SALARIES AS
(SELECT w.COMPANY_NAME, MIN(w.SALARY) AS MIN_SALARY
FROM WORK w
GROUP BY w.COMPANY_NAME)
SELECT ms.COMPANY_NAME
FROM COMPANY_MIN_SALARIES ms
WHERE ms.MIN_SALARY > 100000;
祝你好运。