Single table query of SQL brush questions

basic exercises

query all columns

insert image description here
sql:

select 
    *
from
    user_profile;

query multiple columns

insert image description here

sql:

select
  device_id,
  gender,
  age,
  university
from
  user_profile

Deduplication of query results

insert image description here
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

insert image description here
sql:

select
  device_id
from
  user_profile
limit
  2

Rename the column after the query

insert image description here
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

insert image description here
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

insert image description here

sql:

select
  device_id,
  age
from
  user_profile
order by
  age

Sort by multiple columns after find

insert image description here
sql:

select
  device_id,
  gpa,
  age
from
  user_profile
order by
  gpa,
  age

Sort in descending order after search

insert image description here
sql:

select
  device_id,
  gpa,
  age
from
  user_profile
order by
  gpa desc,
  age desc

Find information about users older than 24

insert image description here
sql:

select
  device_id,
  gender,
  age,
  university
from
  user_profile
where
  age > 24

Find information about users of a certain age group

insert image description here
sql:

select
  device_id,
  gender,
  age
from
  user_profile
where
  age between 20 and 23

Find user information other than Fudan University

insert image description here
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

insert image description here
sql:

select
  device_id,
  gender,
  age,
  university
from
  user_profile
where
  age is NOT NULL

Advanced Operator Exercise (1)

insert image description here
sql:

select
  device_id,
  gender,
  age,
  university,
  gpa
from
  user_profile
where
  gpa > 3.5
  and gender = 'male'

Advanced Operator Exercise (2)

insert image description here
sql:

select
  device_id,
  gender,
  age,
  university,
  gpa
from
  user_profile
where
  university = '北京大学'
  or gpa > 3.7

Where in 和Not in

insert image description here
sql:

select
  device_id,
  gender,
  age,
  university,
  gpa
from
  user_profile
where
  university in ('北京大学', '复旦大学', '山东大学')

operator mix

insert image description here
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

insert image description here
sql:

select
  device_id,
  age,
  university
from
  user_profile
where
  university like '北京%'

Advanced Search

Find the highest GPA

insert image description here
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

insert image description here
sql:

select
  count(gender) as male_num,
  avg(gpa) as avg_gpa
from
  user_profile
where
  gender = 'male'

group calculation exercises

insert image description here
insert image description here
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

insert image description here
insert image description here

insert image description here
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.

Guess you like

Origin blog.csdn.net/weixin_46211269/article/details/126669368