資料庫SQL實戰練習1
題目描述
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
));
SELECT *
FROM employees
ORDER BY hire_date desc limit 1;
2、查詢入職員工時間排名倒數第三的員工所有資訊(表同上)
SELECT *
FROM employees
ORDER BY hire_date desc limit 2,1;
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
salary
int(11) NOT NULL,from_date
));
select s.*,d.dept_no from salaries s,dept_manager d where d.to_date = '9999-01-01' and s.to_date = '9999-01-01' and d.emp_no = s.emp_no; /* SELECT s.*,d.dept_no FROM salaries s,dept_manager d WHERE s.to_date='9999-01-01' AND d.to_date='9999-01-01' AND s.emp_no = d.emp_no; */
4、查詢所有已經分配部門的員工的last_name和first_name
CREATE TABLE dept_emp
(
emp_no
int(11) NOT NULL,
dept_no
char(4) NOT NULL,dept_no
));
CREATE TABLE employees
(
emp_no
int(11) NOT NULL,
PRIMARY KEY (emp_no
));
SELECT last_name,first_name,dept_no
FROM dept_emp d,employees e
WHERE d.emp_no = e.emp_no
5、查詢所有員工的last_name和first_name以及對應部門編號dept_no,也包括展示沒有分配具體部門的員工
CREATE TABLE dept_emp
(
emp_no
int(11) NOT NULL,
PRIMARY KEY (emp_no
));
SELECT e.last_name,e.first_name,d.dept_no
FROM employees e left join dept_emp d on d.emp_no = e.emp_no;
6、查詢所有員工入職時候的薪水情況,給出emp_no以及salary, 並按照emp_no進行逆序
CREATE TABLE employees
(
emp_no
int(11) NOT NULL,
PRIMARY KEY (emp_no
));
CREATE TABLE salaries
(
emp_no
int(11) NOT NULL,from_date
));
SELECT e.emp_no,s.salary
FROM employees e,salaries s
WHERE e.emp_no = s.emp_no
AND e.hire_date = s.from_date
ORDER BY e.emp_no desc;
7、查詢薪水漲幅超過15次的員工號emp_no以及其對應的漲幅次數t
CREATE TABLE salaries
(
emp_no
int(11) NOT NULL,from_date
));
SELECT emp_no,count(salary) as t
FROM salaries
GRIUP BY emp_no
HAVING t>15;