You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何完善SQL查询Research部门薪资超3万员工的配偶姓名?

需求与现有代码

需求:列出所有在‘Research’部门工作且薪资高于30000美元的员工姓名、薪资及配偶姓名。

现有SQL代码:

select fname, lname, dname, salary 
from employee, department 
where dname='Research' 
and dnumber=dno 
and salary >30000;
数据表结构与测试数据
CREATE TABLE DEPT_LOCATIONS 
    (DNUMBER INT NOT NULL , 
     DLOCATION VARCHAR(15) NOT NULL , 
     PRIMARY KEY (DNUMBER, DLOCATION)) ;
 
Insert into dept_locations values('1', 'Houston');
Insert into dept_locations values('4', 'Stafford');
Insert into dept_locations values('5',  'Bellaire');
Insert into dept_locations values('5', 'Sugarland');
Insert into dept_locations values('5', 'Houston');
 
CREATE TABLE DEPARTMENT 
   (DNAME VARCHAR(15) NOT NULL , 
   DNUMBER INT NOT NULL , 
   MGRSSN CHAR(9) NOT NULL , 
   MGRSTARTDATE DATE , 
   PRIMARY KEY (DNUMBER) , 
   UNIQUE (DNAME) );

Insert into department values('Research','5', '333445555', to_date('05/22/1988', 'mm/dd/yyyy'));
Insert into department values('Administration','4','987654321',to_date('01/01/1995',  
'mm/dd/yyyy'));
Insert into department values('Headquarters','1','888665555',to_date('06/19/1981', 'mm/dd/yyyy'));
 
CREATE TABLE EMPLOYEE 
(FNAME CHAR(15) NOT NULL ,
MINIT CHAR, 
LNAME VARCHAR(15) NOT NULL , 
SSN CHAR(9) NOT NULL , 
BDATE DATE,
ADDRESS VARCHAR(30) , 
SEX CHAR, 
SALARY DECIMAL(10,2) , 
SUPERSSN CHAR(9) , 
DNO INT NOT NULL , 
PRIMARY KEY (SSN) ,
FOREIGN KEY (DNO) REFERENCES DEPARTMENT(DNUMBER) ) ; 
 
-- FOREIGN KEY (SUPERSSN) REFERENCES EMPLOYEE(SSN) ,
 
Insert into employee values('John','B','Smith','123456789', to_date('01/09/1955','mm/dd/yyyy'), '731 Fondren, Houston, TX','M',  30000.00, '333445555', '5');
Insert into employee values ('Franklin','T','Wong','333445555', to_date('12/08/1945', 'mm/dd/yyyy'), '638 Voss, Houston, TX','M', 40000.00, '888665555','5');
Insert into employee values ('Alicia','J','Zeleya','999887777',to_date('07/19/1958','mm/dd/yyyy'),'3321 Castle, Spring, TX','F', 25000.00,'987654321','4');
Insert into employee values('Jennifer','S','Wallace','987654321', to_date('06/20/1931' , 'mm/dd/yyyy'),'291 Berry, Bellaire, TX','F',43000.00,'888665555','4');
Insert into employee values('Ramesh','K','Narayan','666884444', to_date('09/15/1952', 'mm/dd/yyyy'),'975 Fire Oak, Humble, TX','M', 38000.00,'333445555','5');
Insert into employee values('Joyce','A','English','453453453', to_date('07/31/1962', 'mm/dd/yyyy'),'5631 Rice, Houston, TX','F', 25000.00,'333445555','5');
Insert into employee values('Ahmad','V','Jabbar ','987987987', to_date('03/29/1959', 'mm/dd/yyyy'),'980 Dallas, Houston, TX', 'M',25000.00,'987654321','4');
Insert into employee values('James','E','Borg','888665555', to_date('11/10/1929', 'mm/dd/yyyy'),'450 Stone, Houston, TX', 'M','55000.00',null,'1');

Create table project
(Pname char(15) not null, 
pnumber int  primary key,
plocation char(15),
dnum int references department(dnumber));
        
insert into project values('ProductX','1','Bellaire','5');
insert into project values('ProductY','2 ','Sugarland ','5');
insert into project values('ProductZ','3','Houston','5');
insert into project values('Computerization','10','Stafford','4');
insert into project values('Reorganization','20','Houston','1');
insert into project values('Newbenefits','30','Stafford','4');

create table WORKS_ON
(essn char(9),
 pno  number references project(pnumber),
 hours decimal(5,2),
 primary key (essn, pno));

insert into works_on values('123456789','1',32.50);
insert into works_on values( '123456789','2',7.50);
insert into works_on values( '666884444','3',40.00);
insert into works_on values( '453453453','1',20.00);
insert into works_on values( '453453453','2 ',20.00);
insert into works_on values( '333445555','2 ',10.00);
insert into works_on values( '333445555','3 ',10.00);
insert into works_on values( '333445555','10 ',10.00);
insert into works_on values( '333445555','20 ',10.00);
insert into works_on values( '999887777','30 ',30.00);
insert into works_on values( '999887777','10 ',10.00);
insert into works_on values( '987987987','10 ',35.00);
insert into works_on values( '987987987','30 ',5.00);
insert into works_on values( '987654321','30 ',20.00);
insert into works_on values( '987654321','20 ',15.00);
insert into works_on values( '888665555','20 ',null);

create table dependent
(essn char(9) references employee(ssn),
 dependent_name char(10),
 sex char,
 bdate date,
 relationship char(10),
 primary key (essn, dependent_name));

insert into dependent values('333445555','Alice','F', 
to_date('04/05/1976','mm/dd/yyyy'),'Daughter');
insert into dependent  values('333445555','Theodore','M', to_date('10/25/1973', 
'mm/dd/yyyy'),'Son');
insert into dependent values('333445555','Joy','F', to_date('05/03/1948', 'mm/dd/yyyy'),'Spouse');
insert into dependent values('987654321','Abner','M', to_date('02/29/1932', 
'mm/dd/yyyy'),'Spouse');
insert into dependent values('123456789','Michael','M', to_date('01/01/1978', 
'mm/dd/yyyy'),'Son');
insert into dependent values('123456789','Alice','F', to_date('12/31/1978', 
'mm/dd/yyyy'),'Daughter');
insert into dependent values('123456789','Elizabeth','F', to_date('05/05/1957', 
'mm/dd/yyyy'),'Spouse');
commit;
完善后的SQL代码

要获取配偶姓名,需关联employee与dependent表,筛选关系为Spouse的记录,推荐使用显式连接语法提升可读性:

SELECT 
    e.fname, 
    e.lname, 
    e.salary, 
    d.dependent_name AS spouse_name
FROM 
    employee e
JOIN 
    department dept ON e.dno = dept.dnumber
LEFT JOIN 
    dependent d ON e.ssn = d.essn AND d.relationship = 'Spouse'
WHERE 
    dept.dname = 'Research' 
    AND e.salary > 30000;

说明

  • 用JOIN替代隐式连接,逻辑更清晰
  • LEFT JOIN确保无配偶信息的员工也能出现在结果中(此时spouse_name为NULL)
  • 关联时直接过滤relationship = 'Spouse',避免后续二次筛选

内容的提问来源于stack exchange,提问作者oneandonly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 15:45:45