Python/ MySQL练习题(一)
Python/ MySQL练习题(一)
查询“生物”课程比“物理”课程成绩高的所有学生的学号
- SELECT
- *
- FROM
- (
- SELECT
- *
- FROM
- course
- LEFT JOIN score ON score.course_id = course.cid
- WHERE
- course.cname = '生物'
- ) AS A
- INNER JOIN (
- SELECT
- *
- FROM
- course
- LEFT JOIN score ON score.course_id = course.cid
- WHERE
- course.cname = '物理'
- ) AS B ON A.student_id = B.student_id
- WHERE
- A.num > B.num
查询平均成绩大于60分的同学的学号和平均成绩
- SELECT
- B.student_id,
- student.sname,
- B.cc
- FROM
- (
- SELECT
- student_id,
- num,
- avg(num) AS cc
- FROM
- score
- GROUP BY
- student_id
- HAVING
- avg(num) > 60
- ) AS B
- LEFT JOIN student ON B.student_id = student.sid
查询所有同学的学号、姓名、选课数、总成绩
- SELECT
- student_id,
- student.sname,
- count(score.course_id)as cc,
- sum(num)as cj
- FROM
- student
- LEFT JOIN score ON score.student_id = student.sid
- GROUP BY
- score.student_id
查询姓“李”的老师的个数
- SELECT * from teacher where tname like '李%'
查询没学过“李平”老师课的同学的学号、姓名
- SELECT student.sid,student.sname from student where sid not in
- (SELECT
- student_id
- FROM
- score
- WHERE course_id IN
- (
- SELECT
- course.cid
- FROM
- course
- LEFT JOIN teacher ON teacher.tid = course.teacher_id
- WHERE
- tname = '李平老师'
- ) GROUP BY student_id)
查询学过“001”并且也学过编号“002”课程的同学的学号、姓名
- select A.student_id,B.sname FROM (SELECT score.student_id,student.sname,course_id
- from score LEFT JOIN student on student.sid=score.student_id where score.course_id='')as A
- LEFT JOIN(SELECT score.student_id,student.sname,course_id
- from score LEFT JOIN student on student.sid=score.student_id where score.course_id='')as B
- on A.student_id=B.student_id
- where A.course_id=1 and B.course_id=2;
查询学过“李平”老师所教的所有课的同学的学号、姓名
- SELECT
- student.sid,
- student.sname
- FROM
- student
- WHERE
- student.sid NOT IN (
- SELECT
- student.sname
- FROM
- student
- WHERE
- student.sid IN (
- SELECT
- course.cid
- FROM
- course
- LEFT JOIN teacher ON teacher.tid = course.teacher_id
- WHERE
- teacher.tname = '李平老师'
- )
- )
查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名
- SELECT
- student.sid,
- student.sname
- FROM
- (
- SELECT
- *
- FROM
- score
- WHERE
- score.course_id = ''
- ) AS A
- LEFT JOIN (
- SELECT
- *
- FROM
- score
- WHERE
- score.course_id = ''
- ) AS B ON A.student_id = B.student_id
- LEFT JOIN student ON student.sid = B.student_id
- WHERE
- A.num < B.num
查询有课程成绩小于60分的同学的学号、姓名
- SELECT
- student.sid,
- student.sname
- FROM
- score
- LEFT JOIN course ON course.cid = score.course_id
- LEFT JOIN student ON student.sid = score.student_id
- WHERE
- score.num < 60
- GROUP BY
- student_id
查询没有学全所有课的同学的学号、姓名
- SELECT
- student.sid,
- student.sname
- FROM
- student
- WHERE
- student.sid NOT IN (
- SELECT
- student.sid
- FROM
- score
- LEFT JOIN course ON course.cid = score.course_id
- LEFT JOIN student ON student.sid = score.student_id
- GROUP BY
- score.student_id
- HAVING
- count(course_id) = (SELECT COUNT(cid) FROM course)
- )
查询至少有一门课与学号为“001”的同学所学相同的同学的学号和姓名
- SELECT
- *
- FROM
- score
- LEFT JOIN student on score.student_id = student.sid
- LEFT JOIN course ON course.cid = score.course_id
- WHERE student_id != 1 AND
- score.course_id in (
- SELECT
- course_id
- FROM
- score
- WHERE
- student_id = 1
- )
- GROUP BY student_id
查询至少学过学号为“001”同学所选课程中任意一门课的其他同学学号和姓名
- SELECT student_id from score where student_id!=1 and course_id IN
- (select course_id from score where student_id =1 GROUP BY course_id)
- GROUP BY student_id
查询和“002”号的同学学习的课程完全相同的其他同学学号和姓名
- SELECT
- student_id,
- sname
- FROM
- score
- LEFT JOIN student ON score.student_id = student.sid
- WHERE
- student_id IN (
- SELECT
- student_id
- FROM
- score
- WHERE
- student_id != 1
- GROUP BY
- student_id
- HAVING
- count(course_id) = (
- SELECT
- count(1)
- FROM
- score
- WHERE
- student_id = 1
- )
- )
- AND course_id IN (
- SELECT
- course_id
- FROM
- score
- WHERE
- student_id = 1
- )
- GROUP BY
- student_id
- HAVING
- count(course_id) = (
- SELECT
- count(1)
- FROM
- score
- WHERE
- student_id = 1
- )
删除学习“李平”老师课的SC表记录
- DELETE FROM score where score.course_id IN
- (SELECT course_id from course LEFT JOIN teacher on teacher.tid=course.teacher_id
- LEFT JOIN score on score.course_id=course.cid
- LEFT JOIN student on score.student_id=student.sid
- WHERE teacher.tname='李平老师'
- GROUP BY course_id;)
向SC表中插入一些记录,这些记录要求符合以下条件:
①没有上过编号“002”课程的同学学号;②插入“002”号课程的平均成绩
- insert into score (student_id,course_id,num)SELECT student_id,1,FLOOR(avg(num))
- (SELECT student_id from score where course_id !=2
- SELECT FLOOR(avg(num))from score where course_id = 2)
按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩,按如下形式显示:
学生ID,语文,数学,英语,有效课程数,有效平均分
- select sc.student_id,
- (select num from score left join course on score.course_id = course.cid where course.cname = "生物" and score.student_id=sc.student_id) as sy,
- (select num from score left join course on score.course_id = course.cid where course.cname = "物理" and score.student_id=sc.student_id) as wl,
- (select num from score left join course on score.course_id = course.cid where course.cname = "体育" and score.student_id=sc.student_id) as ty,
- count(sc.course_id),
- avg(sc.num)
- from score as sc
- group by student_id desc
查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
- select student_id,MAX(num),MIN(num) from score GROUP BY course_id
按各科平均成绩从低到高和及格率的百分数从高到低顺序
- SELECT
- course_id,
- avg(num) AS nn,
- sum(
- CASE
- WHEN num < 60 THEN
- 0
- ELSE
- 1
- END
- ),
- SUM(1),
- sum(
- CASE
- WHEN num < 60 THEN
- 0
- ELSE
- 1
- END
- ) / SUM(1) AS pj
- FROM
- score
- GROUP BY
- course_id
- ORDER BY
- avg(num) DESC
课程平均分从高到低显示(现实任课老师)
- SELECT score.course_id,course.cname,avg(num),teacher.tname from score LEFT JOIN course on course.cid=score.course_id
- LEFT JOIN teacher on teacher.tid=course.teacher_id
- GROUP BY course_id
- HAVING avg(num)
- ORDER BY avg(num) DESC
Python/ MySQL练习题(一)的更多相关文章
- python/MySQL练习题(二)
python/MySQL练习题(二) 查询各科成绩前三名的记录:(不考虑成绩并列情况) select score.sid,score.course_id,score.num,T.first_num,T ...
- python 全栈开发,Day65(MySQL练习题,参考答案)
一.MySQL练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号.ps:针对的是自己的生物成绩比物理成绩高,再 ...
- Python之路-python(mysql介绍和安装、pymysql、ORM sqlachemy)
本节内容 1.数据库介绍 2.mysql管理 3.mysql数据类型 4.常用mysql命令 创建数据库 外键 增删改查表 5.事务 6.索引 7.python 操作mysql 8.ORM sqlac ...
- python 之路,Day11(上) - python mysql and ORM
python 之路,Day11 - python mysql and ORM 本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 ...
- MySQL练习题
MySQL练习题 一.表关系 请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 3.查询平均成绩大于60分的同学的学号和平均成 ...
- MySQL练习题参考答案
MySQL练习题参考答案 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[ ...
- Python—>Mysql—>Dbvisualizer
MySQLdb: https://pypi.python.org/pypi/MySQL-python/1.2.4 import MySQLdb 1.Download Connector/Python: ...
- python入门练习题1
常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...
- Python Mysql 篇
Python 操作 Mysql 模块的安装 linux: yum install MySQL-python window: http://files.cnblogs.com/files/wupeiqi ...
随机推荐
- 移动端常用的meta标签,媒体查询以及一些样式设置《转载收藏》
<meta name="screen-orientation" content="portrait"> <meta name="fu ...
- nodejs加密Crypto简单例子
加密技术通常分为两大类:“对称式”和“非对称式”. 对称式加密: 就是加密和解密使用同一个密钥,通常称之为“Session Key ”这种加密技术在当今被广泛采用,如美国政府所采用的DES加密标准就是 ...
- 使用git将本地代码传到github
方法可能有些小小的差别,但是最终的结果都是一样的 在github上新建代码仓库 确定之后会显示一个仓库的url,复制下来 在本地找一个作为本地仓库的文件夹右键Git Bash Here打开git 把g ...
- curl报35错误码
一.curl常见schannel错误 schannel: SNI or certificate check failed: SEC_E_WRONG_PRINCIPAL<0x80090322> ...
- (floyd)佛洛伊德算法
Floyd–Warshall(简称Floyd算法)是一种著名的解决任意两点间的最短路径(All Paris Shortest Paths,APSP)的算法.从表面上粗看,Floyd算法是一个非常简单的 ...
- Oracle/Hive/Impala SQL比较1
5 Function 指数据库内置的function,不讨论UDF.另外,操作符都不比较了,区别不大. 5.1 数学函数 功能 Oracle Hive Impala ABS 绝对值,有 ...
- 【数据库】数据库的锁机制,MySQL中的行级锁,表级锁,页级锁
转载:http://www.hollischuang.com/archives/914 数据库的读现象浅析中介绍过,在并发访问情况下,可能会出现脏读.不可重复读和幻读等读现象,为了应对这些问题,主流数 ...
- Spring AOP 的proxy详解
spring 提供了多种不同的方案实现对 bean 的 aop proxy, 包括 ProxyFactoryBean, 便利的 TransactionProxyFactoryBean 以及 AutoP ...
- 详细说明手工创建oracle数据库实例
手工建库比起使用DBCA建库来说,是比较麻烦的,但是如果我们学好了手工建库的话,就可以使我们更好地理解Oracle数据库的体系结构.手工建库须要经过几个步骤,每一个步骤都非常关键.它包括:1. 创建必 ...
- Flash Builder4.7安装破解
引用自CSDN博客,日后我会上传FlashBuilder到百度网盘谢谢 http://bbs.csdn.net/topics/391036327