Article directory
-
- basic exercises
-
- query all columns
- query multiple columns
- Deduplication of query results
- Query results limit the number of rows returned
- Rename the column after the query
- Find information about students whose school is Peking University
- Find and sort
- Sort by multiple columns after find
- Sort in descending order after search
- Find information about users older than 24
- Find information about users of a certain age group
- Find user information other than Fudan University
- Use where to filter empty values exercise
- Advanced Operator Exercise (1)
- Advanced Operator Exercise (2)
- Where in 和Not in
- operator mix
- View users with Beijing in their school name
- Advanced Search
- Congratulations
basic exercises
query all columns
sql:
select
*
from
user_profile;
query multiple columns
sql:
select
device_id,
gender,
age,
university
from
user_profile
Deduplication of query results
sql:
select
distinct university
from
user_profile
Method Two:
SELECT university
FROM user_profile
GROUP BY university
Query results limit the number of rows returned
sql:
select
device_id
from
user_profile
limit
2
Rename the column after the query
sql:
select
device_id as 'user_infos_example'
from
user_profile
where
id between 1 and 2
Find information about students whose school is Peking University
sql:
select
device_id,
university
from
user_profile
where
university = '北京大学'
Method Two:
select
device_id,
university
from
user_profile
where
university = '北京大学'
and device_id = user_profile.device_id; # 索引覆盖
Find and sort
sql:
select
device_id,
age
from
user_profile
order by
age
Sort by multiple columns after find
sql:
select
device_id,
gpa,
age
from
user_profile
order by
gpa,
age
Sort in descending order after search
sql:
select
device_id,
gpa,
age
from
user_profile
order by
gpa desc,
age desc
Find information about users older than 24
sql:
select
device_id,
gender,
age,
university
from
user_profile
where
age > 24
Find information about users of a certain age group
sql:
select
device_id,
gender,
age
from
user_profile
where
age between 20 and 23
Find user information other than Fudan University
sql:
select
device_id,
gender,
age,
university
from
user_profile
where
university not in ('复旦大学')
or:
select
device_id,
gender,
age,
university
from
user_profile
where
university != ('复旦大学')
Use where to filter empty values exercise
sql:
select
device_id,
gender,
age,
university
from
user_profile
where
age is NOT NULL
Advanced Operator Exercise (1)
sql:
select
device_id,
gender,
age,
university,
gpa
from
user_profile
where
gpa > 3.5
and gender = 'male'
Advanced Operator Exercise (2)
sql:
select
device_id,
gender,
age,
university,
gpa
from
user_profile
where
university = '北京大学'
or gpa > 3.7
Where in 和Not in
sql:
select
device_id,
gender,
age,
university,
gpa
from
user_profile
where
university in ('北京大学', '复旦大学', '山东大学')
operator mix
sql:
select
device_id,
gender,
age,
university,
gpa
from
user_profile
where
gpa > 3.5
and university = '山东大学'
or gpa > 3.8
and university = '复旦大学'
View users with Beijing in their school name
sql:
select
device_id,
age,
university
from
user_profile
where
university like '北京%'
Advanced Search
Find the highest GPA
sql:
select
gpa
from
user_profile
where
university = '复旦大学'
order by
gpa desc # 降序
limit
1 # 限制输出
or:
select
max(gpa)
from
user_profile
where
university = '复旦大学'
Calculate the number of boys and the average GPA
sql:
select
count(gender) as male_num,
avg(gpa) as avg_gpa
from
user_profile
where
gender = 'male'
group calculation exercises
sql:
select
gender,
university,
count(device_id) as user_num,
avg(active_days_within_30) as avg_active_days,
avg(question_cnt) as avg_question_cnt
from
user_profile
group by
gender,
university
Group Filtering Exercises
Topic: Now the operation wants to check the average posts and replies of each school user, and find low-activity schools for key operations. Please select schools with an average number of posts below 5 or schools with an average number of replies below 20.
sql:
#聚合函数结果作为筛选条件时,不能用where,而是用having语法,配合重命名即可;
select
university,
avg(question_cnt) as avg_question_cnt,
avg(answer_cnt) as avg_answer_cnt
from user_profile
group by university
having avg_question_cnt<5 or avg_answer_cnt<20
Group Sorting Exercises
sql:
select
university,
avg(question_cnt) as avg_question_cnt
from
user_profile
group by
university
order by
avg_question_cnt
Congratulations
You already have the basic knowledge of single-form query, congratulations on getting started! The questions in this article are very simple. If you can complete each question in one minute on average, you will basically master it.