牛客網 資料庫SQL實戰3

2021-10-16 17:13:40 字數 1308 閱讀 5772

查詢各個部門當前(dept_manager.to_date=『9999-01-01』)領導當前(salaries.to_date=『9999-01-01』)薪水詳情以及其對應部門編號dept_no

(注:輸出結果以salaries.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`))

;create

table

`dept_manager`

(`dept_no`

char(4

)not

null

,-- '部門編號'

`emp_no`

int(11)

notnull

,-- '員工編號'

`to_date`

date

notnull

,primary

key(

`emp_no`

,`dept_no`))

;

1.首先聯接兩個表

2.用where子句篩選出dept_manager.to_date='9999-01-01和salaries.to_date='9999-01-01』的表項

3.將where篩選得到的表項進行排序(order by)

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

from salaries as s join dept_manager as d

on s.emp_no = d.emp_no -- 聯接兩個表

where d.to_date =

'9999-01-01'

-- 篩選出to_date 為 '9999-01-01' 的表項

and s.to_date =

'9999-01-01'

order

by s.emp_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...