SQL2:查找入职时间较晚(排名倒数第三)的员工所有信息
//方法一
select * from employees
order by hire_date DESC
limit 2,1
//方法二
select * from employees
where hire_date = (
select distinct hire_date from employees order by hire_date desc limit 2,1
)
分析:
distinct 排除了入职时间重复的行
order by hire_date desc 根据入职日期降序,入职晚的排在前面
limit 2,1 第三页,每页1行
limit
- 如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。
- SELECT * FROM table LIMIT 5,10; // 检索记录行 6-15 ,从第6行往后显示10个
SQL4:查找所有已经分配部门的员工
where语句进行多表查询
select employees.last_name,employees.first_name,dept_emp.dept_no
from employees,dept_emp
WHERE employees.emp_no = dept_emp.emp_no
SQL7:查找薪水记录超过15次的员工号emp_no以及其对应的记录次数t
select emp_no,count(*) t
from salaries
GROUP BY emp_no
HAVING t>15
SQL8:请你找出所有员工具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
select distinct salary
from salaries
where to_date='9999-01-01'
ORDER by salary desc;
SQL10 获取所有非manager的员工emp_no
select emp_no
from employees
where emp_no not in (select emp_no from dept_manager)
删除emp_no重复的记录,只保留最小的id对应的记录
错误写法,不允许在查找表的同时删除数据
delete from titles_test
where id not in(
select min(id)
from titles_test
group by emp_no
)
正确写法,使用select *
得出的表就不再是原表,可以查询时删除原表
delete from titles_test
where id not IN
(
select * from (
select min(id)
from titles_test
group by emp_no
) t
)
SQL62 出现三次以上相同积分的情况
select number
from grade
group by number
HAVING count(number) >= 3