带你1小时写完牛客SQL入门39题

SQL语句对程序员的重要性在于:

数据操作基础:SQL是操作关系型数据库的标准工具,程序员通过SQL实现与数据库的交互,包括查询、增删改查等基本操作。

业务逻辑处理:很多应用程序的核心业务逻辑基于数据库的数据处理,SQL语句的设计直接决定了这些逻辑能否高效正确地执行。

性能影响显著:SQL查询效率直接影响到整个应用系统的响应速度和资源利用率。良好的SQL编写习惯和优化技巧可以极大地提高程序性能。

项目需求必备:几乎所有的软件开发项目都需要与数据库打交道,掌握SQL是现代程序员的基本功,也是完成项目任务的基础能力。

跨平台兼容:尽管不同的数据库系统在SQL的具体实现上可能有差异,但标准SQL语法具有广泛的适用性,使得程序员能够在不同环境下迁移和适应。

因此,无论是在日常开发维护、性能优化还是复杂业务场景下,编写高质量的SQL语句都是衡量一个程序员专业素养和技术实力的重要指标。

目录

SQL1 查询所有列

SQL2 查询多列

SQL3 查询结果去重

SQL4 查询结果限制返回行数

SQL5 将查询后的列重新命名

SQL6 查找学校是北大的学生信息

SQL7 查找年龄大于24岁的用户信息

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

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

SQL10 用where过滤空值练习

SQL11 高级操作符练习(1)

SQL12 高级操作符练习(2)

SQL13 Where in 和Not in

SQL14 操作符混合运用

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

SQL16 查找GPA最高值

SQL17 计算男生人数以及平均GPA

SQL18 分组计算练习题

SQL19 分组过滤练习题

SQL20 分组排序练习题

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

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

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

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

SQL25 查找山东大学或者性别为男生的信息

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

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

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

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

SQL30 统计每种性别的人数

SQL31 提取博客URL中的用户名

SQL32 截取出年龄

SQL33 找出每个学校GPA最低的同学

SQL34 统计复旦用户8月练题情况

SQL35 浙大不同难度题目的正确率

SQL36 查找后排序

SQL37 查找后多列排序

SQL38 查找后降序排列

SQL39 21年8月份练题总数


SQL1 查询所有列

低效率写法:

select *
from user_profile

正常写法:

select id, device_id, gender, age, university, province
from user_profile
SQL2 查询多列
select device_id, gender, age, university
from user_profile
SQL3 查询结果去重
select distinct university
from user_profile
SQL4 查询结果限制返回行数

写法1:

select device_id
from user_profile
limit 0, 2

写法2:

select device_id
from user_profile
where id <= 2
SQL5 将查询后的列重新命名
select device_id as user_infos_example
from user_profile
limit 0, 2
SQL6 查找学校是北大的学生信息
select device_id, university
from user_profile
where university = '北京大学'
SQL7 查找年龄大于24岁的用户信息
select device_id, gender, age, university
from user_profile
where age > 24
SQL8 查找某个年龄段的用户信息

写法1:

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

写法2:

select device_id, gender, age
from user_profile
where age between 20 and 23
SQL9 查找除复旦大学的用户信息
select device_id, gender, age, university
from user_profile
where university != '复旦大学'
SQL10 用where过滤空值练习
select device_id, gender, age, university
from user_profile
where age is not null
SQL11 高级操作符练习(1)
select device_id, gender, age, university, gpa
from user_profile
where gender = 'male' and gpa > 3.5
SQL12 高级操作符练习(2)
select device_id, gender, age, university, gpa
from user_profile
where university = '北京大学' or gpa > 3.7
SQL13 Where in 和Not in
select
    device_id,
    gender,
    age,
    university,
    gpa
from
    user_profile
where
    university in ("北京大学","复旦大学","山东大学")
SQL14 操作符混合运用
select
    device_id,
    gender,
    age,
    university,
    gpa
from
    user_profile
where
    (gpa > 3.5 and university = '山东大学')
    or (gpa > 3.8 and university = '复旦大学')
SQL15 查看学校名称中含北京的用户
select
    device_id,
    age,
    university
from
    user_profile
where
    university like '%北京%'
