SQL基础查询语句总结

时隔三年,复习一下SQL的基础查询语句~

一、基础查询

示例:user_profile

1)查询结果去重

  • 使用distinct关键字(关键词distinct用于返回唯一不同的值)
select distinct university from user_profile
  • 使用group by
select university from user_profile group by university

2)查询结果限制返回行数

select device_id from user_profile limit 0,2 // 运行效率高
select device_id from user_profile limit 2   // 运行效率低
select device_id from user_profile limit 2 offset 0 // 跳过0条,从第一条数据开始取,取两条数据,运行效率中

二、条件查询

示例:user_profile

1)查找某个年龄段的用户信息

select device_id, gender, age from user_profile WHERE age between 20 and 23
select device_id, gender, age from user_profile WHERE age >= 20 and age<=23

2)查找除复旦大学的用户信息

select device_id, gender, age, university from user_profile where university not in ('复旦大学')

3)查找后排序

select device_id, age from user_profile order by age ASC
// 按<列名>进行升序(ASC)或降序(DESC)排序 : ORDER BY <列名> [ASC | DESC ]

4)查找后多列排序

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

5)用where过滤空值

select device_id, gender, age, university from user_profile where age is not NULL
select device_id, gender, age, university from user_profile where age !='' 

6)查看学校名称中含北京的用户 

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

三、高级查询

示例:user_profile

1)查找GPA最高值

select max(gpa) as gpa from user_profile where university='复旦大学'
select gpa from user_profile where university='复旦大学' order by gpa desc limit 1

2)计算男生人数以及平均GPA(保存一位小数)

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

3)分组计算

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

4)分组过滤

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

5) 分组排序

select university,
    avg(question_cnt) as avg_question_cnt
from user_profile
group by university
order by avg_question_cnt

四、多表查询

示例:question_practice_detail

示例:user_profile

1)浙江大学用户题目回答情况

select qpd.device_id, qpd.question_id, qpd.result
from question_practice_detail as qpd
inner join user_profile as up
on up.device_id=qpd.device_id and up.university='浙江大学'
order by question_id

2)统计每个学校的答过题的用户的平均答题数

select university,
    count(question_id) / count(distinct qpd.device_id) as avg_answer_cnt
from question_practice_detail as qpd
inner join user_profile as up
on qpd.device_id=up.device_id
group by university

3)统计每个学校各难度的用户平均刷题数

示例:question_detail 

select 
    university,
    difficult_level,
    round(count(qpd.question_id) / count(distinct qpd.device_id), 4) as avg_answer_cnt
from question_practice_detail as qpd

left join user_profile as up
on up.device_id=qpd.device_id

left join question_detail as qd
on qd.question_id=qpd.question_id

group by university, difficult_level

4)统计每个用户的平均刷题数

select
    "山东大学" as university,
    difficult_level,
    count(qpd.question_id) / count(distinct qpd.device_id) as avg_answer_cnt
from question_practice_detail as qpd
 
inner join user_profile as up
on up.device_id=qpd.device_id and up.university="山东大学"
 
inner join question_detail as qd
on qd.question_id=qpd.question_id
 
group by difficult_level

5)组合查询

select
    device_id, gender, age, gpa
from user_profile
where university='山东大学'
 
union all
 
select
    device_id, gender, age, gpa
from user_profile
where gender='male'

五、常用函数

1. CASE函数

一种多分支的函数,可以根据条件列表的值返回多个可能的结果表达式中的一个,可用在任何允许使用表达式的地方,但不能单独作为一个语句执行。

1)计算25岁以上和以下的用户数量

示例:user_profile

select case when age < 25 or age is null then '25岁以下'
            when age >= 25 then '25岁及以上'
            end age_cut, count(*) as number
from user_profile
group by age_cut

2)查看不同年龄段的用户明细 

select
    device_id,
    gender,
    case
        when age>=25 then '25岁及以上'
        when age>=20 then '20-24岁'
        when age<20 then '20岁以下'
        else '其他'
    end as age_cut
from user_profile

2. 日期函数

1)计算用户8月每天的练题数量

select
    day(date) as day,
    count(question_id) as question_cnt
from question_practice_detail
where month(date)=8 and year(date)=2021
group by date

2)计算用户的平均次日留存率

select count(date2) / count(date1) as avg_ret
from (
    select
        distinct qpd.device_id,
        qpd.date as date1,
        uniq_id_date.date as date2
    from question_practice_detail as qpd
    left join(
        select distinct device_id, date
        from question_practice_detail
    ) as uniq_id_date
    on qpd.device_id=uniq_id_date.device_id
        and date_add(qpd.date, interval 1 day)=uniq_id_date.date
) as id_last_next_date
select avg(if(datediff(date2, date1)=1, 1, 0)) as avg_ret
from (
    select
        distinct device_id,
        date as date1,
        lead(date) over (partition by device_id order by date) as date2
    from (
        select distinct device_id, date
        from question_practice_detail
    ) as uniq_id_date
) as id_last_next_date

3. 文本函数

1)统计每种性别的人数 

select substring_index(profile, ',' , -1) as gender, count(*) as number
from user_submit
group by gender
select if(profile like '%female','female','male') gender, count(*) as number
from user_submit
group by gender

 2)提取博客URL中的用户名

select
-- 替换法 replace(string, '被替换部分','替换后的结果')
-- device_id, replace(blog_url,'http:/url/','') as user_name
 
-- 截取法 substr(string, start_point, length*可选参数*)
-- device_id, substr(blog_url,11,length(blog_url)-10) as user_nam
 
-- 删除法 trim('被删除字段' from 列名)
-- device_id, trim('http:/url/' from blog_url) as user_name
 
-- 字段切割法 substring_index(string, '切割标志', 位置数(负号:从后面开始))
device_id, substring_index(blog_url,'/',-1) as user_name
 
from user_submit;

 3)截取出年龄

select
    substring_index(substring_index(profile, ',', 3), ',', -1) as age,
    count(device_id) as number
from user_submit
group by age

4. 窗口函数

select device_id,university,gpa
from user_profile
where (university,gpa) in (select university,min(gpa) from user_profile group by university)
order by university

猜你喜欢

转载自blog.csdn.net/daydayup858/article/details/134231613