sql做题第十四天初级入门综合练习

简介: sql必做语法

第三十一例:综合练习,统计复旦用户8月练题情况

droptable if exists `user_profile`;

droptable if  exists `question_practice_detail`;

droptable if  exists `question_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,

`date` dateNOTNULL

);

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','2021-05-03');

INSERTINTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');

INSERTINTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');

INSERTINTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');

INSERTINTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');

INSERTINTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');

INSERTINTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');

INSERTINTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');

INSERTINTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');

INSERTINTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');

INSERTINTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');

INSERTINTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');

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

  • 题目描述:想要了解复旦大学的每个用户在8月份练习的总题目数和回答正确的题目数情况,请取出相应明细数据,对于在8月份没有练习过的用户,答题数结果返回0
  • 分析1:先统计每个用户在8月份练习的总题目数和回答正确的题目数:

select

up.device_id,

up.university

from

user_profile up

left join

question_practice_detail qpd

on

up.device_id=qpd.device_id

and

month(qpd.date) = '08'andyear(qpd.date) = '2021'

where

up.university='复旦大学';

  • 分析2:接下来统计答题的总数量和答题正确的数量,8月份没有练习过的用户,答题结果返回0

条件表达式语法:

case

   when sex='1' then

'男'

   when sex='2' then '女'

   else '其他'

end

sum(

   case

       when qpd.resultisnotnull

       then 1

       else 0

   end

) as question_cnt,

sum(

   case

       when qpd.result = 'right'

       then 1

       else 0

   end

) as right_question_cnt

  • 代码示例:

select

up.device_id,

up.university,

sum(

   case

       when qpd.resultisnotnull

       then 1

       else 0

   end

) as question_cnt,

sum(

   case

       when qpd.result = 'right'

       then 1

       else 0

   end

) as right_question_cnt

from

user_profile up

left join

question_practice_detail qpd

on

up.device_id=qpd.device_id

and

month(qpd.date) = '08'andyear(qpd.date) = '2021'

where

up.university='复旦大学'

groupby

device_id;

第三十二题:综合练习,浙大不同难度题目的正确率

droptable if exists `user_profile`;

droptable if  exists `question_practice_detail`;

droptable if  exists `question_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,

`date` dateNOTNULL

);

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','2021-05-03');

INSERTINTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');

INSERTINTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');

INSERTINTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');

INSERTINTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');

INSERTINTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');

INSERTINTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');

INSERTINTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');

INSERTINTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');

INSERTINTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');

INSERTINTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');

INSERTINTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');

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

  • 题目描述:想要了解浙江大学的用户在不同难度题目下答题的正确情况,按照准确率升序输出
  • 分析1:先找出浙江大学的用户有多少:

select

up.device_id

from

user_profile up

where

university='浙江大学';

  • 分析2:查看浙江大学答题情况:

select

up.device_id,qpd.result

from

user_profile up

inner join

question_practice_detail qpd

on

up.device_id = qpd.device_id

where

up.university = '浙江大学';

  • 分析3:找出答题题目对于的难度:

select

up.device_id,qpd.result,qd.diffcult_level

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

up.university = '浙江大学';

  • 分析4:统计答题的正确率情况

sum(

   case

       when qpd.result='right'

       then 1

       else 0

   end

) / count(qpd.result) as correct_rate

  • 代码示例:

select

qd.difficult_level,

sum(

   case

       when qpd.result='right'

       then 1

       else 0

   end

) / count(qpd.result) as correct_rate

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

up.university = '浙江大学'

groupby

qd.difficult_level

orderby

correct_rate asc;

第三十三例:综合练习,21年8月份练题总数

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,

`date` dateNOTNULL

);

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','2021-05-03');

INSERTINTO question_practice_detail VALUES(2,3214,112,'wrong','2021-05-09');

INSERTINTO question_practice_detail VALUES(3,3214,113,'wrong','2021-06-15');

