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 ...
随机推荐
- 分享python分析wave, pcm音频文件
最近研究的,我用的是python3.3, 用matplotlib画图, 下面代码演示分析pcm文件,如果是wave文件,把wave的文件头去掉就是pcm文件了. 代码如下 # -*- coding:u ...
- shiro授权
一.shiro-permission.ini shiro-permission.ini里面的内容相当于在数据库 #用户 [users] #用户zhang的密码是123,此用户具有role1和role2 ...
- servlet的执行过程
第一次访问servlet的过程: 服务器启动:在服务器启动的时候,加载项目,就扫描web.xml文件,获得应用有哪些servlet,url-pattern, 客户端通过URl访问服务器[向服务器发送一 ...
- Jdk1.7+eclipse搭建Java开发环境
Jdk1.7+eclipse搭建Java开发环境 1. 下载jdk1.7 http://www.oracle.com/technetwork/java/javase/downloads/jdk7 ...
- echarts词云引用
最近项目中需要使用echarts的词云图,因为几经波折才引用成功,所以想记下来跟大家分享,(我的随笔不会写那么多让人需要动脑子去理解的东西,就是记录一下步骤,因为经验甚少,底层原理懂得不多,所以就先记 ...
- 跟着大神学zookeeper分布式锁实现-----来自Ruthless
前几天分享了@Ruthless大神的Redis锁,发现和大家都学习了很多东西.因为分布式锁里面,最好的实现是zookeeper的分布式锁.所以在这里把实现方式和大家分享一下. zookeeper分布式 ...
- mac上Pycharm个性化快捷键,类似Myeclipse的快速复制等快捷键
好几天没写博客了,在win10下写了几天python,然后下了pycharm使用,发现还可以,但是太笨重了,切回了mac,然后装了pycharm社区版本. 但是这个使用太别扭了,没有myeclipse ...
- codeforce round#466(div.2)C. Phone Numbers
C. Phone Numbers time limit per test2 seconds memory limit per test256 megabytes inputstandard input ...
- RabbitMQ 发布订阅持久化
RabbitMQ是一种重要的消息队列中间件,在生产环境中,稳定是第一考虑.RabbitMQ厂家也深知开发者的声音,稳定.可靠是第一考虑,为了消息传输的可靠性传输,RabbitMQ提供了多种途径的消息持 ...
- python全栈学习--day9(函数初始)
Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...