SQL16 查找GPA最高值
select
    round(max(gpa),1)
from
    user_profile
where 
    university = '复旦大学'
SQL17 计算男生人数以及平均GPA
select count(*)  as male_num,  round(avg(gpa), 1) as avg_gpa
from user_profile
where gender = 'male'
SQL18 分组计算练习题
select 
     gender, 
     university, 
     count(*) as user_num, 
     round(avg(active_days_within_30),1) as avg_active_day,
     round(avg(question_cnt),1) as avg_question_cnt
from 
     user_profile
group by university, gender
SQL19 分组过滤练习题
select
    university,
    round(avg(question_cnt), 1) as avg_question_cnt,
    round(avg(answer_cnt), 1) as avg_answer_cnt
from
    user_profile  
group by
    university
having 
    avg_question_cnt < 5 
    or avg_answer_cnt < 20
    
SQL20 分组排序练习题
select 
     university,
     round(avg(question_cnt),4) as avg_question_cnt
from 
     user_profile
group by
     university
order by avg_question_cnt asc
SQL21 浙江大学用户题目回答情况

写法1:连接查询

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

写法2:子查询

select
    device_id,
    question_id,
    result
from
    question_practice_detail
where
    device_id in (
        select
            device_id
        from
            user_profile
        where
            university = '浙江大学'
    )
SQL22 统计每个学校的答过题的用户的平均答题数

写法1:

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

写法2:

select
    university,
    count(question_id) / count(distinct up.device_id) as avg_answer_cnt
from
    user_profile up inner join 
    question_practice_detail qpd
    on up.device_id = qpd.device_id
group by
    university
order by
    university
SQL23 统计每个学校各难度的用户平均刷题数
select
    university,
    difficult_level,
    count(qpd.question_id) / count(distinct up.device_id) as avg_answer_cnt
from
    user_profile up,
    question_practice_detail qpd,
    question_detail qd
where
    up.device_id = qpd.device_id
    and qpd.question_id = qd.question_id
group by
    university,
    difficult_level
SQL24 统计每个用户的平均刷题数
select
    university,
    difficult_level,
    count(difficult_level) / count(distinct up.device_id)
from
    user_profile up
    inner join question_practice_detail qpd on up.device_id = qpd.device_id
    inner join question_detail qd on qpd.question_id = qd.question_id
where
    university = '山东大学'
group by
    difficult_level
SQL25 查找山东大学或者性别为男生的信息

union all:并集不去重   union:并集去重

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' 
SQL26 计算25岁以上和以下的用户数量
select
    case
        when age >= 25 then '25岁及以上'
        else '25岁以下'
    end age_cut,
    count(*) number
from
    user_profile
group by
    age_cut
SQL27 查看不同年龄段的用户明细
select
    device_id,
    gender,
    case
        when age < 20 then '20岁以下'
        when (
            age >= 20
            and age <= 24
        ) then '20-24岁'
        when age >= 25 then '25岁及以上'
        else '其他'
    end as age_cnt
from
    user_profile
SQL28 计算用户8月每天的练题数量
select
    DAY (date) as day,
    count(question_id) as question_cnt
from
    question_practice_detail qpd
where
    YEAR (date) = '2021'
    and MONTH (date) = '08'
group by
    day
select
    DAY (date) as day,
    count(question_id) as question_cnt
from
    question_practice_detail qpd
where
    date like '2021-08%'
group by
    day
SQL29 计算用户的平均次日留存率

略...

SQL30 统计每种性别的人数
select
    substring_index (profile, ',', -1) as gender,
    count(device_id) as number
from
    user_submit
group by
    gender
select
    case
    when profile like '%,male' then 'male'
    when profile like '%,female' then 'female'
    end as gender,
    count(device_id) as number
from
    user_submit
group by
    gender
SQL31 提取博客URL中的用户名
select device_id, substring_index(blog_url,'/',-1) as user_name
from user_submit
SQL32 截取出年龄
select
    substring_index (substring_index (profile, ',', 3), ',', -1) as age,
    count(device_id) as number
from
    user_submit
group by
    age
