select * from Student
select * from Class
select * from Score
select * from Subject --1、查询出和张三住在同一个地方的学生信息
select * from Student where StuAddress=
(select StuAddress from Student where StuName='张三') --2、查询年龄大于李四的学生信息
select * from Student where StuAge>
(select StuAge from Student where StuName='李四') --3、查询和张三在同一个班级的学生信息
select * from Student where ClassID=
(select ClassID from Student where StuName='张三') --4、查询出所有脱产班的学生信息
select * from Student where ClassID in
(select ClassID from Class where ClassType='脱产') --5、查询出没有参加过任何考试的学生信息
select * from Student where StuID not in
(select StuID from score) --6、把和张三在同一个班级的所有学生的JAVA成绩加10分
update Score set Marks+=10 where StuID in
(select StuID from Student where ClassID=
(select ClassID from Student where StuName='张三')
)
and SubId=(select SubId from Subject where SubName='JAVA') --7、查询比张三的JAVA成绩好的学生信息
select * from Student where StuID in
(select StuID from Score where Marks>
(select Marks from Score where StuID=(select StuID from Student where StuName='张三')
and SubId=(select SubId from Subject where SubName='JAVA')
)
) --8、查询比张三的JAVA成绩好的学生所在的班级的信息
select * from Class where ClassID in
(select ClassID from Student where StuID in
(select StuID from Score where Marks>
(select Marks from Score where StuID=(select StuID from Student where StuName='张三')
and SubId=(select SubId from Subject where SubName='JAVA')
)
)
) --9、查询比S1T21班所有学生年龄大的“周末班”的学员信息
select * from Student where StuID in
(select StuID from Student where StuAge>
(select max(StuAge) from Student where ClassID in
(select ClassID from Class where ClassType='周末'
)
)
) --10、查询统计出每个脱产班的班级编号、班级类型、开班日期、JAVA平均分、C#平均分、SQL平均分、HTML平均分
select c.ClassID 班级编号,c.ClassType 班级类型,c.ClassDate 班级日期 ,
avg(case SubName when 'JAVA' then Marks end) JAVA平均分,
avg(case SubName when 'C#' then Marks end) C#平均分,
avg(case SubName when 'SQL' then Marks end) SQL平均分,
avg(case SubName when 'HTML' then Marks end) HTML平均分,
avg(Marks) 平均分
from Student st
inner join Class c on st.ClassID=c.ClassID
inner join Score sc on sc.StuID=st.StuID
inner join Subject su on su.SubId=sc.SubId
where c.ClassType='脱产'
group by c.ClassID,c.ClassType,c.ClassDate --11、查询出JAVA成绩比S1T21班JAVA成绩最高分要高的学员信息
select * from Student where StuID in(
select StuID from Score where Marks>(
select max(Marks) from Score where SubId=(select SubId from Subject where SubName='JAVA')
and StuID in(select StuID from Student where ClassID='S1T21')
)
and SubId=(select SubId from Subject where SubName='JAVA')
) --12、查询出JAVA成绩没有及格的学员信息
select * from Student where StuID in(
select StuID from Score where Marks<60
and SubId=(select SubId from Subject where SubName='JAVA')
)
--13、查询出姓“王”的学生的JAVA成绩和C#成绩
select * from Score where StuID in (
select StuID from Student where StuName like '王%'
) select st.StuName 姓名,
(case SubName when 'JAVA' then Marks end) JAVA成绩,
(case SubName when 'C#' then Marks end) C#成绩
from Score sc
inner join Student st on sc.StuID=st.StuID
inner join Subject su on sc.SubId=su.SubId
where st.StuID in (
select StuID from Student where StuName like '王%'
)
group by st.StuName,(case SubName when 'JAVA' then Marks end),(case SubName when 'C#' then Marks end) --14、SQLSERVER分页查询:
--A、查询出学生信息表中第1-3条信息(第一页);
select top 3 * from Student where StuID not in(
select top ((1-1)*3) StuID from Student
)
--B、查询出学生信息表中第4-6条信息(第二页);
select top 3 * from Student where StuID not in(
select top ((2-1)*3) StuID from Student
)
--15、查询没有参加过JAVA考试的学生所在的班级编号、班级类型、开班日期
select st.StuName ,c.ClassID,c.ClassType,c.ClassDate from Student st
inner join Class c on c.ClassID= st.ClassID
where StuID not in (
select StuID from Score where SubId=(select SubId from Subject where SubName='JAVA')
) --16、查询出和“张三”在同一个班的班级人数,平均年龄
select ClassID,count(*) 班级人数,avg(StuAge) 平均年龄 from Student where ClassID=(
select ClassID from student where StuName='张三'
)
group by ClassID --17、查询出学生姓名、班级编号、考试科目名称、考试成绩(使用连接查询)
select st.StuName,st.ClassID,su.SubName,sc.Marks from Score sc
inner join Student st on sc.StuID=st.StuID
inner join Subject su on sc.SubId=su.SubId --18、查询出班级人数最多的班级编号、班级类型、开班日期
select * from Class where ClassID in (
select ClassID from Student group by ClassID having
count(*)=(select top 1 count(*) num from Student group by ClassID order by num desc)
) --19、由于张三、李四、王五三名同学JAVA考试作弊,现将三名同学的成绩作废(删除)
delete from Score where StuID in (select StuID from Student where StuName in ('张三','李四','王五'))
and SubId=(select SubId from Subject where SubName='JAVA') --20、由于S1T23班参加的C#考试难度大,现将该班每个学生的C#成绩加10分
update Score set Marks+=10 where StuID in (select StuID from Student where ClassID='S1T23')
and SubId=(select SubId from Subject where SubName='C#') --21、查询出年龄大于25并且家住湖北襄樊的学生的编号、姓名、班级编号、班级类型、开班日期
select st.StuID,st.StuName,st.ClassID,c.ClassType,c.ClassDate from Student st
inner join Class c on st.ClassID=c.ClassID
where StuID in (
select StuID from Student where StuAge>25 and StuAddress='湖北襄樊'
) --22、查询统计出2010-1-1以前开班的学生人数和平均年龄
select count(*) 人数,avg(StuAge) 平均年龄 from Student where ClassID in (
select ClassID from Class where ClassDate<'2010-1-1'
) --23、查询出每个班级JAVA成绩不及格的人数
select ClassID,count(StuID) JAVA成绩不及格的人数 from Student where StuID in (
select StuID from Score where Marks<60
and SubId=(select SubId from Subject where SubName='JAVA')
)
group by ClassID select c.ClassID,
count(case when SubName='JAVA' and Marks<60 then Marks end) JAVA成绩不及格的人数
from Student st
inner join Class c on st.ClassID=c.ClassID
inner join Score sc on sc.StuID=st.StuID
inner join Subject su on su.SubId=sc.SubId
group by c.ClassID --24、查询出每个班级没有参加JAVA考试的学生人数
select ClassID,count(*) 没有参加JAVA考试的学生人数 from Student where StuID not in (
select StuID from Score where SubId=(select SubId from Subject where SubName='JAVA')
)
group by ClassID --25、根据学生的JAVA课程的分数,来评定级别:
--成绩<60 :★
--60<=成绩<70:★★
--70<=成绩<80:★★★
--80<=成绩<90:★★★★
--90<=成绩<=100:★★★★★
--并且显示评定的等级信息 select st.StuName 学生姓名,sc.Marks 学生JAVA成绩,
(case when Marks<60 then '★'
when Marks>=60 and Marks<70 then '★★'
when Marks>=80 and Marks<90 then '★★★★'
when Marks>=90 and Marks<=100 then '★★★★★'
else '×' end) JAVA课程的分数评级
from Score sc
inner join Student st on sc.StuID=st.StuID
where SubId=(select SubId from Subject where SubName='JAVA')

