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

简介:

分享一些常见的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
)
复制代码

 

本博客为 木宛城主原创,基于 Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名 木宛城主(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。

本文转自木宛城主博客园博客,原文链接:http://www.cnblogs.com/OceanEyes/archive/2012/05/20/tsqlmianshi.html,如需转载请自行联系原作者
目录
打赏
0
0
0
0
20
分享
相关文章
利用 PolarDB PG 版向量化引擎,加速复杂 SQL 查询!完成任务领发财新年抱枕!
利用 PolarDB PG 版向量化引擎,加速复杂 SQL 查询!完成任务领发财新年抱枕!
云原生数据仓库AnalyticDB PostgreSQL同一个SQL可以实现向量索引、全文索引GIN、普通索引BTREE混合查询,简化业务实现逻辑、提升查询性能
本文档介绍了如何在AnalyticDB for PostgreSQL中创建表、向量索引及混合检索的实现步骤。主要内容包括:创建`articles`表并设置向量存储格式,创建ANN向量索引,为表增加`username`和`time`列,建立BTREE索引和GIN全文检索索引,并展示了查询结果。参考文档提供了详细的SQL语句和配置说明。
27 1
|
2月前
|
Java使用sql查询mongodb
通过MongoDB Atlas Data Lake或Apache Drill,可以在Java中使用SQL语法查询MongoDB数据。这两种方法都需要适当的配置和依赖库的支持。希望本文提供的示例和说明能够帮助开发者实现这一目标。
58 17
如何在 Oracle 中配置和使用 SQL Profiles 来优化查询性能?
在 Oracle 数据库中,SQL Profiles 是优化查询性能的工具,通过提供额外统计信息帮助生成更有效的执行计划。配置和使用步骤包括:1. 启用自动 SQL 调优;2. 手动创建 SQL Profile,涉及收集、执行调优任务、查看报告及应用建议;3. 验证效果;4. 使用 `DBA_SQL_PROFILES` 视图管理 Profile。
SQL做数据分析的困境,查询语言无法回答的真相
SQL 在简单数据分析任务中表现良好,但面对复杂需求时显得力不从心。例如,统计新用户第二天的留存率或连续活跃用户的计算,SQL 需要嵌套子查询和复杂关联,代码冗长难懂。Python 虽更灵活,但仍需变通思路,复杂度较高。相比之下,SPL(Structured Process Language)语法简洁、支持有序计算和分组子集保留,具备强大的交互性和调试功能,适合处理复杂的深度数据分析任务。SPL 已开源免费,是数据分析师的更好选择。
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
8月前
|
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
197 13
|
8月前
|
SQL
解锁 SQL Server 2022的时间序列数据功能
【7月更文挑战第14天】要解锁SQL Server 2022的时间序列数据功能,可使用`generate_series`函数生成整数序列,例如:`SELECT value FROM generate_series(1, 10)。此外,`date_bucket`函数能按指定间隔(如周)对日期时间值分组,这些工具结合窗口函数和其他时间日期函数,能高效处理和分析时间序列数据。更多信息请参考官方文档和技术资料。
118 9
对比 SQL Server中的VARCHAR(max) 与VARCHAR(n) 数据类型
【7月更文挑战7天】SQL Server 中的 VARCHAR(max) vs VARCHAR(n): - VARCHAR(n) 存储最多 n 个字符(1-8000),适合短文本。 - VARCHAR(max) 可存储约 21 亿个字符,适合大量文本。 - VARCHAR(n) 在处理小数据时性能更好,空间固定。 - VARCHAR(max) 对于大文本更合适,但可能影响性能。 - 选择取决于数据长度预期和业务需求。
621 1

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等