【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

相关文章

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

MySQL的JDBC编程详解

《MySQL的JDBC编程详解》:本文主要介绍MySQL的JDBC编程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言一、前置知识1. 引入依赖2. 认识 url二、JDBC 操作流程1. JDBC 的写操作2. JDBC 的读操作总结前言本文介绍了mysq

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1