1 Average and decimal places
The table structure is as follows.
Query the scores of each post 平均数
, and sort them in descending order of the scores , and keep the results 3位
after the decimal point (after 3 digits 四舍五入
)
select job, round(avg(score), 3) from grade
group by job
order by avg(score) desc;
Pay attention to the use of round
and avg
functions.
2 Find options in table A that do not have table B
The table structure is as follows:
Find all employees who are not department leaders emp_no
.
select emp_no from employees
where emp_no not in (select emp_no from dept_manager);
Here look up two tables at the same time, pay attention to not in
the use of keywords .
3 not in
keywords
The table structure is as follows:
Delete the duplicate record of emp_no, and only keep the record corresponding to the smallest id
delete from titles_test
where id not in(
select * from (
select min(id) from titles_test
group by emp_no
) as t1
);
analysis
not in
The statement in the following brackets is to find the record with the smallest id among the duplicate records.
select * from (
select min(id) from titles_test
group by emp_no
) as t1
These three rows of records are to be deleted, just select
replace the ones in the figure delete
.
4 concat
function to connect two fields
The table structure is as follows
. The last_name and first_name of all employees are spliced together as the Name, separated by a space in the middle
select concat(last_name, ' ', first_name) as Name
from employees;
5 表名 left join 表名 on 条件
left outer join
The table structure is as follows to
find the task status of each person, and the output results are sorted in ascending order according to the person's 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;