天天看點

牛客網資料庫SQL實戰(1-5)

1、查找最晚入職員工的所有資訊

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`)); 

牛客網資料庫SQL實戰(1-5)
select * from employees where hire_date=(select max(hire_date) from employees);
select * from employees order by hire_date desc limit 1;      

讨論:https://www.nowcoder.com/questionTerminal/218ae58dfdcd4af195fff264e062138f

2、查找入職員工時間排名倒數第三的員工所有資訊

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`));

牛客網資料庫SQL實戰(1-5)
select * from employees order by hire_date desc limit 2,1;
select * from employees where hire_date = (select hire_date from employees order by hire_date desc limit 2,1);      

讨論:https://www.nowcoder.com/questionTerminal/ec1ca44c62c14ceb990c3c40def1ec6c

3、查找各個部門目前(to_date=\'9999-01-01\')上司目前薪水詳情以及其對應部門編号dept_no

CREATE TABLE `dept_manager` (

`dept_no` char(4) NOT NULL,

`emp_no` int(11) NOT NULL,

`from_date` date NOT NULL,

`to_date` date NOT NULL,

PRIMARY KEY (`emp_no`,`dept_no`));

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`));

牛客網資料庫SQL實戰(1-5)
select s.* ,d.dept_no
from salaries as s 
join dept_manager as d 
on s.emp_no = d.emp_no
where s.to_date = \'9999-01-01\'
and d.to_date=\'9999-01-01\';      

讨論:https://www.nowcoder.com/questionTerminal/c63c5b54d86e4c6d880e4834bfd70c3b

4、查找所有已經配置設定部門的員工的last_name和first_name

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`));

牛客網資料庫SQL實戰(1-5)
select e.last_name , e.first_name , d.dept_no
from employees e , dept_emp d
where e.emp_no = d.emp_no;

SELECT ep.last_name, ep.first_name, dp.dept_no 
FROM employees ep 
INNER JOIN dept_emp dp
ON ep.emp_no = dp.emp_no;      

讨論:https://www.nowcoder.com/questionTerminal/6d35b1cd593545ab985a68cd86f28671

5、查找所有員工的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`));

牛客網資料庫SQL實戰(1-5)
SELECT e.last_name, e.first_name, d.dept_no 
FROM employees e 
LEFT JOIN dept_emp d
ON e.emp_no = d.emp_no;      

讨論:https://www.nowcoder.com/questionTerminal/dbfafafb2ee2482aa390645abd4463bf

注意:

  INNER JOIN 兩邊表同時有對應的資料,即任何一邊缺失資料就不顯示。

  LEFT JOIN 會讀取左邊資料表的全部資料,即便右邊表無對應資料。

  RIGHT JOIN 會讀取右邊資料表的全部資料,即便左邊表無對應資料。

牛客網資料庫SQL實戰(1-5)