1 Accumulation operation
The table structure is as follows
demand
Know the score, find the worst ranking. The results are sorted in ascending order of grade.
SQL statement:
select grade, sum(number) over(order by grade) as t_rank
from class_grade
order by grade;
Some might mysql version 不支持over()函数
, 8.0
more than just support.
2 Create a view
The table structure is as follows
demand
Created 视图actor_name_view
, containing only first_name
and last_name
two, and two columns renamed first_name
as first_name_v
, last_name
changed to last_name_v
.
SQL statement
create view actor_name_view as
select first_name as first_name_v, last_name as last_name_v from actor;
operation result
3 Right connection
The table structure is as follows
demand
Find all department employees have been assigned last_name
and first_name
as well dept_no
, including temporary staff is not sector-specific allocation.
SQL statement
select e.last_name, e.first_name, d.dept_no from dept_emp as d
right join employees as e
on d.emp_no = e.emp_no;
# select e.last_name, e.first_name, d.dept_no from employees as e
# left join dept_emp as d
# on e.emp_no = d.emp_no;
This type of question 左连接
may 右连接
be used to get the correct result.
operation result
4 Left query, additional conditions
The table structure is as follows
demand
分类为null
The movie id
to be queried starts with 及名称
.
SQL statement
select f.film_id, f.title from film as f
left join film_category as fc
on f.film_id = fc.film_id
where category_id is NULL;
Analysis, the joint query of the three tables is more complicated. The above statement can be divided into two parts.
select f.film_id, f.title from film as f
left join film_category as fc
on f.film_id = fc.film_id;
The running result of this part is
and then use the condition to filter out the desired records.
Of course, you can also join
directly use the subquery without using the statement
select film_id, title from film
where film_id not in (select film_id from film_category);
It can be compared to another problem I recorded .
5 exists
keywords
The table structure is as follows
demand
Find all the information of employees who are not assigned to a specific department
SQL statement
select * from employees
where not exists (select emp_no from dept_emp where employees.emp_no = dept_emp.emp_no);
Of course you can also use not in
instead
select * from employees where emp_no
not in (select emp_no from dept_emp);