找出所有员工当前(to_date=‘9999-01-01’)具体的薪水salary情况,对于相同的薪水只显示一次,并按照逆序显示
select distinct salary from salaries where to_date= '9999-01-01' order by salary desc ;
!!!注意:order by 语句必须放到最后
select salary from salaries where to_date= '9999-01-01' group by salary order by salary desc ;
对指定列分组查询即相同的只会显示一次,不会重复
查找最晚入职员工的所有信息
select * from employees order by hire_date desc limit 1 ;
limit 1
查找入职员工时间排名倒数第三的员工所有信息
select * from employees order by hire_date desc limit 2 , 1 ;
limit m, n
select * from employees order by hire_date desc limt 1 offset 2 ;
limit m offset n
查找薪水涨幅超过15次的员工号emp_no以及其对应的涨幅次数t
select emp_no count ( emp_no) t from salaries group by emp_no having t> 15 ;
获取所有部门当前manager的当前薪水情况,给出dept_no, emp_no以及salary,当前表示to_date=‘9999-01-01’
to_date= '9999-01-01'
select d. dept_no, d. emp_no, s. salary
from dept_manager d, salaries s
where d. emp_no= s. emp_no and d. to_date= '9999-01-01' and s. to_date= '9999-01-01' ;
从titles表获取按照title进行分组,每组个数大于等于2,给出title以及对应的数目t
select title, count ( title) t from titles group by title having t>= 2 ;
查找重复的邮箱
select Email from Person group by Email having count ( Email) >= 2 ;
编写一个SQL查询,输出表中所有大国家的名称、人口和面积。大国家:一个国家的面积超过300万平方公里,或者人口超过2500万
select name, population, area from World where area> 3000000 or population> 25000000 ;
获取 Employee 表中第 n 高的薪水(Salary)
select distinct Salary from Employee order by Salary desc limit 1 offset N- 1 ;
查找字符串str中子字符串s出现的次数cnt
select ( length( str) - length( replace ( str, s, '' ) ) ) / length( s) cnt;
用空字符串替换要查找的字符串,相减之后的长度除去子字符串的长度,即为次数
查找所有员工入职时候的薪水情况,给出emp_no以及salary, 按照emp_no进行逆序
select e. emp_no, s. salary from employee e, salaries s
where e. emp_no= s. emp_no and e. hire_date= s. from_date
order by e. emp_no desc ;
获取所有非manager的员工emp_no --> 使用复合查询
select emp_no from employees where not in select emp_no from dept_manager
职工号不在manaher表里的员工即为不是manager的员工
获取所有员工当前的manager,如果当前的manager是自己的话结果不显示,当前表示to_date=‘9999-01-01’。结果第一列给出当前员工的emp_no,第二列给出其manager对应的manager_no。
select e. emp_no emp_no, m. emp_no manager_no
from dept_emp e, dept_manager m
where e. emp_no!= m. emp_no 员工和上司的职工号不一样
and e. dept_no= m. dept_no 在同一个部门
and e. to_date= '9999-01-01'
and m. to_date= '9999-01-01' ;