牛客MySQL练习题

2022-01-17  本文已影响0人  抬头挺胸才算活着
SELECT *
FROM employees
WHERE hire_date = (
        SELECT max(hire_date)
        FROM employees
        );
SELECT *
FROM employees
WHERE hire_date = (
        SELECT distinct hire_date
        FROM employees
        ORDER BY hire_date DESC limit 1 offset 2
        );

同部门多个最多的工资会返回多条:group找到最大的工资后再join找出对应的emp_no

SELECT t.dept_no
    ,emp_no
    ,maxSalary
FROM (
    SELECT dept_no
        ,dept_emp.emp_no
        ,salary
    FROM dept_emp
    INNER JOIN salaries ON dept_emp.emp_no = salaries.emp_no
    ) t
INNER JOIN (
    SELECT dept_no
        ,max(salary) AS maxSalary
    FROM dept_emp
    INNER JOIN salaries ON dept_emp.emp_no = salaries.emp_no
    GROUP BY dept_no
    ) AS r 
    ON t.dept_no = r.dept_no AND t.salary = r.maxSalary
ORDER BY dept_no;

第二种解法只取一个,无法理解?

select r.dept_no,r.emp_no,max(r.salary) from (
select d.dept_no,d.emp_no,s.salary from dept_emp d,salaries s
where d.emp_no=s.emp_no·
and d.to_date='9999-01-01' 
and s.to_date='9999-01-01'
order by s.salary desc
)as r
group by r.dept_no
order by r.dept_no asc

另外一种开窗函数的解法

SELECT dept_no
    ,emp_no
    ,salary AS maxSalary
FROM (
    SELECT dept_no
        ,emp_no
        ,salary
        ,row_number() OVER (
            PARTITION BY dept_no ORDER BY salary desc
            ) AS rn
    FROM (
        SELECT dept_emp.emp_no
            ,dept_emp.dept_no
            ,salaries.salary
        FROM dept_emp
        INNER JOIN salaries ON dept_emp.emp_no = salaries.emp_no
        ) AS t_dept_salaries
    ) AS t_dept_salaries_ordered
WHERE rn = 1;
SELECT emp_no
    ,salary
FROM (
    SELECT emp_no
        ,salary
        ,DENSE_RANK() OVER (
            ORDER BY salary DESC
            ) AS r
    FROM salaries
    ) AS t_rank
WHERE r = 2;

方法2:使用子查询获取第二多的薪水,然后筛选

SELECT emp_no
    ,salary
FROM salaries
WHERE salary = (
        SELECT DISTINCT salary
        FROM salaries
        ORDER BY salary DESC limit 1 offset 1
        );
SELECT e.emp_no
    ,s.salary
    ,e.last_name
    ,e.first_name
FROM salaries s
INNER JOIN employees e ON s.emp_no = e.emp_no
WHERE salary = (
        SELECT max(salary)
        FROM salaries
        WHERE salary != (
                SELECT max(salary)
                FROM salaries
                )
        );

方法2:使用inner join找到两个比它大于等于的薪水,好处是可以得到任意大的薪水

SELECT e.emp_no
    ,s.salary
    ,e.last_name
    ,e.first_name
FROM employees e
JOIN salaries s ON e.emp_no = s.emp_no
    AND s.salary = (
        SELECT s1.salary
        FROM salaries s1
        INNER JOIN salaries s2 ON s1.salary < s2.salary
        GROUP BY s1.salary
        HAVING count(*) = 1
        )
SELECT current_s.emp_no
    ,current_s.salary - entry_s.salary as growth
FROM (
    SELECT emp_no
        ,salary
    FROM salaries
    WHERE to_date = "9999-01-01"
    ) AS current_s
INNER JOIN (
    SELECT e.emp_no
        ,salary
    FROM employees e
    INNER JOIN salaries s ON e.hire_date = s.from_date
    ) AS entry_s ON current_s.emp_no = entry_s.emp_no
ORDER by growth;
SELECT emp_no
    ,salary
    ,DENSE_RANK() OVER (
        ORDER BY salary DESC
        )
FROM salaries
ORDER BY salary DESC;

方法2:利用本身之间的join,注意DISTINCT的使用

SELECT s1.emp_no
    ,s1.salary
    ,count(DISTINCT s2.salary) AS rank
FROM salaries s1
INNER JOIN salaries s2 ON s1.emp_no = s2.emp_no
    AND s1.salary <= s2.salary
GROUP BY s1.emp_no
ORDER BY s1.salary DESC;
SELECT f.title
    ,f.description
FROM film f
WHERE f.film_id IN (
        SELECT film_id
        FROM film_category fc
        WHERE fc.category_id = (
                SELECT category_id
                FROM category
                WHERE name = 'Action'
                )
        )
上一篇下一篇

猜你喜欢

热点阅读