SELECT d.dept_no, d.emp_no, s.salary FROM dept_emp d JOIN salaries s ON d.emp_no = s.emp_no WHERE s.salary = ( SELECT MAX(s1.salary) FROM dept_emp d1 JOIN salaries s1 ON d1.emp_no = s1.emp_no WHERE d1.dept_no = d.dept_no ) ORDER BY d.dept_no ASC;
可以连续left join
1 2 3 4
SELECT e.last_name, e.first_name, d.dept_name FROM employees e LEFT JOIN dept_emp de ON e.emp_no = de.emp_no LEFT JOIN departments d ON de.dept_no = d.dept_no;
select emp_no,salary from salaries where salary = ( select salary from salaries group by salary order by salary desc limit 1 offset 1 ) order by emp_no asc
select emp_no, salary, DENSE_RANK() over(order by salary desc) t_rank from salaries order by t_rank asc,emp_no asc
1 2 3 4 5 6 7 8 9 10 11 12
- 不使用窗口函数
SELECT s1.emp_no, s1.salary, ( SELECT COUNT(DISTINCT s2.salary) FROM salaries s2 WHERE s2.salary >= s1.salary ) AS t_rank FROM salaries s1 ORDER BY s1.salary DESC, s1.emp_no ASC;
CREATE TABLE actor ( actor_id smallint(5) not null PRIMARY KEY, first_name varchar(45) not null, last_name varchar(45) not null, last_update date not null );