sql语句联系1

-- 本题目的表结构
--Student(sno,name,age,sex)  学生信息
--Course(cno,name,times)  课程表 
--SC(sno,cno,score)  成绩表 
--Teacher(tno,name)   教师表

 CREATE TABLE Student 学生信息
   ( 
      sno    INT,  --学号
      name nvarchar(32),--姓名 
      age  INT, --年龄
      sex  nvarchar(8)--性别 
   ) 
 
 CREATE TABLE Course 课程表 
   ( 
      cno    INT, --课程编号
      name nvarchar(32), --课程名
      times    INT --课时
   ) 
 
 CREATE TABLE Sc 成绩表
   ( 
      sno    INT, --学号
      cno    INT, --课程编号
      score INT --分数
   ) 
 
 CREATE TABLE Teacher 教师表
   ( 
      tno    INT,  --教师编号
      name nvarchar(16)  --教师姓名
   )
   
   --插入测试数据
   insert into Student
   select 1,N'刘一',18,N'男' union all
   select 2,N'钱二',19,N'女' union all
   select 3,N'张三',17,N'男' union all
   select 4,N'李四',18,N'女' union all
   select 5,N'王五',17,N'男' union all
   select 6,N'赵六',19,N'女' 
   
   insert into Teacher 
  select 1,N'叶平' union all
  select 2,N'贺高' union all
  select 3,N'杨艳' union all
  select 4,N'周磊'
  
  insert into Course 
  select 1,N'语文',1 union all
  select 2,N'数学',2 union all
  select 3,N'英语',3 union all
  select 4,N'物理',4
  
  insert into Sc 
  select 1,1,56 union all 
  select 1,2,78 union all 
  select 1,3,67 union all 
  select 1,4,58 union all 
  select 2,1,79 union all 
  select 2,2,81 union all 
  select 2,3,92 union all 
  select 2,4,68 union all 
  select 3,1,91 union all 
  select 3,2,47 union all 
  select 3,3,88 union all 
  select 3,4,56 union all 
  select 4,2,88 union all 
  select 4,3,90 union all 
  select 4,4,93 union all 
  select 5,1,46 union all 
  select 5,3,78 union all 
  select 5,4,53 union all 
  select 6,1,35 union all 
  select 6,2,68 union all 
  select 6,4,71
  
  




--以下为测试题目
--(1)查询课程1比课程2成绩低的学生的学号;
select sno from Sc where cno=1 and score<all(select score from Sc where cno=2)
--(2)查询平均成绩小于60分的同学的学号和平均成绩;
select sno,AVG(score) from Sc  group by sno having AVG(score)<60
--(3)查询所有同学的学号、姓名、选课数、总成绩;
select sc.sno as 学号,name as 姓名,SUM(score) as 总成绩,COUNT(cno) as 选课数 from Sc,Student 
where sc.sno=Student.sno group by sc.sno,name
--(4)查询姓“李”的老师的个数;
select * from Teacher where name like '李%'
--(5)查询没学过“叶平”老师课的同学的学号、姓名;
select distinct x.sno,name from Sc x,Student y where x.sno not in(
select sno from Sc where cno=(select tno from Teacher where name='叶平')
) and x.sno=y.sno

--(6)查询学过1并且也学过编号2课程的同学的学号、姓名;
select distinct x.sno,name from Sc x,Student y where x.sno=y.sno and x.sno in
(select x.sno from Sc x,Sc y where x.cno='1' and y.cno='2' and x.sno=y.sno) 
--(7)查询学过“贺高”老师所教的所有课的同学的学号、姓名;
select distinct x.sno,name from Sc x,Student y where x.sno in(
select sno from Sc where cno=(select tno from Teacher where name='贺高')
) and x.sno=y.sno
--(8)查询课程编号2的成绩比课程编号1课程低的所有同学的学号、姓名;
select x.sno,name from Sc x,Sc y, Student where x.sno=y.sno and x.cno=2 and y.cno=1
and x.score<y.score and x.sno=Student.sno
--(9)查询有课程成绩小于60分的同学的学号、姓名;
select x.sno,name,score from Sc x, Student where score<60 and x.sno=Student.sno
--(10)查询没有学全所有课的同学的学号、姓名;
select x.sno,name from Student ,Sc x where x.sno=Student.sno group by x.sno,name having
COUNT( x.cno)<(select COUNT(1) from Course) 

