SQL查询语句47题

  1 select * from student
  2 select * from score
  3 --select * from grade
  4 select * from course
  5 select * from teacher
  6
  7 --1、 查询Student表中的所有记录的Sname、Ssex和Class列。
  8 select sname,ssex,class from Student
  9 --2、 查询教师所有的单位即不重复的Depart列。
 10 select distinct depart from teacher
 11 --3、 查询Student表的所有记录。
 12 select * from student
 13 --4、 查询Score表中成绩在60到80之间的所有记录。
 14 select * from score where degree between 60 and 80
 15 --5、 查询Score表中成绩为85,86或88的记录。
 16 select * from score where degree in (85,86,88)
 17 --6、 查询Student表中“95031”班或性别为“女”的同学记录。
 18 select * from student where class=‘95031‘ or ssex=‘女‘
 19 --7、 以Class降序查询Student表的所有记录。
 20 select * from student order by class desc
 21 --8、 以Cno升序、Degree降序查询Score表的所有记录。
 22 select * from score order by cno,degree desc
 23 --9、 查询“95031”班的学生人数。
 24 select COUNT(*) from student where class=‘95031‘
 25 --10、查询Score表中的最高分的学生学号和课程号。
 26 select sno,cno from score where degree in (select MAX(degree) from score)
 27 select top 1 sno,cno from score order by degree desc
 28 --11、查询‘3-105’号课程的平均分。
 29 select AVG(degree) from score where cno=‘3-105‘
 30 --12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
 31 select AVG(degree) from score where cno in (select cno from score group by cno having COUNT(*)>=5) and cno like ‘3%‘
 32 select AVG(degree) from score where cno like ‘3%‘ group by cno having COUNT(*)>=5
 33 --13、查询最低分大于70,最高分小于90的Sno列。
 34 select distinct sno from score
 35 where sno in (select sno from score group by sno having MIN(degree)>70)
 36     and sno in (select sno from score group by sno having MAX(degree)<90)
 37
 38 select sno from score group by sno having MAX(degree)<90 and MIN(degree)>70
 39 --14、查询所有学生的Sname、Cno和Degree列。
 40 select sname,cno,degree from student,score where student.sno=score.sno
 41 --15、查询所有学生的Sno、Cname和Degree列。
 42 select student.sno,cname,degree from student
 43     join score on student.sno=score.sno
 44     join course on score.cno=course.cno
 45 --16、查询所有学生的Sname、Cname和Degree列。
 46 select sname,cname,degree from student
 47     join score on student.sno=score.sno
 48     join course on score.cno=course.cno
 49 --17、查询“95033”班所选课程的平均分。
 50 select AVG(degree) from score
 51 where degree in (select degree from score join student on student.sno=score.sno where class=‘95033‘)
 52 --18、假设使用如下命令建立了一个grade表:
 53 --create table grade(low  int,upp  int,rank  varchar(1))
 54 --insert into grade values(90,100,‘A‘)
 55 --insert into grade values(80,89,‘B‘)
 56 --insert into grade values(70,79,‘C‘)
 57 --insert into grade values(60,69,‘D‘)
 58 --insert into grade values(0,59,‘E‘)
 59 --现查询所有同学的Sno、Cno和rank列。
 60 select sno,cno,[rank] from score,grade where degree between low and upp
 61 --19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。//无关子查询
 62             --“109”号同学成绩
 63             --select degree from score where sno=‘109‘ and cno=‘3-105‘
 64 select * from student join score on student.sno=score.sno
 65 where cno=‘3-105‘ and degree > (select degree from score where sno=‘109‘ and cno=‘3-105‘)
 66 --20、查询score中选学多门课程的同学中分数为非最高分成绩的记录。
 67 --select sno from score group by sno having COUNT(*)>1
 68 --select MAX(degree) from score group by cno
 69 select * from score a
 70 where sno in (select sno from score group by sno having COUNT(*)>1)
 71 and
 72 (degree not in (select MAX(degree) from score b where a.cno=b.cno group by cno))--相关子查询
 73
 74 --21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
 75 select * from score where degree>(select degree from score where sno=‘109‘ and cno=‘3-105‘)
 76 --22、查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
 77 select sno,sname,sbirthday from student
 78 where YEAR(sbirthday)= YEAR((select sbirthday from student where sno=‘108‘))
 79 --23、查询“张旭“教师任课的学生成绩。
 80 --select cno from course where course.tno=(select tno from teacher where tname=‘张旭‘)
 81 select degree from score where cno=(select cno from course where course.tno=(select tno from teacher where tname=‘张旭‘))
 82 --24、查询选修某课程的同学人数多于5人的教师姓名。
 83 --select cno from score group by cno having COUNT(*)>5
 84 --select tno from course where cno=(select cno from score group by cno having COUNT(*)>5)
 85 select tname from teacher
 86 where tno=(select tno from course where cno=(select cno from score group by cno having COUNT(*)>5))
 87 --25、查询95033班和95031班全体学生的记录。
 88 select student.sno,sname,ssex,sbirthday,class,course.cno,degree,cname,teacher.tno,tname,tsex,prof,depart from student
 89 join score on student.sno=score.sno
 90 join course on score.cno=course.cno
 91 join teacher on course.tno=teacher.tno
 92 where class=‘95033‘ or class=‘95031‘ order by student.sno
 93 --26、查询存在有85分以上成绩的课程Cno.
 94 select distinct cno from score where degree>=85
 95 --27、查询出“计算机系“教师所教课程的成绩表。
 96 --select tno from teacher where depart=‘计算机系‘
 97 --select cno from course where tno in (select tno from teacher where depart=‘计算机系‘)
 98 select * from score
 99 where cno in (select cno from course where tno in (select tno from teacher where depart=‘计算机系‘))