SQL33 找出每个学校GPA最低的同学
select device_id, university, gpa
from user_profile u1
where gpa <= all (select u2.gpa from user_profile u2 where u1.university = u2.university )
order by university
select
    device_id,
    university,
    gpa
from
    user_profile u1
where
    gpa = (
        select
            min(u2.gpa)
        from
            user_profile u2
        where
            u1.university = u2.university
    )
order by
    university
select
    device_id,
    university,
    gpa
from
    user_profile u1
where
    (gpa,university) in (
        select
            min(u2.gpa),
            u2.university
        from
            user_profile u2
        group by u2.university
    )
order by
    university
SQL34 统计复旦用户8月练题情况
select
    up1.device_id,
    university,
    count(question_id) question_cnt,
    count(if ((result = 'right'), 1, null)) right_question_cnt
from
    question_practice_detail qpd
    right outer join (
        select
            device_id,
            university
        from
            user_profile
        where
            university = '复旦大学'
    ) up1 on up1.device_id = qpd.device_id
    and month (date) = '08'
group by
    up1.device_id
SQL35 浙大不同难度题目的正确率
select
    difficult_level,
    count(if ((result = 'right'), 1, null)) / count(*) as correct_rate
from
    question_practice_detail qpd
    inner join question_detail qd on qpd.question_id = qd.question_id
    inner join user_profile up on up.device_id = qpd.device_id
where
    university = '浙江大学'
group by
    difficult_level
order by
    correct_rate asc
SQL36 查找后排序
select device_id , age 
from user_profile
order by age asc 
SQL37 查找后多列排序
select device_id, gpa, age
from user_profile
order by gpa, age
SQL38 查找后降序排列
select device_id, gpa, age
from user_profile
order by gpa desc, age desc
SQL39 21年8月份练题总数
select
    count(distinct device_id) as did_cnt,
    count(question_id) as question_cnt
from
    question_practice_detail
where
    month (date) = '08'
  • 22
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
SQL是高级的非过程化编程语言,是沟通数据库服务器和客户端的重要工具,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以,具有完全不同底层结构的不同数据库系统,可以使用相同的SQL语言作为数据输入与管理的SQL接口。 它以记录集合作为操作对象,所有SQL语句接受集合作为输入,返回集合作为输出,这种集合特性允许一条SQL语句的输出作为另一条SQL语句的输入,所以SQL语句可以嵌套,这使它具有极大的灵活性和强大的功能,在多数情况下,在其他语言中需要一大段程序实现的功能只需要一个SQL语句就可以达到目的,这也意味着用SQL语言可以出非常复杂的语句。    结构化查询语言(Structured Query Language)最早是IBM的圣约瑟研究实验室为其关系数据库管理系统SYSTEM R开发的一种查询语言,它的前身是SQUARE语言。SQL语言结构简洁,功能强大,简单易学,所以自从IBM公司1981年推出以来,SQL语言得到了广泛的应用。如今无论是像Oracle、Sybase、DB2、Informix、SQL Server这些大型的数据库管理系统,还是像Visual Foxpro、PowerBuilder这些PC上常用的数据库开发系统,都支持SQL语言作为查询语言。    美国国家标准局(ANSI)与国际标准化组织(ISO)已经制定了SQL标准。ANSI是一个美国工业和商业集团组织,负责开发美国的商务和通讯标准。ANSI同时也是ISO和International Electrotechnical Commission(IEC)的成员之一。ANSI 发布与国际标准组织相应的美国标准。1992年,ISO和IEC发布了SQL国际标准,称为SQL-92。ANSI随之发布的相应标准是ANSI SQL-92。ANSI SQL-92有时被称为ANSI SQL。尽管不同的关系数据库使用的SQL版本有一些差异,但大多数都遵循 ANSI SQL 标准。SQL Server使用ANSI SQL-92的扩展集,称为T-SQL,其遵循ANSI制定的 SQL-92标准。    SQL语言包含4个部分:    数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句。    数据操作语言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句。    数据查询语言(DQL),例如:SELECT语句。    数据控制语言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句。    SQL语言包括三种主要程序设计语言类别的语句:数据定义语言(DDL),数据操作语言(DML)及数据控制语言(DCL)。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nuist__NJUPT

给个鼓励吧,谢谢你

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值