Sql 语法练习的更多相关文章

  1. 值得注意的ibatis动态sql语法格式

    一.Ibatis常用动态sql语法,简单粗暴用一例子 <select id="iBatisSelectList" parameterClass="java.util ...

  2. Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等)

    Linq to SQL 语法查询(链接查询,子查询 & in操作 & join,分组统计等) 子查询 描述:查询订单数超过5的顾客信息 查询句法: var 子查询 = from c i ...

  3. SQL 语法总结

    学了一个月的java,开始有入门的感觉.这段时间接触到了java的JDBC, 发现学习这部分的内容还是要有SQL的基础,于是花费了几天时间学习了一下SQL语法,并将其总结于下. 选择数据 SELECT ...

  4. SQL语法和运算符(一)

    一个数据库通常包含一个或多个表.每个表由一个名字标识,表包含带有数据的记录(行). 一些最重要的SQL命令(SQL对大小写不敏感): 一.SQL语法 select:从数据库中提取数据 update:更 ...

  5. [转]MySQL 最基本的SQL语法/语句

    MySQL 最基本的SQL语法/语句,使用mysql的朋友可以参考下.   DDL-数据定义语言(Create,Alter,Drop,DECLARE) DML-数据操纵语言(Select,Delete ...

  6. SQL语法整理

    SQL是Structured Query Language的缩写,中文全名为结构化查询语言,是一种用于数据存储,查询,更新和管理的关系数据库系统. SQL语法 创建表 create table tab ...

  7. ORACLE分页查询SQL语法——最高效的分页

    --1:无ORDER BY排序的写法.(效率最高)--(经过测试,此方法成本最低,只嵌套一层,速度最快!即使查询的数据量再大,也几乎不受影响,速度依然!) SELECT * FROM (SELECT  ...

  8. [Android新手区] SQLite 操作详解--SQL语法

    该文章完全摘自转自:北大青鸟[Android新手区] SQLite 操作详解--SQL语法  :http://home.bdqn.cn/thread-49363-1-1.html SQLite库可以解 ...

  9. 有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎

    有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎 presto,hive,drill,calcite,sparksq

  10. editplus的配置文件来支持sql语法高亮【转】

      editplus默认是没有sql语法高亮的,原因是它的内部没有sql.stx的这样一个语法文件 我们自己在 EditPlus 的安装目录下面新建一个文件名为sql.stx,然后打开editplus ...

随机推荐

  1. error_reporting函数引起的error_log配置失效的问题

    由于项目代码中大量使用了error_reporting(0);导致php.ini中的error_log失效,不记录错误日志, 导致调试起来非常不便,耗费大量的时间,所以在php.ini的配置中禁止掉e ...

  2. springboot-定时任务-多线程

    1.配置异步线程池 import java.util.concurrent.Executor; import org.springframework.context.annotation.Bean; ...

  3. 差分约束算法————洛谷P4878 [USACO05DEC] 布局

    题目: 不难看出题意主要是给出ml+md个格式为xi-xj<=ak的不等式,xi-xj为i,j俩头牛的距离,要我们求x1-xn的最大值. 经过上下加减我们可以将这几个不等式化成x1-xn< ...

  4. edusoho twig 引入文件功能

    在这里不得不提 edusoho twig 模板引擎了 跟smarty 比较类似 不过感觉还是更好一点儿 这里用的标签就只有一个 {% include '路径/文件名' %} 大家在首页做的改动比较多 ...

  5. Union All 使用注意事项和字段顺序不一致导致的异常

    使用注意,先说结果: UNION 操作符用于合并两个或多个 SELECT 语句的结果集,这里需要注意的是: UNION 内部的 SELECT 语句必须拥有相同数量的列: 列也必须拥有相似的数据类型(实 ...

  6. #论文阅读# Universial language model fine-tuing for text classification

    论文链接:https://aclweb.org/anthology/P18-1031 对文章内容的总结 文章研究了一些在general corous上pretrain LM,然后把得到的model t ...

  7. 浪潮服务器NF84260M3安装Windows server 2012 R2 RAID配置

    这里是已经做了RAID6,再做系统 浪潮服务器NF84260M3 U盘启动,光盘刻录 Windows server 2012 R2 镜像,地址:迅雷下载,ed2k://|file|cn_windows ...

  8. [转帖]System Dynamic Management Views

    System Dynamic Management Views https://docs.microsoft.com/en-us/sql/relational-databases/system-dyn ...

  9. 使用pycharm开发web——django2.1.5(五)表单和通用视图

    看了刘江老师教程这么多天,卧槽,我才发现他也曾跻身于行伍之间,interesting 刘老师这波讲解很到位,告诉你如何编写单例视图的时候忽然告诉你,其实不用这么麻烦,我们有通用视图,那些总是要做相似的 ...

  10. 异常处理 try

    语法错误 这种错误的不能使用异常处理,你自己粗心写错怪谁,哼哼哼 比如说少冒号啦,丢了括号啦 逻辑错误 try: num = int(input("请输入数字")) print(1 ...