1 平均数和保留小数位
表结构如下
查询各个岗位分数的平均数
,并且按照分数降序排序,结果保留小数点后面3位
(3位之后四舍五入
)
select job, round(avg(score), 3) from grade
group by job
order by avg(score) desc;
要注意round
和avg
函数的使用。
2 查找A表中没有B表的选项
表结构如下
查找所有非部门领导的员工emp_no
。
select emp_no from employees
where emp_no not in (select emp_no from dept_manager);
这里同时查找两个表,注意关键词not in
的使用。
3 not in
关键词
表结构如下
删除emp_no重复的记录,只保留最小的id对应的记录
delete from titles_test
where id not in(
select * from (
select min(id) from titles_test
group by emp_no
) as t1
);
分析
not in
后面的括号中的语句是查找重复的记录中,id最小的记录。
select * from (
select min(id) from titles_test
group by emp_no
) as t1
这三行记录是要删除的,将图中的select
换成delete
即可。
4 concat
函数连接两个字段
表结构如下
将所有员工的last_name和first_name拼接起来作为Name,中间以一个空格区分
select concat(last_name, ' ', first_name) as Name
from employees;
5 表名 left join 表名 on 条件
左外连接
表结构如下
查找每个人的任务情况,而且输出结果按照person的id升序排序。
select p.id, p.name, t.content from person as p
left join task as t
on p.id = t.person_id
order by p.id;