0
嗨,我已經寫了Derby數據庫的SQL查詢,其獲取具有最高工資SQL查詢:具有最高工資從每家公司選擇員工,
這些都是表結構
create table Company_Tbl_One(
company_Id int primary key,
company_name varchar(100)
)
create table Employee_Tbl_One(
employee_Id int primary key,
employee_name varchar(100),
company int references Company_Tbl_One
)
alter table Employee_Tbl_One add salary int
insert into Company_Tbl_One values(12,'Facebook Inc');
insert into Company_Tbl_One values(11,'Google Inc');
insert into Company_Tbl_One values(10,'Yahoo Inc');
insert into Company_Tbl_One values(14,'AOL Inc');
insert into EMPLOYEE_TBL_ONE(employee_Id,employee_name,company,salary) values(1, 'Tom Jackson',12,1000);
insert into EMPLOYEE_TBL_ONE(employee_Id,employee_name,company,salary) values(2, 'Jimmy John',12,200);
insert into EMPLOYEE_TBL_ONE(employee_Id,employee_name,company,salary) values(3, 'Samual Jackson',11,2000);
insert into EMPLOYEE_TBL_ONE(employee_Id,employee_name,company,salary) values(4, 'Sam Raime',10,3000);
insert into EMPLOYEE_TBL_ONE(employee_Id,employee_name,company,salary) values(5, 'Tidy Mann',14,5000);
insert into EMPLOYEE_TBL_ONE(employee_Id,employee_name,company,salary) values(6, 'Oliver Stone',14,5300);
insert into EMPLOYEE_TBL_ONE(employee_Id,employee_name,company,salary) values(7, 'Falcon Gonzalies',10,3300);
insert into EMPLOYEE_TBL_ONE(employee_Id,employee_name,company,salary) values(8, 'Ister Jack',11,2300);
從每家公司的員工
的SQL查詢如下
select e.employee_name, c.company_name
from EMPLOYEE_TBL_ONE e,
COMPANY_TBL_ONE c,
(select max(salary) as SALARY,
company as COMPANY_ID
from EMPLOYEE_TBL_ONE group by COMPANY) x
where c.company_Id = x.COMPANY_ID
and e.salary = x.SALARY
and e.company = c.company_Id
現在上面的SQL查詢工作正常,並獲取我的結果,
但是,有沒有其他方式來編寫SQL查詢相同的問題陳述?
不相關但是:你應該開始在FROM子句中使用顯式的'JOIN'而不是在where子句中的隱式連接。關於你的問題:由於德比缺乏窗口功能,我沒有看到另一種方法來實現你想要的。 – 2014-09-22 12:39:34
爲什麼你需要多種方式來編寫你的查詢?是由於某種原因你認定的不合適嗎? – 2014-09-22 14:14:04