--(11)查询至少有一门课与学号为1的同学所学相同的同学的学号和姓名;
select distinct x.sno ,name from Sc x,Student where x.sno=Student.sno 
and x.cno in (select cno from Sc where sno='1')

--(12)查询和2号的同学学习的课程完全相同的其他同学学号和姓名;
select distinct x.sno ,name from Sc x,Student where x.sno=Student.sno 
and x.cno in (select cno from Sc where sno='2') group by x.sno,name
having COUNT(x.cno)=(select COUNT(1) from Sc where sno='1')
--(14)按平均成绩从低到高显示所有学生的“语文”、“数学”、“英语”三门的课程成绩;
select a.sno ,a.score as 语文 ,b.score as 数学,c.score as 英语,
d.score as 物理,AVG(a.score) as 平均分 from Sc a,Sc b,Sc c,Sc d where a.cno='1'and b.cno='2'and c.cno='3'
and d.cno='4' and a.sno=b.sno and b.sno=c.sno and c.sno=d.sno group by a.sno,a.score,b.score
,c.score,d.score order by AVG(a.score)
--(15)查询各科成绩最高和最低的分,显示列为:课程ID,最高分,最低分;
select cno as 课程ID, MAX(score) as 最高分,MIN(score) as 最低分 from Sc group by cno

--(16)按各科平均成绩从高到低和及格率的百分数从高到低顺序;
select cno, AVG(score),100 * SUM(CASE WHEN  isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) from 
Sc group by cno order by AVG(score) desc

--(17)统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]
select x.cno,name,SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100 - 85]
 ,SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85-70]
 ,SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [60 - 70]
 ,SUM(CASE WHEN score< 60 THEN 1 ELSE 0 END) AS [<60] from Sc x,Course where x.cno=Course.cno group by x.cno,name

--(18)查询同名同姓学生名单,并统计同名人数;
select * from Student w where exists(select name from Student where name=w.name group by name having COUNT(*)>1)
select name, COUNT(*) from Student group by name having COUNT(*)>1



  drop table Student
  drop table Course
  drop table Sc
  drop table Teacher
SQL是高级的非过程化编程语言,是沟通数据库服务器和客户端的重要工具,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以,具有完全不同底层结构的不同数据库系统,可以使用相同的SQL语言作为数据输入与管理的SQL接口。 它以记录集合作为操作对象,所有SQL语句接受集合作为输入,返回集合作为输出,这种集合特性允许一条SQL语句的输出作为另一条SQL语句的输入,所以SQL语句可以嵌套,这使它具有极大的灵活性和强大的功能,在多数情况下,在其他语言中需要一大段程序实现的功能只需要一个SQL语句就可以达到目的,这也意味着用SQL语言可以写出非常复杂的语句。    结构化查询语言(Structured Query Language)最早是IBM的圣约瑟研究实验室为其关系数据库管理系统SYSTEM R开发的一种查询语言,它的前身是SQUARE语言。SQL语言结构简洁,功能强大,简单易学,所以自从IBM公司1981年推出以来,SQL语言得到了广泛的应用。如今无论是像Oracle、Sybase、DB2、Informix、SQL Server这些大型的数据库管理系统,还是像Visual Foxpro、PowerBuilder这些PC上常用的数据库开发系统,都支持SQL语言作为查询语言。    美国国家标准局(ANSI)与国际标准化组织(ISO)已经制定了SQL标准。ANSI是一个美国工业和商业集团组织,负责开发美国的商务和通讯标准。ANSI同时也是ISO和International Electrotechnical Commission(IEC)的成员之一。ANSI 发布与国际标准组织相应的美国标准。1992年,ISO和IEC发布了SQL国际标准,称为SQL-92。ANSI随之发布的相应标准是ANSI SQL-92。ANSI SQL-92有时被称为ANSI SQL。尽管不同的关系数据库使用的SQL版本有一些差异,但大多数都遵循 ANSI SQL 标准。SQL Server使用ANSI SQL-92的扩展集,称为T-SQL,其遵循ANSI制定的 SQL-92标准。    SQL语言包含4个部分:    数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句。    数据操作语言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句。    数据查询语言(DQL),例如:SELECT语句。    数据控制语言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句。    SQL语言包括三种主要程序设计语言类别的语句:数据定义语言(DDL),数据操作语言(DML)及数据控制语言(DCL)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值