首页 > 数据库 >牛客sql刷题

牛客sql刷题

时间:2023-11-12 22:26:31浏览次数:31  
标签:count question up qpd 牛客 sql device id 刷题

一、非技术快速入门

https://www.nowcoder.com/exam/oj?page=1&tab=SQL篇&topicId=199

题目记录:

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

  • 题目
    image

image

  • 结果
    image

  • 代码:

select up.device_id, '复旦大学' as university,
    count(question_id) as question_cnt,
    # 计算做对的题目的个数
    sum(if(qpd.result='right', 1, 0)) as right_question_cnt
from user_profile as up
left join question_practice_detail as qpd
  on qpd.device_id = up.device_id and month(qpd.date) = 8
where up.university = '复旦大学'
group by up.device_id

问题:如何确定那个表是主表?--看题目中的查询结果是以哪个表为准:因为要未做过题的用户,所以要用user表为主,如果以question表为主的话,那就只有做过的题的用户了。

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

  • 题目:
    image

image

image

  • 结果:
    image

  • 代码:

select difficult_level,
    avg(if(qpd.result='right', 1, 0)) as correct_rate
#    sum(if(qpd.result='right', 1, 0)) / count(qpd.question_id) as correct_rate
#    count(if(qpd.result='right', 1, null)) / count(qpd.question_id) as correct_rate
from user_profile as up

inner join question_practice_detail as qpd
    on up.device_id = qpd.device_id

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

where up.university = '浙江大学'
group by qd.difficult_level
order by correct_rate asc;

试题分析:1.先写select的部分;2.看筛选的字段与表中哪些字段相关,如果表与表之间有字段相等的关系,就使用关联;3.在where\group by\order by的部分调用各自表信息的限定条件。

注意:多张表联合查询:需要用到join,join有多种语法,因为条件限定需要是浙江大学的用户,所以需要是user_profile表的并且能统计出题目难度的记录,因此用user_profile表inner join另外两张表。
image

SQL39 21年8月份练题总数

  • 题目:
    image

  • 结果:
    image

  • 代码:

select
     count(distinct device_id) as did_cnt,
     count(question_id) as question_cnt
from question_practice_detail
where date like '2021-08%'

注意:对用户需要进行去重

标签:count,question,up,qpd,牛客,sql,device,id,刷题
From: https://www.cnblogs.com/yuyingblogs/p/17827989.html

相关文章

  • mysql 表级锁之一lock table
    1.locktablet1read:1.1.当前线程:读/写当前表/其他表:unlocktables;locktablet1read;select*fromt1;INSERTINTO`t1`(`c2`,`c3`,`c4`)VALUES('1','1','1');select*fromt2;INSERTINTO`t2`(`c2`,`c3`,`c4`)VALUES('......
  • SQLHelper帮助类库
    usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingMicrosoft.Data.SqlClient;usingSystem.Data;usingSystem.Configuration;namespaceYido.AdoNet.ToSQLServer{///<summary>......
  • Sqlserver 游标
    1,声明游标DECLAREcursor_nameCURSOR[LOCAL|GLOBAL][FORWARD_ONLY|SCROLL][STATIC|KEYSET|DYNAMIC|FAST_FORWARD][READ_ONLY|SCROLL_LOCKS|OPTIMISTIC][TYPE_WARNING]FORselect_statement[FORUPDATE......
  • 如何在 Python 中执行 MySQL 结果限制和分页查询
    PythonMySQL限制结果限制结果数量示例1:获取您自己的Python服务器选择"customers"表中的前5条记录:importmysql.connectormydb=mysql.connector.connect(host="localhost",user="您的用户名",password="您的密码",database="我的数据库"......
  • 如何在 Python 中执行 MySQL 结果限制和分页查询
    PythonMySQL限制结果限制结果数量示例1:获取您自己的Python服务器选择"customers"表中的前5条记录:importmysql.connectormydb=mysql.connector.connect(host="localhost",user="您的用户名",password="您的密码",database="我的数据库"......
  • psql
    1.installpsql sudoaptinstallpostgresqlpostgresql-contrib;2.loginasthedefaultuserpostgressudo-upostgrespsql;3.createuserassuperuserwithpasswordcreateusersamsuperuserloginpassword'Sam0001!';4.showallusers;\......
  • mysql主从同步延时解决
    ......
  • python3使用pymsql操作mysql数据库
    操作系统:Windows10_x64python版本:3.9.2pymysql版本:1.0.2MySQL版本:5.7.38 之前写过一篇关于python操作mysql数据库的文章:https://www.cnblogs.com/MikeZhang/p/pythonOptMysql20170703.html当时是基于python2.7和mysql5.5来整理的,但目前python2.7已经不再维护,主......
  • Java登陆第五天——SQL之DQL(三)
    子查询子查询就是在where中再嵌套一个查询SQL,可以理解为Java中方法的返回值。--甚至可以套中套无限套--被查询出来的表根据结果分为:单行子查询和多行子查询select列名from表名where( 另一个select语句 );准备数据--创建PersoncreatetablePerson(idint,......
  • linux下安装mysql
    Linux下安装MySQL概述简单介绍MySQL是什么,运用场景。在Linux操作系统(默认64位)下安装MySQL需要注意的点,以及通过navicate16连接本地MySQL实现图形化操作。注意:需要root权限,或者您能正常使用sudo提权。MySQL简介MySQL是一个关系型数据库管理系统,由瑞典MySQLAB公司开发,属于......