Mysql 練習(牛客網)

jialun0116發表於2020-11-02

Mysql 練習

  1. 查詢最晚入職員工的所有資訊
select * from employees where hire_date = (select max(hire_date) from employees);
  1. 查詢入職員工時間排名倒數第三的員工的所有資訊
-- order by 排序
-- desc 從大到小
-- limit 2,1 從第二條開始讀,讀一條
select * from employees order by hire_date desc limit 2,1;
  1. 查詢各個部門當前(dept_manager.to_date=‘9999-01-01’),領導當前(salaries.to_date=‘9999-01-01’)薪水詳情以及其對應部門編號dept_no
select salaries.*, dept_manager.dept_no from 
salaries,dept_manager on salaries.emp_no = dept_manager.emp_no 
where salaries.to_date='9999-01-01' and dept_manager.to_date='9999-01-01';
  1. 查詢所有已經分配部門的員工的last_name和first_name以及dept_no
select employees.last_name,employees.first_name,dept_emp.dept_no
from employees 
join dept_emp on dept_emp.emp_no = employees.emp_no;
  1. 查詢所有員工的last_name和first_name以及對應部門編號dept_no,也包括暫時沒有分配具體部門的員工
-- left join 左連線
select employees.last_name,employees.first_name,dept_emp.dept_no
from employees 
left join dept_emp on dept_emp.emp_no = employees.emp_no;
  1. 查詢所有員工入職時候的薪水情況,給出emp_no以及salary, 並按照emp_no進行逆序(請注意,一個員工可能有多次漲薪的情況)
-- desc 逆序
select employees.emp_no,salaries.salary
from employees left join salaries
where employees.emp_no = salaries.emp_no
and employees.hire_date = salaries.from_date
order by employees.emp_no desc;
  1. 查詢薪水變動超過15次的員工號emp_no以及其對應的變動次數t
-- 按照員工號分類 
select emp_no,count(from_date) as t 
from salaries
group by emp_no 
having t > 15;
  1. 找出所有員工當前(to_date=‘9999-01-01’)具體的薪水salary情況,對於相同的薪水只顯示一次,並按照逆序顯示
select salary from salaries
where to_date = '9999-01-01'
group by salary 
order by salary desc;
  1. 獲取所有部門當前(dept_manager.to_date=‘9999-01-01’)manager的當前(salaries.to_date=‘9999-01-01’)薪水情況,給出dept_no, emp_no以及salary(請注意,同一個人可能有多條薪水情況記錄)
select dept_manager.dept_no,dept_manager.emp_no,salaries.salary
from dept_manager 
left join salaries on dept_manager.emp_no = salaries.emp_no
where salaries.to_date = '9999-01-01'
and dept_manager.to_date = '9999-01-01'
  1. 獲取所有非manager的員工emp_no
select emp_no from employees 
where emp_no not in 
(select emp_no from dept_manager)

相關文章