資料庫SQL實戰

2021-08-06 06:07:34 字數 1417 閱讀 1547

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

輸入描述:

輸出描述:

last_name

first_name

dept_name

facello

georgi

marketing

省略省略

省略sluis

mary

null

select e.last_name, e.first_name, da.dept_name

from (employees e left join dept_emp de on e.emp_no = de.emp_no)

left join departments da on de.dept_no = da.dept_no

思路:運用兩次left join連線巢狀

1、第一次left join連線employees表與dept_emp表,得到所有員工的last_name和first_name以及對應的dept_no,也包括暫時沒有分配部門的員工

2、第二次left join連線上表與departments表,即連線dept_no與dept_name,得到所有員工的last_name和first_name以及對應的dept_name,也包括暫時沒有分配部門的員工

資料庫SQL實戰

無emp no birth date first name last name gender hire date 10008 1958 02 19 saniya kalloufi m1994 09 15 示例1無 無 select from employeesorder byhire datedes...

資料庫SQL實戰

找出所有員工當前 to date 9999 01 01 具體的薪水salary情況,對於相同的薪水只顯示一次,並按照逆序顯示 create table salaries emp no int 11 not null,salary int 11 not null,from date date not ...

資料庫SQL實戰

獲取當前 to date 9999 01 01 薪水第二多的員工的emp no以及其對應的薪水salary create table salaries emp no int 11 not null,salary int 11 not null,from date date not null,to d...