100 --28、查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。//查关子查询 exists存在
101 select * from teacher t1 where depart=‘计算机系‘ and not exists
102 (
103     select * from teacher t2 where depart=‘电子工程系‘ and t1.prof=t2.prof
104 )
105 --29、查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。
106 select cno,sno,degree from score where cno=‘3-105‘ and degree>(select MIN(degree) from score where cno=‘3-245‘) order by degree desc
107 --30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.//相关子查询
108 --select cno,sno,degree from score where cno=‘3-105‘ and degree>(select MAX(degree) from score where cno=‘3-245‘)
109 --30、查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree.//相关子查询
110 select cno,sno,degree from score s1 where  cno=‘3-105‘ and sno in
111 (
112     select sno from score where cno=‘3-245‘
113 )
114  and degree>
115 (
116     select degree from score s2 where cno=‘3-245‘ and sno in
117     (
118         select sno from score where cno =‘3-105‘
119     )
120     and s1.sno=s2.sno
121 )
122 --31、查询所有教师和同学的name、sex和birthday.
123 select sname as name,ssex as sex,sbirthday as birthday from student
124 union
125 select tname,tsex,tbirthday from teacher
126 --32、查询所有“女”教师和“女”同学的name、sex和birthday.
127 select sname as name,ssex as sex,sbirthday as birthday from student where ssex=‘女‘
128 union
129 select tname,tsex,tbirthday from teacher where tsex=‘女‘
130 --33、查询成绩比该课程平均成绩低的同学的成绩表。
131 select * from score where degree<(select AVG(degree) from score where cno in (select cno from score group by cno))
132
133 --34、查询所有任课教师的Tname和Depart.
134 select tname,depart from teacher
135 --35  查询所有未讲课的教师的Tname和Depart.
136 --select cno from course where cno not in (select cno from score)
137 select tname,depart from teacher
138 where tno in (select tno from course where cno= (select cno from course where cno not in (select cno from score)))
139
140 select tname,depart from teacher
141 --36、查询至少有2名男生的班号。
142 select class from student where ssex=‘男‘ group by class having COUNT(ssex)>=2
143 --37、查询Student表中不姓“王”的同学记录。
144 select * from student where sname not like ‘王%‘
145 --38、查询Student表中每个学生的姓名和年龄。
146 --(YEAR(GETDATE())-YEAR(sbirthday))
147 select sname,(YEAR(GETDATE())-YEAR(sbirthday)) from student
148 --39、查询Student表中最大和最小的Sbirthday日期值。
149 select MAX(sbirthday),MIN(sbirthday) from student
150 --40、以班号和年龄从大到小的顺序查询Student表中的全部记录。
151 select * from student order by class desc,sbirthday
152 --41、查询“男”教师及其所上的课程。
153 --select tname from teacher where tsex=‘男‘
154 --tsex=‘男‘ and course.tno in (select tno from teacher where tsex=‘男‘)
155 select tname,cname from teacher
156 join course on teacher.tno=course.tno
157     where tsex=‘男‘ and course.tno in (select tno from teacher where tsex=‘男‘)
158 --42、查询最高分同学的Sno、Cno和Degree列。
159 select sno,cno,degree from score where degree in (select MAX(degree) from score)
160 --43、查询和“李军”同性别的所有同学的Sname.
161 select sname from student where ssex=(select ssex from student where sname=‘李军‘)
162 --44、查询和“李军”同性别并同班的同学Sname.//也可以用相关子查询
163 select sname from student where ssex=(select ssex from student where sname=‘李军‘) and class=(select class from student where sname=‘李军‘)
164 --45、查询所有选修“计算机导论”课程的“男”同学的成绩表。
165 select * from score where cno in (select cno from course where cname=‘计算机导论‘) and sno in (select sno from student where ssex=‘男‘)
166 --46、查询score表中分数最高的学生的信息。//多层嵌套
167 select * from student where sno in (select sno from score where degree in (select MAX(degree) from score))
168 --47、查询score表中的平均分在80分以上的学生信息。//无关查询
169 --select sno from score group by sno having AVG(degree)>=80
170 select * from student
171 where sno in (select sno from score where score.sno in(select sno from score group by sno having AVG(degree)>=80))
时间: 2024-10-23 23:38:53

