牛客網 資料庫SQL實戰12

2021-10-17 01:55:57 字數 2643 閱讀 2559

獲取所有部門中當前(dept_emp.to_date = 『9999-01-01』)員工當前(salaries.to_date=『9999-01-01』)薪水最高的相關資訊,給出dept_no, emp_no以及其對應的salary,按照部門編號公升序排列。

create

table

`dept_emp`

(`emp_no`

int(11)

notnull

,`dept_no`

char(4

)not

null

,`from_date`

date

notnull

,`to_date`

date

notnull

,primary

key(

`emp_no`

,`dept_no`))

;create

table

`salaries`

(`emp_no`

int(11)

notnull

,`salary`

int(11)

notnull

,`from_date`

date

notnull

,`to_date`

date

notnull

,primary

key(

`emp_no`

,`from_date`))

;如插入:

insert

into dept_emp values

(10001

,'d001'

,'1986-06-26'

,'9999-01-01');

insert

into dept_emp values

(10002

,'d001'

,'1996-08-03'

,'9999-01-01');

insert

into dept_emp values

(10003

,'d001'

,'1996-08-03'

,'1997-08-03');

insert

into salaries values

(10001

,90000

,'1986-06-26'

,'1987-06-26');

insert

into salaries values

(10001

,88958

,'2002-06-22'

,'9999-01-01');

insert

into salaries values

(10002

,72527

,'1996-08-03'

,'1997-08-03');

insert

into salaries values

(10002

,72527

,'2000-08-02'

,'2001-08-02');

insert

into salaries values

(10002

,72527

,'2001-08-02'

,'9999-01-01');

insert

into salaries values

(10003

,90000

,'1996-08-03'

,'1997-08-03'

);

則輸出

1.首先我們用乙個子查詢先篩選出滿足dept_emp.to_date = '9999-01-01』和salaries.to_date='9999-01-01』這兩個條件的表項中各個部門員工的最高工資

2.再檢索所有表項,找出工資等於我們在上一步中子查詢返回的工資的員工表項,最後按題目要求輸出

select d.dept_no, d.emp_no, s.salary

from dept_emp as d inner

join salaries as s

on d.emp_no = s.emp_no

and d.to_date =

'9999-01-01'

and s.to_date =

'9999-01-01'

where s.salary =

(select

max(s1.salary)

from dept_emp as d1 inner

join salaries as s1

on d1.emp_no = s1.emp_no

and d1.to_date =

'9999-01-01'

and s1.to_date =

'9999-01-01'

where d1.dept_no = d.dept_no

group

by d1.dept_no)

order

by d.dept_no;

牛客網資料開發題庫 牛客網資料庫SQL實戰(1)

查詢最晚入職員工的所有資訊 入門 需要查詢最晚入職員工的資訊,即查詢hire date最大的資料,使用倒序並取第乙個人即可。select from employees order by hire date desc limit 0,1 desc 使用order by時在後面加上desc表示倒序,即從...

牛客網 資料庫SQL實戰36 40

36.對於如下表actor,其對應的資料為 actor id first name last name last update 1penelope guiness 2006 02 15 12 34 33 2nick wahlberg 2006 02 15 12 34 33 建立乙個actor nam...

牛客網 資料庫SQL實戰1

題目描述 查詢最晚入職員工的所有資訊,為了減輕入門難度,目前所有的資料裡員工入職的日期都不是同一天 sqlite裡面的注釋為 mysql為comment create table employees emp no int 11 notnull 員工編號 birth date date notnull...