用SQL语句进行数据库查询(复杂查询)

简介: 用SQL语句进行数据库查询(复杂查询)

本篇可当做例题练习,


1.查询比”林红”年纪大的男学生信息


语句:


select *
from Student
where Sex='男' and 
  year(Birth)-(select year(Birth)from Student--这里是需要告诉查询的表名,相当于嵌套
  where Sname='林红')<0


2.检索所有学生的选课信息,包括学号、姓名、课程名、成绩,性别.


语句:


select sc.sno,sname, course.Cno,Cname,Grade,Sex
--这里如果两个表中都有同一个属性,则需要标明在哪个表,如sc.sno
from student,sc,Course
where student.Sno=sc.Sno and Sc.Cno=course.Cno


3.查询已经选课的学生的学号、姓名、课程名、成绩.


语句:


select sc.sno ,sname , Cname , Grade
from student s , course c, sc
where s.sno=sc.sno and c.cno=sc.cno


4.查询选修了“C语言程序设计”的学生的学号与姓名


–a.用内连接查询


语句:


select sc.Sno,sname from student inner join sc on
student.Sno=sc.Sno inner join course on sc.Cno =course.cno
and Cname='C语言程序设计'


–b.用连接查询


语句:


select sc.Sno,sname from student,sc,course where
student .Sno=sc.Sno and sc.Cno =course.cno
and Cname='C语言程序设计'


–c.用子查询


语句:


select Sno,sname from student where Sno in
(select Sno from sc where Cno=
(select cno from course where Cname ='C语言程序设计'))


5.查询与”张虹”在同一个班级的学生学号、姓名、家庭住址


–a.用连接查询


语句:


select a.Sno,a.sname,a.Home_addr from student a,student b 
where a.Classno =b.Classno and b.Sname ='张虹' and a.Sname!='张虹'


–b.用子查询


语句:


select Sno,sname,Home_addr  from student where
classno=(select classno from student where sname='张虹')
and sname!='张虹'


6.查询其他班级中比”051”班所有学生年龄大的学生的学号、姓名


代码1:


select Sno,sname,Home_addr  from student where
classno!='051' and Birth<all (select Birth  from student where classno='051')


代码2:


select Sno,sname,Home_addr  from student where
classno!='051' and Birth<(select min(Birth)  from student where classno='051')


7.(选作)查询选修了全部课程的学生姓名。本题使用除运算的方法。


–由题意可得另一种语言,没有一个选了课的学生没有选course表里的课程。那么,我们需要两个NOT EXISTS表示双重否定;


语句:


select Sname from student
where not exists (
select * from course
where not exists (
select * from sc
where sno=student. sno
and cno=Course.cno))


8.查询至少选修了学生“20110002”选修的全部课程的学生的学号,姓名。


语句:


select Sno, Sname from student
where sno in (
select distinct sno from sc as sc1
where not exists (
select * from sc as sc2 where sc2.sno='20110002'
and not exists (
select * from sc as sc3 where sc3.Sno=sc1.sno and
sc3.cno=sC2.cno) )
)


9.检索选修了“高数”课且成绩至少高于选修课程号为“002"课程的学生的学号、课程号、成绩,并按成绩从高到低排列。


语句:


select sc.Sno, sc.cno , grade from sc where
grade >all(select grade from sc where cno='002' ) and
Cno= (select Cno
from course where Cname='高数')
order by Grade desc


10.检索选修了至少3门以上课程的学生的学号、总成绩(不统计不及格的成绩),并要求按总成绩降序排列。


语句:


select sno,SUM(grade) from sc where sno in (select Sno from sc group by sno
having COUNT(*)>=3) and Grade>=60 group by sno
order by SUM (grade) desc


11.检索多于3名学生选修的并以3结尾的课程号的平均成绩。


语句:


select avg(Grade) as 平均成绩
from sc
where Cno like '%3' group by cno
having count (Cno)>3


12.检索最高分与最低分之差大于5分的学生的学号、姓名、最高分、最底分。


select distinct sc.sno 学号,sname 姓名,
max (grade) as最高分,min (grade) as最低分
from student,sc
where sc.sno=student.Sno group by sc.sno , Sname
having max(grade) -min (grade) >5


