【MySql】牛客SQL刷題(上)


牛客SQL題目

題目鏈接:https://www.nowcoder.com/ta/sql

  1. 查找最晚入職員工的所有信息

    select * 
    from 
    employees
    where 
    hire_date = (select max(hire_date) from employees);
  2. 查找入職員工時間排名倒數第3的員工所有信息

    select * 
    from 
    employees 
    order by 
    hire_date 
    desc --降序
    limit 2,1;--從3項,偏移1項,即第3項
  3. 查找各個部門當前(to_date='9999-01-01')的領導的當前薪水詳情以及其對應部門編號dept_no

    select s.*,d.dept_no
    from salaries as s
    join dept_manager as d
    on d.emp_no=s.emp_no
    where s.to_date='9999-01-01'
    and d.to_date='9999-01-01';
  4. 查找所有已經分配部門的員工的last_name和first_name

    select e.last_name,e.first_name,d.dept_no
    from employees as e
    join dept_emp as d
    on e.emp_no = d.emp_no;
  5. 查找所有員工的last_name和first_name以及對應部門編號dept_no,也包括展示沒有分配具體部門的員工

    select e.last_name,e.first_name,d.dept_no
    from employees as e
    left join dept_emp as d--坐表為主,右表補充,為空補NULL
    on e.emp_no = d.emp_no;
  6. 查找所有員工入職時候的薪水情況,給出emp_no以及salary, 並按照emp_no進行逆序

    select e.emp_no,s.salary
    from salaries as s
    join employees as e
    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

    count( ):統計記錄的條數

    還需要group by emp_no將每個員工的記錄顯示在一條記錄中

    select emp_no,count(emp_no) as t
    from salaries
    group by
    emp_no
    having t > 15;
  8. 找出所有員工當前(to_date='9999-01-01')具體的薪水salary情況,對於相同的薪水只顯示一次,並按照逆序顯示

    distinct:去除重復

    select distinct salary-- 去除重復
    from salaries
    where to_date = '9999-01-01'
    order by
    salary
    desc;
  9. 獲取所有部門當前manager的當前薪水情況,給出dept_no, emp_no以及salary,當前表示to_date='9999-01-01'

    select d.dept_no,d.emp_no,s.salary
    from dept_manager as d
    join salaries as s
    on d.emp_no=s.emp_no
    where d.to_date='9999-01-01'
    and s.to_date='9999-01-01';
  10. 獲取所有非manager的員工emp_no

    select emp_no
    from employees
    where emp_no not in (select emp_no from dept_manager);
  11. 獲取所有員工當前的manager,如果當前的manager是自己的話結果不顯示,當前表示to_date='9999-01-01'。

    select e.emp_no,m.emp_no as manager_no
    from dept_emp as e
    join dept_manager as m
    on e.dept_no=m.dept_no
    where e.emp_no <> m.emp_no -- 當前manager是自己不顯示,<>不等於
    and e.to_date='9999-01-01'
    and m.to_date='9999-01-01';
  12. 獲取所有部門中當前員工薪水最高的相關信息,給出dept_no, emp_no以及其對應的salary

    select d.dept_no,d.emp_no,max(s.salary)
    from dept_emp as d
    join salaries as s
    on d.emp_no = s.emp_no
    group by
    d.dept_no;
  13. 從titles表獲取按照title進行分組,每組個數大於等於2,給出title以及對應的數目t。

    select title,count(title) as t
    from titles
    group by
    title
    having t >= 2;
  14. 查找employees表所有emp_no為奇數,且last_name不為Mary的員工信息,並按照hire_date逆序排列

    select * from employees
    where emp_no%2 =1
    and last_name <> 'Mary'
    order by
    hire_date
    desc;
  15. 統計出當前各個title類型對應的員工當前(to_date='9999-01-01')薪水對應的平均工資。結果給出title以及平均工資avg。

    select t.title,avg(s.salary)
    from titles as t
    join salaries as s
    on t.emp_no = s.emp_no
    where s.to_date='9999-01-01'
    and t.to_date='9999-01-01'
    group by
    t.title;
  16. 獲取當前(to_date='9999-01-01')薪水第二多的員工的emp_no以及其對應的薪水salary

    select emp_no,salary from salaries
    where to_date='9999-01-01'
    order by
    salary
    desc
    limit 1,1;  -- 降序,取第二個
  17. 查找當前薪水(to_date='9999-01-01')排名第二多的員工編號emp_no、薪水salary、last_name以及first_name,不准使用order by

    select e.emp_no,s.salary,e.last_name,e.first_name
    from employees as e
    join salaries as s
    on e.emp_no=s.emp_no
    where s.to_date='9999-01-01'
    order by
    s.salary
    desc
    limit 1,1;

    不適用order by:需要用嵌套select和max結合

    select e.emp_no,max(salary) ,e.last_name,e.first_name --這里用max(salary)
    from  salaries as s,employees as e
    where s.emp_no = e.emp_no
    and salary<  -- 小於最大薪水中的最大,就是第二大
    (
    select max(salary)
    from salaries
    where to_date = '9999-01-01'
    );
  18. 查找所有員工的last_name和first_name以及對應的dept_name,也包括暫時沒有分配部門的員工

    雙左連接

    select e.last_name,e.first_name,de.dept_name
    from employees as e
    left join dept_emp as d
    on e.emp_no=d.emp_no
    left join departments as de
    on d.dept_no=de.dept_no;
  19. 查找員工編號emp_no為10001其自入職以來的薪水salary漲幅值growth

    通過入職時間來排序查詢:

    如果直接用薪水最大值-最小值,有可能最后一次是降薪。

    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 to_date ASC LIMIT 1)
    ) AS growth
  20. 查找所有員工自入職以來的薪水漲幅情況,給出員工編號emp_no以及其對應的薪水漲幅growth,並按照growth進行升序

    --將兩個查詢的結果,當成兩個表,進行join
    select S2.emp_no, (S2.salary-S1.salary) as growth
    from 
    (select e.emp_no,s.salary 
     from employees as e 
     join salaries as s 
     on e.emp_no=s.emp_no 
     and e.hire_date=s.from_date) as S1  --所有員工的入職工資
    join 
    (select e.emp_no,s.salary 
     from employees as e 
     join salaries as s 
     on e.emp_no=s.emp_no 
     and s.to_date='9999-01-01') as S2   --所有員工的當前工資
    on S1.emp_no = S2.emp_no
    order by growth;
  21. 統計各個部門對應員工漲幅的次數總和,給出部門編碼dept_no、部門名稱dept_name以及次數sum

    select dp.dept_no,dp.dept_name,count(s.emp_no) as sum
    from departments as dp
    join dept_emp as de on de.dept_no = dp.dept_no
    join salaries as s
    on de.emp_no = s.emp_no
    group by
    de.dept_no;
  22. 對所有員工的當前(to_date='9999-01-01')薪水按照salary進行按照1-N的排名,相同salary並列且按照emp_no升序排列

    SELECT s1.emp_no, s1.salary, COUNT(DISTINCT s2.salary) AS rank
    FROM salaries AS s1, salaries AS s2
    WHERE s1.to_date = '9999-01-01'  AND s2.to_date = '9999-01-01' AND s1.salary <= s2.salary
    GROUP BY s1.emp_no
    ORDER BY s1.salary DESC, s1.emp_no ASC
  23. 獲取所有非manager員工當前的薪水情況,給出dept_no、emp_no以及salary ,當前表示to_date='9999-01-01'

    select de.dept_no,de.emp_no,s.salary
    from dept_emp as de
    join salaries as s
    on de.emp_no=s.emp_no
    where de.emp_no not in (select emp_no from dept_manager where to_date='9999-01-01')
    and de.to_date='9999-01-01'
    and s.to_date='9999-01-01';
  24. 獲取員工其當前的薪水比其manager當前薪水還高的相關信息,當前表示to_date='9999-01-01',

    select S1.emp_no as emp_no,S2.emp_no as manager_no,S1.salary as emp_salary,S2.salary as manager_salary
    from
    (select s.salary,e.emp_no,e.dept_no from salaries as s join dept_emp as e on e.emp_no = s.emp_no and s.to_date='9999-01-01') as S1,
    (select s.salary,m.emp_no,m.dept_no from salaries as s join dept_manager as m on m.emp_no = s.emp_no and s.to_date='9999-01-01') as S2
    where S1.dept_no = S2.dept_no and S1.salary > S2.salary
  25. 匯總各個部門當前員工的title類型的分配數目,結果給出部門編號dept_no、dept_name、其當前員工所有的title以及該類型title對應的數目count

    select d.dept_no,d.dept_name,t.title,count(t.title) as count
    from titles as t
    join dept_emp as e
    on e.emp_no = t.emp_no and e.to_date='9999-01-01' and t.to_date='9999-01-01'
    join
    departments as d
    on d.dept_no = e.dept_no
    group by d.dept_no,T.title
  26. 給出每個員工每年薪水漲幅超過5000的員工編號emp_no、薪水變更開始日期from_date以及薪水漲幅值salary_growth,並按照salary_growth逆序排列。

    提示:在sqlite中獲取datetime時間對應的年份函數為strftime('%Y', to_date)

    select s2.emp_no,s2.from_date,s2.salary-s1.salary as salary_growth
    from salaries as s1,salaries as s2
    where s1.emp_no = s2.emp_no
    and salary_growth > 5000
    and (strftime("%Y",s2.to_date) - strftime("%Y",s1.to_date) = 1
    OR strftime("%Y",s2.from_date) - strftime("%Y",s1.from_date) = 1)
    order by salary_growth desc
  27. 查找描述信息中包括robot的電影對應的分類名稱以及電影數目,而且還需要該分類對應電影數量>=5部

    SELECT c.name AS name, COUNT(f.film_id) AS amount
    FROM film AS f, film_category AS fc, category AS c,
    (SELECT category_id FROM film_category GROUP BY category_id HAVING COUNT(category_id) >= 5) AS cc
    WHERE f.description LIKE '%robot%'
    AND f.film_id = fc.film_id
    AND fc.category_id = c.category_id
    AND c.category_id = cc.category_id
  28. 使用join查詢方式找出沒有分類的電影id以及名稱

    select f.film_id,f.title
    from film as f
    left join 
    film_category as fc
    on fc.film_id = f.film_id
    where fc.category_id is null
  29. 使用子查詢的方式找出屬於Action分類的所有電影對應的title,description

    -- 非子查詢
    select f.title,f.description
    from film as f,film_category as fc,
    (select category_id from category where name = 'Action') as ac
    where f.film_id = fc.film_id and fc.category_id = ac.category_id
    --join
    select f.title,f.description
    from film as f 
    inner join 
    film_category as fc 
    on f.film_id = fc.film_id
    inner join 
    category as c 
    on c.category_id = fc.category_id
    where c.name = 'Action';

     

    --子查詢
    select f.title,f.description
    from film as f where f.film_id in 
    (select fc.film_id from film_category as fc where fc.category_id in
    (select c.category_id from category as c where name ='Action'))
  30. explain ,此題毫無意義 = =


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM