3

我是 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
);

查找为第一银行工作并住在奥马哈的所有经理的薪水。

这是我对这个我认为是正确的,但我不想回答这个问题,直到我也能得到第二个......

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

查找所有员工中薪水最高的员工的姓名和公司名称。

4

3 回答 3

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;
于 2012-09-11T03:54:24.300 回答
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';

那应该这样做。

于 2012-09-11T03:38:20.827 回答
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
于 2012-09-11T20:38:02.557 回答