Systematic learning of SQL syntax, level-based advancement - study notes

Study Notes - Systematic study of SQL syntax, level-based advancement

Note reference: Author YupiLevel-based Sql statement learning experience website, Website open source code< /span>

If you want to experience the syntax locally: 1. Download the open source code of Big Brother and run the front-end project (recommended);
2. Or use the Sql tool yourself to simply experience the following syntax experience< /span>
Tutorial on using MariaDB tools to practice SQL statements

Database table creation

-- `student`
create table if not exists `student`
(
    `id`       integer          not null primary key AUTOINCREMENT,
    `name`     varchar(256)     not null,
    `age`      int              null,
    `class_id`    bigint           not null,
    `score`    double default 0 null,
    `exam_num` int    default 0 null
);
insert into `student` (`name`, `age`, `class_id`, `score`, `exam_num`)
values ('鸡哥', 25, 1, 2.5, 1);
insert into `student` (`name`, `age`, `class_id`, `score`, `exam_num`)
values ('鱼皮', 18, 1, 400, 4);
insert into `student` (`name`, `age`, `class_id`, `score`, `exam_num`)
values ('热dog', 40, 2, 600, 4);
insert into `student` (`name`, `age`, `class_id`, `score`, `exam_num`)
values ('摸FISH', null, 2, 360, 4);
insert into `student` (`name`, `age`, `class_id`, `score`, `exam_num`)
values ('李阿巴', 19, 3, 120, 2);
insert into `student` (`name`, `age`, `class_id`, `score`, `exam_num`)
values ('老李', 56, 3, 500, 4);
insert into `student` (`name`, `age`, `class_id`, `score`, `exam_num`)
values ('李变量', 24, 4, 390, 3);
insert into `student` (`name`, `age`, `class_id`, `score`, `exam_num`)
values ('王加瓦', 23, 4, 0, 4);
insert into `student` (`name`, `age`, `class_id`, `score`, `exam_num`)
values ('赵派森', 80, 4, 600, 4);
insert into `student` (`name`, `age`, `class_id`, `score`, `exam_num`)
values ('孙加加', 60, 5, 100.5, 1);

1. Basic syntax - query - full table query
select * from student
2. Basic syntax - query - select query
select name, age from student;
3. Basic syntax - query - alias
select name as 学生姓名, age as 学生年龄 from student;
4. Basic syntax - query - constants and operations
select name , score, score * 2 as double_score from student;
5. Basic syntax - conditional query - where
select name , score from student where name ='鱼皮'
6. Basic syntax - conditional query - operator
select name , age from student where name != '热dog'
7. Basic syntax - conditional query - null value
select name , age , score from student where age is not null
8. Basic syntax - conditional query - fuzzy query
select name , score from student where name not like '%李%'
9. Basic syntax - conditional query - logical operation
select name , score from  student where  name like '%李%' or score > 500
10. Basic grammar - deduplication
select distinct class_id , exam_num from student
11. Basic syntax - sorting
select name , age , score from student order by score desc , age asc
12. Basic syntax - truncation and offset
select name , age from student order by age asc limit 1 , 3
13. Basic syntax - conditional branch
select name , case when (age > 60) then '老同学'
 when (age >= 20) then '年轻' else  '小同学' end as age_level
from student order by name asc

14. Function - time function
select name , date() as 当前日期 from student
15. Function - String processing
select id ,  name , upper(name) as upper_name from student where  name = '热dog'
16. Function - aggregate function
select sum(score) as total_score , avg(score) as avg_score , max(score) as max_score, min(score)
as min_score from student
17. Group aggregation - single field grouping
select class_id , avg(score) as avg_score from student group by class_id
18. Group aggregation - multi-field grouping
select class_id ,exam_num, count (*) as total_num from student group by class_id ,exam_num

19. Group aggregation - having clause

select class_id, sum(score) as total_score from student group by class_id having sum(score) > 150


20. Query advanced - related query - cross join
select s.name as student_name , s.age as student_age,
s.class_id , c.name as class_name  from student s cross join class c;

21. Query advanced - related query - inner join
select s.name as student_name , s.age as student_age , s.class_id ,
c.name as class_name, c.level as class_level from student s
join class c on s.class_id = c.id;
22. Query advanced - related query - outer join
select s.name as student_name, s.age as student_age, s.class_id , c.name as class_name,
c.level as class_level from student s   left join class c on s.class_id = c.id
23. Query advanced - subquery
select name , score , class_id from student  where class_id
in (select id as class_id from class )

24. Query advanced - subquery - exists
select name ,age , class_id from student
where not exists( select * from class where student.class_id = class.id)

24. Query advanced - combined query

select name ,age ,score, class_id from student
union all
select name, age ,score, class_id from student_new
26. Query advanced - window function - sum over

Windowing is a powerful query tool that allows us to retain the original details and perform calculations on grouped data and splice them on each piece of data. Like a perspective glass, it can focus on specific groups to display all the information.

select id , name , age, score, class_id ,
avg(score) over (partition by class_id) as  class_avg_score
from student
27. Query advanced - window function - sum over order by

cumulative sum

select id , name , age , score , class_id,
sum(score) over  (partition by class_id order by score asc) as class_sum_score
from student
28. Query advanced - window function - rank
select id , name , age , score , class_id ,
rank() over (partition by class_id order by score desc) as ranking
from student
28. Query advanced - window function - row_number

The difference from rank is that the line number is unique and will increase if it is the same.


select id , name , age , score , class_id,
row_number() over (partition by class_id order by score desc) as row_number
from student


30. Query advanced - window function - lag / lead

Check the left/right situation

select id , name , age , score , class_id ,
lag(name ,1, null) over (partition by class_id order by score desc) as prev_name,
lead(name, 1,null) over (partition by class_id order by score desc) as next_name
from student

Disclaimer: The article is only for learning and creating value, please share it!
Let’s get started with the backend 204146007

Guess you like

Origin blog.csdn.net/ly_xiamu/article/details/135000658