SQL牛客網刷題(二) 4 19 20題解析

2021-10-08 07:43:42 字數 3442 閱讀 8938

【sql4. 查詢所有已經分配部門的員工的last_name和first_name以及dept_no】

題目描述:

查詢所有已經分配部門的員工的last_name和first_name以及dept_no(請注意輸出描述裡各個列的前後順序)

create table `dept_emp` (

`emp_no` int(11) not null,

`dept_no` char(4) not null,

`from_date` date not null,

`to_date` date not null,

primary key (`emp_no`,`dept_no`));

create table `employees` (

`emp_no` int(11) not null,

`birth_date` date not null,

`first_name` varchar(14) not null,

`last_name` varchar(16) not null,

`gender` char(1) not null,

`hire_date` date not null,

primary key (`emp_no`));

**:

select last_name, first_name, dept_no

from dept_emp as d join employees as e

on d.emp_no = e.emp_no

order by d.emp_no;

思路:

這是一道「中等」難度的題目,這題其實較sql3還要簡單一些,思路就是在dept_emp表中的員工肯定是分配了部分的員工,因此只需建立乙個內連線,就可以得到想要的結果。

【sql19. 查詢所有員工的last_name和first_name以及對應的dept_name】

題目描述:

查詢所有員工的last_name和first_name以及對應的dept_name,也包括暫時沒有分配部門的員工

create table `departments` (

`dept_no` char(4) not null,

`dept_name` varchar(40) not null,

primary key (`dept_no`));

create table `dept_emp` (

`emp_no` int(11) not null,

`dept_no` char(4) not null,

`from_date` date not null,

`to_date` date not null,

primary key (`emp_no`,`dept_no`));

create table `employees` (

`emp_no` int(11) not null,

`birth_date` date not null,

`first_name` varchar(14) not null,

`last_name` varchar(16) not null,

`gender` char(1) not null,

`hire_date` date not null,

primary key (`emp_no`));

**:

select last_name, first_name, dept_name

from employees as e left join dept_emp as d

on e.emp_no = d.emp_no

left join departments as d1 on d.dept_no = d1.dept_no

order by e.emp_no;

思路:

這道題是道「較難」難度的題目,需要對三張表進行關聯。

首先,將employees和dept_emp關聯,和sql5思路一樣,只是由於要求查詢出沒有分配部分的員工,所以用left join:

employees as e left join dept_emp as d

on e.emp_no = d.emp_no

其次,在將dept_emp和departments做一次關聯,其實這道題目這部分用join也能得到一樣的結果,但是這套題會報錯,還是老老實實用left join吧:

dept_emp as d left join departments as d1 on d.dept_no = d1.dept_no
【sql20. 查詢員工編號emp_no為10001其自入職以來的薪水salary漲幅】

題目描述:

查詢員工編號emp_no為10001其自入職以來的薪水salary漲幅(總共漲了多少)growth(可能有多次漲薪,沒有降薪)

create table `salaries` (

`emp_no` int(11) not null,

`salary` int(11) not null,

`from_date` date not null,

`to_date` date not null,

primary key (`emp_no`,`from_date`));

**1:

select (max(salary)-min(salary)) as growth

from salaries

where emp_no = 10001;

思路1:

這道題難度是「較難」,**1的思路是個很討巧的方式,直接求出10001號員工最大薪資和最小薪資的差值,對於這道題,員工沒有被降薪過,所以可以依靠這種方法通過題目,但是如果員工最後一次是被降薪的,顯然就有問題了,因此我推薦下面的方法:

**2:

select (

(select salary from salaries

where emp_no = 10001

order by to_date desc limit 1)-

(select salary from salaries

where emp_no = 10001

order by from_date limit 1)

)as growth;

思路2:

這條**寫出來不難理解,有點類似sql2和3的思路,用limit語句篩選最後的日期和員工入職的日期分別對應的薪資差值,我一開始卡在了在as growth後面加了from salaries導致一直不能通過。

以上,歡迎各位指正。

牛客網SQL刷題41 50

create table if not exists titles test id int 11 not null primary key,emp no int 11 not null,title varchar 50 not null,from date date not null,to date...

牛客網刷題 SQL篇

牛客網sql刷題日記 day 1 查詢最晚入職員工的資訊 題目描述 有乙個員工employees表簡況如下 建表語句如下 create tableemployees emp noint 11 not null,birth datedate not null,first namevarchar 14 ...

牛客網刷題

時間限制 c c 1秒,其他語言2秒 空間限制 c c 262144k,其他語言524288k 64bit io format lld 立華奏在學習初中數學的時候遇到了這樣一道大水題 設箱子內有 n 個球,其中給 m 個球打上標記,設一次摸球摸到每乙個球的概率均等,求一次摸球摸到打標記的球的概率 e...