我是SQL新手,本学期我正在上DB课程。我有大约20个查询尝试这个数据库,其中有两个我无法弄清楚...我想知道这里有人能帮忙!
这是我已经创建的表格。
create table employee(
EID varchar(20) primary key,
Lastname varchar(20),
Firstname varchar(20),
Midinitial char(1),
gender char(1),
street varchar(20),
city varchar(20)
);
create table works(
EID varchar(20) primary key,
Lastname varchar(20),
Firstname varchar(20),
Midinitial char(1),
company_name varchar(20),
salary numeric(5,0),
foreign key(EID) references employee,
foreign key(company_name) references company
);
create table company(
company_name varchar(20) primary key,
city varchar(20)
);
create table manages(
EID varchar(20) primary key,
Lastname varchar(20),
Firstname varchar(20),
Midinitial char(1),
ManagerLastname varchar(20),
MFirstname varchar(20),
MMidinitial varchar(20),
start_date date,
foreign key (EID) references employee
);
查找为First Bank工作并住在奥马哈的所有经理的工资。
以下是我对这一点的看法,我认为这是正确的但我不想回答这个问题,直到我能得到第二个......
select distinct salary
from employee, works, manages
where employee.city = 'Omaha'
and works.company_name = 'First Bank'
and manages.managerlastname = employee.Lastname
and employee.EID = works.EID
和
查找所有员工薪水最高的员工的姓名和公司名称。
答案 0 :(得分:2)
我很确定它应该是:
SELECT w.salary FROM works w INNER JOIN employee e ON w.EID = e.EID
WHERE e.city = 'Omaha' AND w.company_name = 'First Bank' AND EXISTS
( SELECT * FROM managers m WHERE m.EID = w.EID);
第二个:
SELECT e.Firstname, e.Lastname, w.company_name FROM employee e INNER JOIN
works w ON e.EID = w.EID ORDER BY w.salary DESC LIMIT 1;
答案 1 :(得分:1)
SELECT w.salary
FROM works w
INNER JOIN employee e
ON e.EID = w.EID
AND e.city = 'Omaha'
WHERE w.company_name = 'First Bank';
应该这样做。
答案 2 :(得分:0)
这两个都从我表中的信息中返回了我需要的内容。谢谢你的帮助!
查询1
select distinct salary
from employee, works, manages
where employee.city = 'Omaha'
and works.company_name = 'First Bank'
and manages.managerlastname = employee.Lastname
and employee.EID = works.EID
查询2
select firstname, lastname, company_name
from works w1, (select max(salary) as max_salary
from works) w2
where w1.salary = w2.max_salary