SQL语法练习(一)
- 查询学习课程”python”比课程 “java” 成绩高的学生的学号;
– 思路:
– 获取所有有python课程的人(学号,成绩) - 临时表
– 获取所有有java课程的人(学号,成绩) - 临时表
– 根据学号连接两个临时表:
– 学号 | 物理成绩 | 生物成绩
– 然后再进行筛选
1
2
3
4
5select A.s_id from
(select s_id, num as python from score left join course on score.c_id = course.c_id where course.c_name = 'python') as A
left join
(select s_id, num as java from score left join course on score.c_id = course.c_id where course.c_name = 'java') as B
on A.s_id = B.s_id where A.python > B.java;
查询平均成绩大于65分的同学的姓名和平均成绩(保留两位小数);
1
2
3
4
5SELECT student.s_name as names, round(AVG(score.num), 2) as average
FROM student, score
WHERE student.s_id = score.s_id
GROUP BY student.s_name
HAVING AVG(score.num) > 65;查询所有同学的姓名、选课数、总成绩
1
2
3
4
5SELECT student.s_name, COUNT(score.s_id) as course_num, SUM(score.num) as total_grades
FROM student, score
WHERE student.s_id = score.s_id
GROUP BY student.s_name
ORDER BY student.s_name;查询所有的课程的名称以及对应的任课老师姓名;
1
2
3SELECT course.c_name, teacher.t_name
FROM course, teacher
WHERE course.t_id = teacher.t_id;查询没学过“alex”老师课的同学的姓名;
1
2
3
4
5
6
7SELECT s_name
FROM student
WHERE student.s_id NOT IN (
SELECT DISTINCT score.s_id
FROM score, course, teacher
WHERE course.c_id = score.c_id AND teacher.t_id = course.t_id AND teacher.t_name = 'alex'
)查询学过’python’并且也学过编号’java’课程的同学的姓名
1
2
3
4
5
6
7SELECT s_name FROM
(SELECT score.s_id as sid, score.c_id as cid
FROM score, course
WHERE score.c_id = course.c_id AND (course.c_name = 'python' OR course.c_name = 'java')) as B
LEFT JOIN student ON B.sid = student.s_id
GROUP BY s_name
HAVING COUNT(s_name) > 1;查询学过“alex”老师所教的全部课程的同学的姓名
1
2
3
4
5
6
7
8
9
10SELECT s_name FROM
(SELECT score.s_id as sid, score.c_id as cid
FROM score, course, teacher
WHERE score.c_id = course.c_id AND teacher.t_id = course.t_id AND teacher.t_name = 'alex') as B
LEFT JOIN student ON B.sid = student.s_id
GROUP BY s_name
HAVING COUNT(s_name) = (SELECT COUNT(course.c_id) FROM
course, teacher
WHERE
teacher.t_id = course.t_id AND teacher.t_name='alex');查询挂科超过两门(包括两门)的学生姓名;
1
2
3
4
5
6
7SELECT s_name FROM
(SELECT score.s_id as sid, score.c_id as大专栏 SQL语法练习(一)span> cid
FROM score, course
WHERE score.c_id = course.c_id AND (score.num < 60 OR score.num ISNULL)) as B
LEFT JOIN student ON B.sid = student.s_id
GROUP BY s_name
HAVING COUNT(s_name) > 1;查询有课程成绩小于60分的同学的姓名;
1
2
3
4
5
6SELECT s_name FROM
(SELECT score.s_id as sid, score.c_id as cid
FROM score, course
WHERE score.c_id = course.c_id AND score.num < 60) as B
LEFT JOIN student ON B.sid = student.s_id
GROUP BY s_name;查询选修了全部课程的学生姓名;
1
2
3
4
5
6
7
8
9
10
11
12
13
14SELECT s_name FROM
(SELECT score.s_id as sid, score.c_id as cid
FROM score, course
WHERE score.c_id = course.c_id) as B
LEFT JOIN student ON B.sid = student.s_id
GROUP BY s_name
HAVING COUNT(s_name) = (SELECT COUNT(*) FROM course); SELECT s_name FROM
student
LEFT JOIN score ON student.s_id = score.s_id
GROUP BY s_name
HAVING COUNT(s_name) = (SELECT COUNT(*) FROM course)查询至少有一门课程与“貂蝉”同学所学课程相同的同学姓名;
1
2
3
4
5
6SELECT s_name
FROM student, score
WHERE student.s_id = score.s_id AND student.s_name != '貂蝉' AND score.c_id IN (SELECT c_id FROM score, student
WHERE score.s_id = student.s_id AND student.s_name = '貂蝉') GROUP BY s_name;查询学过’貂蝉’同学全部课程的其他同学姓名;
1
2
3
4
5
6
7
8
9
10
11
12SELECT s_name FROM
(SELECT score.s_id as sid, score.c_id as cid
FROM score, course, student
WHERE
score.c_id = course.c_id AND
student.s_id = score.s_id AND
student.s_name != '貂蝉' AND
score.c_id in (SELECT c_id FROM score, student WHERE student.s_name = '貂蝉' AND student.s_id = score.s_id)
) as B
LEFT JOIN student ON B.sid = student.s_id
GROUP BY s_name
HAVING COUNT(s_name) >= (SELECT COUNT(score.c_id) FROM score, student WHERE student.s_name = '貂蝉' AND student.s_id = score.s_id);
– ——————————————————————————————————————————————————————————————
– INSERT INTO student VALUES (1, ‘鲁班’, 12, ‘男’);
– INSERT INTO student VALUES (2, ‘貂蝉’, 20, ‘女’);
– INSERT INTO student VALUES (3, ‘刘备’, 35, ‘男’);
– INSERT INTO student VALUES (4, ‘关羽’, 34, ‘男’);
– INSERT INTO student VALUES (5, ‘张飞’, 33, ‘女’);
–
–
– INSERT INTO teacher VALUES (1, ‘大王’);
– INSERT INTO teacher VALUES (2, ‘alex’);
– INSERT INTO teacher VALUES (3, ‘egon’);
– INSERT INTO teacher VALUES (4, ‘peiqi’);
– INSERT INTO course VALUES (1, ‘python’, 1);
– INSERT INTO course VALUES (2, ‘java’, 2);
– INSERT INTO course VALUES (3, ‘linux’, 3);
– INSERT INTO course VALUES (4, ‘go’, 2);
– INSERT INTO score VALUES (1, 1, 1, 79);
– INSERT INTO score VALUES (2, 1, 2, 77);
– INSERT INTO score VALUES (3, 1, 3, 58);
– INSERT INTO score VALUES (4, 2, 2, 66);
– INSERT INTO score VALUES (5, 2, 3, 77);
– INSERT INTO score VALUES (6, 3, 1, 61);
– INSERT INTO score VALUES (7, 3, 2, 64);
– INSERT INTO score VALUES (8, 4, 3, 70);
SQL语法练习(一)的更多相关文章
- 值得注意的ibatis动态sql语法格式
一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...
- Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)
Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...
- SQL 语法总结
学了一个月的java,开始有入门的感觉.这段时间接触到了java的JDBC, 发现学习这部分的内容还是要有SQL的基础,于是花费了几天时间学习了一下SQL语法,并将其总结于下. 选择数据 SELECT ...
- SQL语法和运算符(一)
一个数据库通常包含一个或多个表.每个表由一个名字标识,表包含带有数据的记录(行). 一些最重要的SQL命令(SQL对大小写不敏感): 一.SQL语法 select:从数据库中提取数据 update:更 ...
- [转]MySQL 最基本的SQL语法/语句
MySQL 最基本的SQL语法/语句,使用mysql的朋友可以参考下. DDL-数据定义语言(Create,Alter,Drop,DECLARE) DML-数据操纵语言(Select,Delete ...
- SQL语法整理
SQL是Structured Query Language的缩写,中文全名为结构化查询语言,是一种用于数据存储,查询,更新和管理的关系数据库系统. SQL语法 创建表 create table tab ...
- ORACLE分页查询SQL语法——最高效的分页
--1:无ORDER BY排序的写法.(效率最高)--(经过测试,此方法成本最低,只嵌套一层,速度最快!即使查询的数据量再大,也几乎不受影响,速度依然!) SELECT * FROM (SELECT ...
- [Android新手区] SQLite 操作详解--SQL语法
该文章完全摘自转自:北大青鸟[Android新手区] SQLite 操作详解--SQL语法 :http://home.bdqn.cn/thread-49363-1-1.html SQLite库可以解 ...
- 有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎
有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎 presto,hive,drill,calcite,sparksq
- editplus的配置文件来支持sql语法高亮【转】
editplus默认是没有sql语法高亮的,原因是它的内部没有sql.stx的这样一个语法文件 我们自己在 EditPlus 的安装目录下面新建一个文件名为sql.stx,然后打开editplus ...
随机推荐
- 3.docker machine 连接 aliyun 远程docker 服务器
1.在aliyun ecs 创建docker 服务器 docker-machine create -d aliyunecs machine-aliyunecs 2.远程连接 docker 获取客户端 ...
- dubbo的重试原则
验证思路.使用超时来验证重试次数 XML 注解
- angular 父子组件传值 用get set 访问器设置默认值
private _PLACEHOLDER: string; @Input() public set placeholder(v: string) { this._PLACEHOLDER = v; } ...
- maven中scope属性有哪些
compile,缺省值,适用于所有阶段,会随着项目一起发布. provided,类似compile,期望JDK.容器或使用者会提供这个依赖.如servlet.jar. runtime,只在运行时使用, ...
- 吴裕雄--天生自然python Google深度学习框架:Tensorflow实现迁移学习
import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...
- [转载]Python方法绑定——Unbound/Bound method object的一些梳理
本篇主要总结Python中绑定方法对象(Bound method object)和未绑定方法对象(Unboud method object)的区别和联系.主要目的是分清楚这两个极容易混淆的概念,顺便将 ...
- DocCms_2016 代码审计
0x00 结构浏览 按照代码审计的惯例,拿到这款cms之后首先浏览了一下目录结构,在基本了解之后,首先进入/index.php,这里包含了两个文件:/admini/config/qd-config.p ...
- P1010 幂次方 P1022 计算器的改良
P1010 幂次方 一.题目 https://www.luogu.org/problemnew/show/P1010 二.代码 #include<bits/stdc++.h> using ...
- scala slick mysql utf8mb4 支持
语言 scala sql包 slick 3.2.0 数据库 mysql https://stackoverflow.com/questions/36741141/scala-slick-jdbc ...
- linux系统用户管理(一)
Linux系统用户管理1.用户管理*****2.Linux用户命令****3.用户创建的原理***4.密码管理***5.组命令管理**6.身份切换*****7.sudo提升权限***** 我们现在所使 ...