13.创建一个表Student_other,结构同student,输入若干记录,部分记录和student表中的相同。


–创建过程:


create table student__other (
Sno char (8) primary key,
Sname varchar (8) not null,
sex char(2) not null,
Birth smalldatetime not null,
Classno char (3) not null,
Entrance_date smalldatetime not null,
Home_addr varchar (40) ,
sdept char (2) not null,
Postcode char (6)
)


随意插入几条student表中没有的数据:



–a.查询同时出现在Student表和student_other表中的记录


语句:


select * from student__other so ,student s
where so.sno=s.sno


----b.查询Student表和Student_other表中的全部记录


代码:


select * from student
union
select * from student__other
目录
相关文章
|
5月前
|
SQL 机器学习/深度学习 人工智能
从“写SQL”到“聊数据”:NL2SQL如何用自然语言解锁数据库?
本文系统性地阐述了自然语言转SQL(NL2SQL) 技术如何让非技术背景的业务分析师实现数据自助查询,从而提升数据驱动决策的效率与准确性。
从“写SQL”到“聊数据”:NL2SQL如何用自然语言解锁数据库?
|
4月前
|
SQL 监控 关系型数据库
一键开启百倍加速!RDS DuckDB 黑科技让SQL查询速度最高提升200倍
RDS MySQL DuckDB分析实例结合事务处理与实时分析能力,显著提升SQL查询性能,最高可达200倍,兼容MySQL语法,无需额外学习成本。
|
4月前
|
SQL 存储 关系型数据库
MySQL体系结构详解:一条SQL查询的旅程
本文深入解析MySQL内部架构,从SQL查询的执行流程到性能优化技巧,涵盖连接建立、查询处理、执行阶段及存储引擎工作机制,帮助开发者理解MySQL运行原理并提升数据库性能。
|
4月前
|
SQL 人工智能 Linux
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
499 5
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
|
3月前
|
SQL 存储 监控
SQL日志优化策略:提升数据库日志记录效率
通过以上方法结合起来运行调整方案, 可以显著地提升SQL环境下面向各种搜索引擎服务平台所需要满足标准条件下之数据库登记作业流程综合表现; 同时还能确保系统稳健运行并满越用户体验预期目标.
292 6
|
4月前
|
SQL 监控 关系型数据库
SQL优化技巧:让MySQL查询快人一步
本文深入解析了MySQL查询优化的核心技巧,涵盖索引设计、查询重写、分页优化、批量操作、数据类型优化及性能监控等方面,帮助开发者显著提升数据库性能,解决慢查询问题,适用于高并发与大数据场景。
|
5月前
|
SQL XML Java
通过MyBatis的XML配置实现灵活的动态SQL查询
总结而言,通过MyBatis的XML配置实现灵活的动态SQL查询,可以让开发者以声明式的方式构建SQL语句,既保证了SQL操作的灵活性,又简化了代码的复杂度。这种方式可以显著提高数据库操作的效率和代码的可维护性。
378 18
|
3月前
|
SQL 关系型数据库 MySQL
(SQL)SQL语言中的查询语句整理
查询语句在sql中占了挺大一部分篇幅,因为在数据库中使用查询语句的次数远多于更新与删除命令。而查询语句比起其他语句要更加的复杂,可因为sql是数据库不可或缺的一部分,所以即使不懂,也必须得弄懂,以上。
284 0
|
5月前
|
SQL 人工智能 Java
用 LangChain4j+Ollama 打造 Text-to-SQL AI Agent,数据库想问就问
本文介绍了如何利用AI技术简化SQL查询操作,让不懂技术的用户也能轻松从数据库中获取信息。通过本地部署PostgreSQL数据库和Ollama模型,结合Java代码,实现将自然语言问题自动转换为SQL查询,并将结果以易懂的方式呈现。整个流程简单直观,适合初学者动手实践,同时也展示了AI在数据查询中的潜力与局限。
725 8
|
5月前
|
SQL 人工智能 Linux
SQL Server 2025 RC0 发布 - 从本地到云端的 AI 就绪企业数据库
SQL Server 2025 RC0 发布 - 从本地到云端的 AI 就绪企业数据库
331 5