【SQL】牛客网SQL非技术入门40道代码|练习记录

2024-06-10 16:52

本文主要是介绍【SQL】牛客网SQL非技术入门40道代码|练习记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

跟着刷题:是橘长不是局长哦_哔哩哔哩_bilibili

6查询学校是北大的学生信息

select
device_id, university
from user_profile
where university = '北京大学'

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

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

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

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

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

select
device_id,	gender,	age,	university
from user_profile
where university != '复旦大学'

10用where过滤空值练习

select
device_id,	gender,	age,	university
from user_profile
where age is not null

11高级操作符练习(1)

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

12高级操作符练习(2)

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

13Where in 和Not in

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

14操作符混合运用

select
device_id,	gender,	age,	university, gpa
from user_profile
where gpa>3.5 and university='山东大学'
or  gpa>3.8 and university='复旦大学'

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

selectdevice_id,age,universityfromuser_profile
whereuniversity like '%北京%'

16查找GPA最高值

执行顺序是先where再from

excel里也是先筛选再聚合??为什么捏?好处是:先筛选之后数据量就变少了,然后再计算(或者更复杂的操作),数据量越少,执行速度就快,大概率

select
round(max(gpa), 1)
from user_profile
where university = '复旦大学'

17计算男生人数以及他们的平均GPA

函数count()

可以是

  • count(id):统计某个列中非 NULL 值的数量
  • count(*):统计表中的总行数
  • count(1):这与 COUNT(*) 相同
select
round(count(1), 1)
,round(avg(gpa), 1)
from user_profile
where gender='male'

17改题:计算男生人数以及全班的平均GPA

count(if())

select
round(count(if gender = 'male', 1, null), 1)
,round(avg(gpa), 1)
from user_profile

18分组计算练习题

分组聚合

selectgender,university,round(count(device_id), 1),round(avg(active_days_within_30), 1),round(avg(question_cnt), 1)
fromuser_profile
group by1,2

19分组过滤练习题

在excel里会怎么做:先求出平均发帖和回帖情况(用数据透视表做),然后再筛选符合条件的

关键字:having,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

解法2:可以用子查询

不过having存在,就是让你可以少写一层子查询,仅此

select
*
from(selectuniversity,avg(question_cnt) as avg_question_cnt,avg(answer_cnt) as avg_answer_cntfromuser_profilegroup byuniversity
) as a
where avg(question_cnt) < 5 or avg(answer_cnt) < 20

20分组排序练习

关键字:order by,默认升序排列;降序加desc,如 order by 2 desc

select 
university
,avg(question_cnt)
from user_profile
group by 1
order by 2

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

excel里XLOOKUP,只连接某个字段

关键字join,表连接

  • 左连接:left join,保证左边的表不变,右表拼接上来,如果没有拼上,右边的那条数据就不要了,保证了左表的完整性
  • 右连接:
  • 全连接:为空也保留,左右都保留
  • 内连接:join,只要一边没连上全都丢掉

1)只连接指定列:

select from question_practice_detail a
left join
(selectdevice_id, universityfrom user_profile
) b
on a.device_id = b.device_id

2)连接所有列:

select from question_practice_detail a
left join user_profile b
on a.device_id = b.device_id

先连接,后筛选,

select 
a.device_id as device_id
,a.question_id as question_id
,a.result as result
from question_practice_detail a
left join user_profile b
on a.device_id = b.device_id
where b.university = '浙江大学'

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

说明:某学校用户平均答题数量计算方式为该学校用户答题总次数除以答过题的不同用户个数

select
b.university as university
, count(1) / count(distinct a.device_id) as avg_answer_cnt
from question_practice_detail a
left join user_profile b
on a.device_id = b.device_id
group by 1
order by 1

23

事实表、维度表、信息表?

第一步连接(多表连接)

selectfrom question_practice_detail a
left join user_profile b
on a.device_id = b.device_id
left join question_detail c
on a.question_id = c.question_id

bug?刚刚在牛客提交,明明是一样的答案提交错误,等了一会就可以了

select
b.university as university
,c.difficult_level as difficult_level
,count(1) / count(distinct a.device_id) as avg_answer_cnt
from question_practice_detail a
left join user_profile b
on a.device_id = b.device_id
left join question_detail c
on a.question_id = c.question_id
group by 1, 2

24

这篇关于【SQL】牛客网SQL非技术入门40道代码|练习记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1048727

相关文章

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL中EXISTS与IN用法使用与对比分析

《MySQL中EXISTS与IN用法使用与对比分析》在MySQL中,EXISTS和IN都用于子查询中根据另一个查询的结果来过滤主查询的记录,本文将基于工作原理、效率和应用场景进行全面对比... 目录一、基本用法详解1. IN 运算符2. EXISTS 运算符二、EXISTS 与 IN 的选择策略三、性能对比

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

SQL Server跟踪自动统计信息更新实战指南

《SQLServer跟踪自动统计信息更新实战指南》本文详解SQLServer自动统计信息更新的跟踪方法,推荐使用扩展事件实时捕获更新操作及详细信息,同时结合系统视图快速检查统计信息状态,重点强调修... 目录SQL Server 如何跟踪自动统计信息更新:深入解析与实战指南 核心跟踪方法1️⃣ 利用系统目录

MySQL 内存使用率常用分析语句

《MySQL内存使用率常用分析语句》用户整理了MySQL内存占用过高的分析方法,涵盖操作系统层确认及数据库层bufferpool、内存模块差值、线程状态、performance_schema性能数据... 目录一、 OS层二、 DB层1. 全局情况2. 内存占js用详情最近连续遇到mysql内存占用过高导致

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd