sql做题第十一天

简介: sql必做练习

第二十四例:链接查询,统计每个用户的平均刷题数

droptable if exists `user_profile`;

droptable if  exists `question_practice_detail`;

CREATETABLE `user_profile` (

`id` intNOTNULL,

`device_id` intNOTNULL,

`gender` varchar(14) NOTNULL,

`age` int ,

`university` varchar(32) NOTNULL,

`gpa` float,

`active_days_within_30` int ,

`question_cnt` int ,

`answer_cnt` int

);

CREATETABLE `question_practice_detail` (

`id` intNOTNULL,

`device_id` intNOTNULL,

`question_id`int NOTNULL,

`result` varchar(32) NOTNULL

);

CREATETABLE `question_detail` (

`id` intNOTNULL,

`question_id`int NOTNULL,

`difficult_level` varchar(32) NOTNULL

);

INSERTINTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);

INSERTINTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);

INSERTINTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);

INSERTINTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);

INSERTINTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);

INSERTINTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);

INSERTINTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);

INSERTINTO question_practice_detail VALUES(1,2138,111,'wrong');

INSERTINTO question_practice_detail VALUES(2,3214,112,'wrong');

INSERTINTO question_practice_detail VALUES(3,3214,113,'wrong');

INSERTINTO question_practice_detail VALUES(4,6543,111,'right');

INSERTINTO question_practice_detail VALUES(5,2315,115,'right');

INSERTINTO question_practice_detail VALUES(6,2315,116,'right');

INSERTINTO question_practice_detail VALUES(7,2315,117,'wrong');

INSERTINTO question_practice_detail VALUES(8,5432,117,'wrong');

INSERTINTO question_practice_detail VALUES(9,5432,112,'wrong');

INSERTINTO question_practice_detail VALUES(10,2131,113,'right');

INSERTINTO question_practice_detail VALUES(11,5432,113,'wrong');

INSERTINTO question_practice_detail VALUES(12,2315,115,'right');

INSERTINTO question_practice_detail VALUES(13,2315,116,'right');

INSERTINTO question_practice_detail VALUES(14,2315,117,'wrong');

INSERTINTO question_practice_detail VALUES(15,5432,117,'wrong');

INSERTINTO question_practice_detail VALUES(16,5432,112,'wrong');

INSERTINTO question_practice_detail VALUES(17,2131,113,'right');

INSERTINTO question_practice_detail VALUES(18,5432,113,'wrong');

INSERTINTO question_practice_detail VALUES(19,2315,117,'wrong');

INSERTINTO question_practice_detail VALUES(20,5432,117,'wrong');

INSERTINTO question_practice_detail VALUES(21,5432,112,'wrong');

INSERTINTO question_practice_detail VALUES(22,2131,113,'right');

INSERTINTO question_practice_detail VALUES(23,5432,113,'wrong');

INSERTINTO question_detail VALUES(1,111,'hard');

INSERTINTO question_detail VALUES(2,112,'medium');

INSERTINTO question_detail VALUES(3,113,'easy');

INSERTINTO question_detail VALUES(4,115,'easy');

INSERTINTO question_detail VALUES(5,116,'medium');

INSERTINTO question_detail VALUES(6,117,'easy');

  • 题目:请你写一个SQL查询,计算山东,不同难度的用户平均答题量,保留四位小数
  • 分析1:先找出山东大学的用户:select university from user_profile where university='山东大学'
  • 分析2:再找出不同难度的用户平均答题量:select university,difficult_level from user_profile u inner join question_practice_detail qpd on u.device_id=qpd.device_id inner join question_detail qd on qod.question_id = qd.question_id where university='山东大学' group by difficult_level;
  • 分析3:求用户平均答题量:count(qpd.question_id)/count(distinct qpd.device_id)
  • 示例代码

select

university,

difficult_level,

round(count(qpd.question_id)/count(distinct qpd.device_id),4) as avg_answer_cnt

from

user_profile u

inner join

question_practice_detail qpd

on

u.device_id=qpd.device_id

inner join

question_detail qd

on

qpd.question_id = qd.question_id

where

university='山东大学'

groupby

difficult_level;

第二十五例:组合查询,查找山东大学或者性别为男生的信息

droptable if exists `user_profile`;

droptable if  exists `question_practice_detail`;

CREATETABLE `user_profile` (

`id` intNOTNULL,

`device_id` intNOTNULL,

`gender` varchar(14) NOTNULL,

`age` int ,

`university` varchar(32) NOTNULL,

`gpa` float,

`active_days_within_30` int ,

`question_cnt` int ,

`answer_cnt` int

);

CREATETABLE `question_practice_detail` (

`id` intNOTNULL,

`device_id` intNOTNULL,

`question_id`int NOTNULL,

`result` varchar(32) NOTNULL

);

CREATETABLE `question_detail` (

`id` intNOTNULL,

`question_id`int NOTNULL,

`difficult_level` varchar(32) NOTNULL

);

INSERTINTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4,7,2,12);

INSERTINTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0,15,5,25);

INSERTINTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2,12,3,30);

INSERTINTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6,5,1,2);

INSERTINTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8,20,15,70);

INSERTINTO user_profile VALUES(6,2131,'male',28,'山东大学',3.3,15,7,13);

INSERTINTO user_profile VALUES(7,4321,'male',28,'复旦大学',3.6,9,6,52);

INSERTINTO question_practice_detail VALUES(1,2138,111,'wrong');

INSERTINTO question_practice_detail VALUES(2,3214,112,'wrong');

INSERTINTO question_practice_detail VALUES(3,3214,113,'wrong');

INSERTINTO question_practice_detail VALUES(4,6543,111,'right');

INSERTINTO question_practice_detail VALUES(5,2315,115,'right');

INSERTINTO question_practice_detail VALUES(6,2315,116,'right');

INSERTINTO question_practice_detail VALUES(7,2315,117,'wrong');

INSERTINTO question_practice_detail VALUES(8,5432,117,'wrong');

INSERTINTO question_practice_detail VALUES(9,5432,112,'wrong');

INSERTINTO question_practice_detail VALUES(10,2131,113,'right');

INSERTINTO question_practice_detail VALUES(11,5432,113,'wrong');

INSERTINTO question_practice_detail VALUES(12,2315,115,'right');

INSERTINTO question_practice_detail VALUES(13,2315,116,'right');

INSERTINTO question_practice_detail VALUES(14,2315,117,'wrong');

INSERTINTO question_practice_detail VALUES(15,5432,117,'wrong');

INSERTINTO question_practice_detail VALUES(16,5432,112,'wrong');

INSERTINTO question_practice_detail VALUES(17,2131,113,'right');

INSERTINTO question_practice_detail VALUES(18,5432,113,'wrong');

INSERTINTO question_practice_detail VALUES(19,2315,117,'wrong');

INSERTINTO question_practice_detail VALUES(20,5432,117,'wrong');

INSERTINTO question_practice_detail VALUES(21,5432,112,'wrong');

INSERTINTO question_practice_detail VALUES(22,2131,113,'right');

INSERTINTO question_practice_detail VALUES(23,5432,113,'wrong');

INSERTINTO question_detail VALUES(1,111,'hard');

INSERTINTO question_detail VALUES(2,112,'medium');

INSERTINTO question_detail VALUES(3,113,'easy');

INSERTINTO question_detail VALUES(4,115,'easy');

INSERTINTO question_detail VALUES(5,116,'medium');

INSERTINTO question_detail VALUES(6,117,'easy');

  • 题目:分别查看学校为山东大学或者性别为男性的用户的device_id,gender,age,和gap数据,请取出相应结果,不去重
  • 分析1:查看学校为山东大学的device_id,gender,age和gpa数据:select device_id,gender,age,gpa from user_file where university='山东大学';
  • 分析2:性别为男性的用户的device_id,gender,age和gpa数据:select device_id ,gender,age,gpa from user_profile where gender='male';
  • 需要使用union all把两个查询的数据都输出
  • 示例代码

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';


相关文章
|
SQL 数据挖掘
sql做题第九天
sql语法必做
|
SQL 索引
sql做题第十六天(删除记录篇)
• 扩展:在 delete 后加 limit 是个好习惯。原因如下: • 1,delete from 是全表查找的,如果加上limit 时,删除指定的条数后,就会return了。效率提高不少。 • 2,降低写错 SQL 的代价,即使删错了,例如limit 100,也就删除了100条数据,也能通过binlog找回数据 • 3,避免长事务,delete执行时,涉及的行是会加锁,如果删除的数据量大,那业务功能都要不能用了 • 4,加锁都是基于索引的,如果查询字段没有加索引,那会扫描到主键索引上,那么就算查询出来的只有一条记录,也会锁表 • 5,delete数据量大时,容易占用cpu,导致越删除越慢
|
SQL 关系型数据库 MySQL
sql做题第十五天(更新记录篇)
第三十七例:更新记录(2) • 题目地址:更新记录(二)牛客题霸牛客网 (nowcoder.com) • 初始化数据:
|
SQL 算法 索引
sql做题第十四天(插入记录)
• 题目描述:牛客后台会记录每个用户的试卷作答记录到exam_record表,现在有两个用户的作答记录详情如下: • 用户1001在2021年9月1日晚上10点11分12秒开始作答试卷9001,并在50分钟后提交,得了90分; • 用户1002在2021年9月4日上午7点1分2秒开始作答试卷9002,并在10分钟后退出了平台。 • 试卷作答记录表exam_record中,表已建好,其结构如下,请用一条语句将这两条记录插入表中。
|
SQL 移动开发
|
SQL Serverless
|
SQL 关系型数据库 MySQL
sql做题第七天
sql必做练习