SQL查询语句47题的相关文章

Hibernate 关于执行sql查询语句(转)

原文  http://www.yshjava.cn/post/543.html 主题 SQLHibernate Hibernate对原生SQL查询的支持和控制是通过SQLQuery接口实现的.通过Session接口,我们能够很方便的创建一个SQLQuery(SQLQuery是一个接口,在Hibernate4.2.2之前,默认返回的是SQLQuery的实现类--SQLQueryImpl对象,在下文中出现的SQLQuery如非注明,都是指该子类)对象来进行原生SQL查询: session.creat

将Sql查询语句获取的数据插入到List列表里面

Sql查询语句获取的数据是分格式的,我们还用SqlDataReader来做,然后用IDataReader来接收读取,以下是代码: //我想查询一个用户表的信息,该用户有姓名,密码,信息三列 //1.定义一个用户类型的List数组,userInfo类的代码在下方 List<userInfo> userInfo = new List<userInfo>(); //2.我们要读取查询语句的数据,并且保存了.这里我们将使用IDataReader语句 //数据库类的实例,类的代码在下方 DB

如何编写高效的SQL查询语句

概述 如何编写性能比较高的SQL查询语句呢?两个方法:创建合理的索引:书写高效的SQL语句 索引的基本原理 索引分为聚集索引和非聚集索引.一个表只能创建一个聚集索引和N个非聚集索引,这句话的由来主要是由于索引的原理决定的. 数据库中的一张表不论你创建不创建索引,或者,不论你创建那种类型的索引,其在硬盘上的存储是一样的,那么,创建索引和不创建索引,或者,创建聚集索引和非聚集索引的区别在什么地方呢? 其区别是表内数据在内存的存在形式.对于没有创建索引的表,其加载到内存里时,就只有数据块:对于有聚集索

Java连接MySQL数据库实现用户名密码的验证方法 Java语句中sql查询语句&#39;&#39; &quot;&quot;作用

//方法一,可以验证登录,但方法不实用.package com.swift; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; public class Logi

SQL查询语句 group by后, 字符串合并

原文:SQL查询语句 group by后, 字符串合并 合并列值 --******************************************************************************************* 表结构,数据如下: id value ----- ------ 1 aa 1 bb 2 aaa 2 bbb 2 ccc 需要得到结果: id values ------ ----------- 1 aa,bb 2 aaa,bbb,ccc 即:gr

(转)经典SQL查询语句大全

一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份数据的 device USE master EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat' --- 开始 备份 BACKUP DATABASE pubs TO testBack 4.说明:创建

Java 获取SQL查询语句结果

step1:构造连接Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/db","root","123"); step2:构造语句String sql = "select username,password from t_user where

工作中常用SQL 查询语句备忘

--当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列.select (case when a>b then a else b end ), (case when b>c then b esle c end) from table_name --求和查询 create table #tmp(rq varchar(10), shengfu nchar(1)) insert into #tmp values('2005-05-09','胜') insert into #tm

EF5中 执行 sql语句使用Database.ExecuteSqlCommand 返回影响的行数 ; EF5执行sql查询语句 Database.SqlQuery 带返回值

一: 执行sql语句,返回受影响的行数 在mysql里面,如果没有影响,那么返回行数为  -1 ,sqlserver 里面  还没有测试过 using (var ctx = new MyDbContext()) { ctx.Database.ExecuteSqlCommand("UPDATE Person SET Name = 'Michael' WHERE PersonID = 1"); } 二 : Database.SqlQuery<T>   EF5执行sql查询语句