INSERTINTO question_practice_detail VALUES(4,6543,111,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(5,2315,115,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(6,2315,116,'right','2021-08-14');

INSERTINTO question_practice_detail VALUES(7,2315,117,'wrong','2021-08-15');

INSERTINTO question_practice_detail VALUES(8,3214,112,'wrong','2021-05-09');

INSERTINTO question_practice_detail VALUES(9,3214,113,'wrong','2021-08-15');

INSERTINTO question_practice_detail VALUES(10,6543,111,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(11,2315,115,'right','2021-08-13');

INSERTINTO question_practice_detail VALUES(12,2315,116,'right','2021-08-14');

INSERTINTO question_practice_detail VALUES(13,2315,117,'wrong','2021-08-15');

INSERTINTO question_practice_detail VALUES(14,3214,112,'wrong','2021-08-16');

INSERTINTO question_practice_detail VALUES(15,3214,113,'wrong','2021-08-18');

INSERTINTO question_practice_detail VALUES(16,6543,111,'right','2021-08-13');

  • 题目描述:想要了解2021年8月份所有练习过题目的总用户数和练习过的总次数
  • 分析1:找出练习过题目的总用户数:

select

count(distinct device_id) as did_cnt

from

question_practice_detail

where

datelike'2021-08%';

  • 代码示例

select

count(distinct device_id) as did_cnt,

count(question_id) as question_cnt

from

question_practice_detail

where

datelike'2021-08%';

  • 代码示例2:

select

count(distinct device_id) as did_cnt,

count(question_id) as question_cnt

from

question_practice_detail

where

date>='2021-08-01'anddate <= '2021-08-31';

  • 代码示例3:

select

count(distinct device_id) as did_cnt,

count(question_id) as question_cnt

from

question_practice_detail

where

year(date)=2021and month(date)=8;


相关文章
|
7月前
|
SQL Perl
PL/SQL经典练习
PL/SQL经典练习
|
2月前
|
SQL 存储 数据管理
SQL数据库的使用指南:从入门到精通
随着信息技术的飞速发展,数据库已成为各类企业和组织不可或缺的一部分。作为最流行的数据库管理系统之一,SQL数据库广泛应用于各种场景,如数据存储、数据管理、数据分析等。本文将详细介绍SQL数据库的使用方法,帮助初学者快速入门,并帮助有经验的开发者深化理解。一、SQL数据库基础首先,我们需要理解SQL数
129 2
|
2月前
|
SQL 数据库
SQL数据库基础语法入门
[link](http://www.vvo.net.cn/post/082935.html)
|
3月前
|
SQL 安全 数据库
从入门到精通:Python Web安全守护指南,SQL注入、XSS、CSRF全防御!
【9月更文挑战第13天】在开发Python Web应用时,安全性至关重要。本文通过问答形式,详细介绍如何防范SQL注入、XSS及CSRF等常见威胁。通过使用参数化查询、HTML转义和CSRF令牌等技术,确保应用安全。附带示例代码,帮助读者从入门到精通Python Web安全。
94 6
|
2月前
|
SQL 安全 关系型数据库
SQL自动化注ru-SQLmap入门操作(一)
SQL自动化注ru-SQLmap入门操作(一)
|
2月前
|
SQL 安全 关系型数据库
SQL自动化注茹-SQLmap入门操作(二)
SQL自动化注茹-SQLmap入门操作(二)
|
4月前
|
SQL 存储 关系型数据库
数据库SQL入门指南
数据库SQL入门指南
|
4月前
|
Java 数据库连接 数据库
告别繁琐 SQL!Hibernate 入门指南带你轻松玩转 ORM,解锁高效数据库操作新姿势
【8月更文挑战第31天】Hibernate 是一款流行的 Java 持久层框架,简化了对象关系映射(ORM)过程,使开发者能以面向对象的方式进行数据持久化操作而无需直接编写 SQL 语句。本文提供 Hibernate 入门指南,介绍核心概念及示例代码,涵盖依赖引入、配置文件设置、实体类定义、工具类构建及基本 CRUD 操作。通过学习,你将掌握使用 Hibernate 简化数据持久化的技巧,为实际项目应用打下基础。
287 0
|
4月前
|
SQL 关系型数据库 数据挖掘
SQL 基础入门简直太重要啦!从零开始,带你轻松掌握数据查询与操作,开启数据世界大门!
【8月更文挑战第31天】在数字化时代,数据无处不在,而 SQL(Structured Query Language)则是开启数据宝藏的关键钥匙。无论你是编程新手还是数据处理爱好者,掌握 SQL 都能帮助你轻松提取和分析信息。SQL 简洁而强大,像一位魔法师,能从庞大数据库中迅速找到所需数据。从查询、条件筛选到排序、分组,SQL 功能多样,还能插入、更新和删除数据,助你在数据海洋中畅游无阻。
47 0
|
4月前
|
SQL 数据库 索引
SQL语言入门:如何表达你的数据需求
在数据库的世界里,SQL(Structured Query Language)是一种至关重要的语言,它允许用户与数据库进行交互,执行数据的查询、更新、插入和删除等操作