前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >你必须掌握的一些常见的SQL语句,包含单表查询、高级查询(连接查询、复合条件查询、嵌套查询)

你必须掌握的一些常见的SQL语句,包含单表查询、高级查询(连接查询、复合条件查询、嵌套查询)

作者头像
用户1161731
发布2018-01-11 11:55:42
2.6K0
发布2018-01-11 11:55:42
举报
文章被收录于专栏:木宛城主木宛城主木宛城主

分享一些常见的SQL语句,包含单表查询、高级查询(连接查询、复合条件查询、嵌套查询等)。

--建立学生信息表Students
create table Students
(
    SId char(5) not null primary key,
    SName nvarchar(20) unique,
    SGender char(10) default('Male'),
    SAge int,
    SSdept nvarchar(250)
    
)
--课程表
create table Course
(
    CNo Char(4) not null primary key,
    CName nvarchar(50),
    CPNo char(4),
    CCreadit smallint
    foreign key(cpno) references course(cno)
    
    
)
--学生选课表
create table StudentCourse
(
    SCId char(5) not null ,
    SCCNo char(4) not null,
    SCGrade smallint,
    primary key(Scid,sccno),
    foreign key(SCId) references Students(Sid),
    foreign key(sccno) references Course(cno)
)

--查询每个系的学生人数
select COUNT(*) as '人数',SSdept as '所在系' 
from Students group by SSdept
--查询计算机系男女生人数
select COUNT(*) as '人数',SGender 性别 
from Students where ssdept='计算机科学与技术' group by SGender
--查询每个系男女生人数
select COUNT(*) as '人数',SSdept as '系',SGender as '性别' 
from students group by ssdept,SGender
--查询男生人数超过1的系
select ssdept as '所在系',COUNT(*) as '人数'
from Students where SGender='Male' group by ssdept having COUNT(*)>2
--查询和张三同一系的学生学号、姓名
select SId as '学号' ,SName as '姓名'
from Students 
where SSdept=(select ssdept from Students where SName='张三') and SName<>'张三'
--查询比张三年纪大的学生的姓名、性别
select SName as '姓名',SGender as '性别' 
from Students where SAge>(select sage from students where sname='张三')

--查询张三的学号和其选修的课程号和成绩
select sc.SCCNo as '课程号',sc.SCGrade as '成绩'
from students as s,Course c,StudentCourse sc
where s.SId=sc.SCId and sc.SCCNo=c.CNo and s.SName='张三'

--查询张三选修高等数学上这门课的成绩

select sc.SCCNo as '课程号',c.CName as '课程名',sc.SCGrade as '成绩'
from students as s,Course c,StudentCourse sc
where s.SId=sc.SCId and sc.SCCNo=c.CNo and c.CName='高等数学上' and s.SName='张三'

--查询与张三一样大的学生姓名,性别,年龄。
select SName as '姓名',SGender as '性别',SAge as '年龄' from Students
where SAge=(select SAge from Students where SName='张三') and SName<>'张三'

--查询选修了高等数学上的学生的学号、姓名
select s.SId as '学号',s.SName as '姓名'
from students as s,Course c,StudentCourse sc
where s.SId=sc.SCId and sc.SCCNo=c.CNo and c.CName='高等数学上'

--查询张三选修的所有课程号、课程名称、成绩
select sc.SCCNo as '课程号',c.CName as '课程名',sc.SCGrade as '成绩'
from students as s,Course c,StudentCourse sc
where s.SId=sc.SCId and sc.SCCNo=c.CNo  and s.SName='张三'

--查询学习了张三选修某门课程的学生学号、姓名、年龄
select * from Students s1,StudentCourse sc1 where sc1.SCCNo in
(
    select sc.SCCNo from Students s,StudentCourse sc
    where sc.SCId=s.SId and s.SName='张三' 
)
and  sc1.SCId=s1.SId

--查询张三选修的所有课程的课程号、课程名称

select sc.SCCNo as '课程号',c.CName as '课程名' from Students s,Course c,StudentCourse sc
where s.SId=sc.SCId and sc.SCCNo=c.CNo and s.SName='张三'
--查询比张三年龄大的学生学号、姓名
select SId as '学号',SName as '姓名' from Students 
where SAge>(select SAge from Students where SName='张三')

--查询选修每门课程中成绩小于其平均成绩的学生学号
select sc1.SCId as '学生学号' from StudentCourse sc1 where SCGrade<
(
  select AVG(SCGrade) from StudentCourse sc2 where sc2.SCCNo=sc1.SCCNo
)

--查询张三选修高等数学上的课程的成绩
select * from StudentCourse  sc ,Students s,Course c
where sc.SCId=s.SId and sc.SCCNo=c.CNo and s.SName='张三' and c.CName='高等数学上'

select * from StudentCourse where SCCNo=
(
    Select Cno from Course where CName='高等数学上'
)
and
SCId=
(
    select sid from Students where  SName='张三'

)

--查询张三选修课程的平均成绩
select AVG(sc.SCGrade) as '平均成绩' from StudentCourse  sc ,Students s
where sc.SCId=s.SId and s.SName='张三'

--查询选修课程的平均成绩小于张三平均成绩的学生学号
    
 select sC.SCId from StudentCourse sc group by sc.SCId having AVG(SC.SCGrade)< 
 (
    
 select AVG(sc.SCGrade) from StudentCourse sc ,Students s where sc.SCId=s.SId and s.SName='张三'
 )

--查询课程的平均成绩低于张三平均成绩的课程号

 select sC.SCCNo from StudentCourse sc group by sc.SCCNo having AVG(SC.SCGrade)< 
 (
    
         select AVG(sc.SCGrade) from StudentCourse sc ,Students s where sc.SCId=s.SId and s.SName='张三'
 )
 
--查询选修课程成绩大于等于该课程平均成绩的学生学号
 
select SCId from StudentCourse sc1 where  sc1.SCGrade >=
(
     select avg( sc2.SCGrade ) from StudentCourse sc2  where sc2.SCCNo=sc1.SCCNo
)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-05-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 分享一些常见的SQL语句,包含单表查询、高级查询(连接查询、复合条件查询、嵌套查询等)。
相关产品与服务
云数据库 SQL Server
腾讯云数据库 SQL Server (TencentDB for SQL Server)是业界最常用的商用数据库之一,对基于 Windows 架构的应用程序具有完美的支持。TencentDB for SQL Server 拥有微软正版授权,可持续为用户提供最新的功能,避免未授权使用软件的风险。具有即开即用、稳定可靠、安全运行、弹性扩缩等特点。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档