SQL语句实例详解:查询、插入、更新、删除及聚合函数应用
SQL语句实例详解:查询、插入、更新、删除及聚合函数应用
本文将通过13个SQL语句实例,详细讲解数据库查询、插入、更新、删除操作,并介绍聚合函数的使用,涵盖内连接、外连接、嵌套查询、条件语句等技巧,并对SQL语句的语法结构进行总结,帮助读者理解SQL语句的基本操作和应用场景。
1. 数据插入
select sno,sname,ssex,sage
into JSJSTUDENT
from Student
select*
from JSJSTUDENT
该语句将Student表中的sno、sname、ssex、sage列数据插入到新表JSJSTUDENT中。
2. 数据插入(临时表)
select top 3*
into #student
from student
order by sage desc
select *
from #student
该语句将student表中按sage降序排列的前3条数据插入到临时表#student中。
3. 内连接与派生查询
-- 显内
select sc.sno,sname,ssex,grade,cname
from Student inner join sc on Student.sno=sc.sno ,Course
where sdept='计算机系' and Course.cno=Sc.cno
-- 派生
select student.sno,sname,ssex,cname,sc.grade
from course ,sc,(select sno from student where sdept ='计算机系' ) as A inner join student on a.sno=student.sno
where a.sno=sc.sno and course.cno=sc.cno
该示例展示了两种实现方式,分别使用显式内连接和派生查询,最终实现相同的效果,查询计算机系学生的信息,包括学生编号、姓名、性别、成绩和课程名称。
4. 左连接(查询不存在数据)
select student.sno,sname,sdept
from student left join sc on student.sno=sc.sno
where sc.sno is null
--where cno is null
该语句查询所有没有选课的学生信息,通过左连接和判断sc.sno为null来实现。
5. 外连接与嵌套查询
-- 外
select course.cno,cname,ccredit
from course left join sc on course.cno=sc.cno
where sno is null
-- 嵌套
select cno,cname,ccredit
from course
where cno not in (select cno from sc)
该示例展示了两种实现方式,分别使用外连接和嵌套查询,最终实现相同的效果,查询所有没有被选修的课程信息。
6. 聚合函数和分组排序
select top 3 count(*) as '选课门数',sc.sno,sname,sdept,avg(grade) as '平均分'
from sc, student
where sc.sno=student.sno
group by sc.sno,sname,sdept
order by '选课门数' desc
该语句统计每个学生选课门数,并按照选课门数降序排列,只显示前3个学生信息,并计算每个学生的平均成绩。
7. 聚合函数和分组
select count(*) as '选课人数',avg (grade) as '平均分' ,course.cno,cname,ccredit
from course,sc
where course.cno=sc.cno and course.cno !=2
group by all course.cno,cname,ccredit
--having course.cno !=2
该语句统计每个课程的选课人数和平均成绩,并排除cno=2的课程,使用group by all可以确保所有课程都进行分组。
8. 聚合函数和分组(CUBE)
select cno,ssex,count(*) as '人数',avg (grade) as '平均分'
from student,sc
where student.sno=sc.sno
--group by cno,ssex
--group by cno,ssex with rollup
group by cno,ssex with cube
该语句统计每个课程、每个性别的选课人数和平均成绩,使用with cube进行分组,可以得到所有可能的组合结果。
**9. **
**10. **
11. 条件语句
select student.sno ,sname,grade,
case
when grade>=90 then'优秀'
when grade>=80 and grade<90 then '良好'
when grade>=70 and grade<80 then '中等'
when grade>69 and grade<70 then '及格'
--when grade<60 then'不及格'
else'不及格'
end as 'GradeCategory'
from student inner join sc on sc.sno=student.sno
where cno in (select cno from course where cname='数据库') and sdept='计算机系'
该语句查询计算机系学生选修数据库课程的成绩,并根据成绩划分等级,将结果存储在GradeCategory列中。
12. 更新数据
update sc
set grade=grade-5
from student,sc
where student.sno=sc.sno and sdept='计算机系'
--where sno in (select sno from student where sdept='计算机系')
该语句将计算机系学生的所有成绩减去5分。
13. 删除数据
delete sc
from student,sc
where student.sno=sc.sno and sdept='计算机系',and grade<60
--where sno in (select sno from student where sdept='计算机系' ) and grade<60
该语句删除计算机系学生中成绩低于60分的记录。
分析总结
在这些SQL语句中,包含了一些基本的查询、插入、更新和删除操作,以及一些聚合函数的使用。其中,查询操作包括选择指定列并插入到新表中、按条件筛选数据、连接多个表进行查询、查询不存在的数据等。插入操作主要是将查询结果插入到临时表中。更新操作是根据条件对数据进行更新。删除操作是根据条件删除数据。聚合函数的使用包括对数据进行统计计算,如计算平均值、总数等。根据这些SQL语句的使用情况,可以得出以下一些总结:
- SQL语句的基本语法结构是:select ... from ... where ... group by ... having ... order by ...
- SQL语句可以通过连接多个表来进行复杂的查询操作,可以使用内连接、外连接和嵌套查询等方式。
- SQL语句可以使用聚合函数对数据进行统计计算,如count、avg等。
- SQL语句可以使用条件语句(case)来对数据进行分类,如根据成绩划分为优秀、良好、中等等。
- SQL语句可以使用更新语句对数据进行修改,如根据条件更新数据。
- SQL语句可以使用删除语句对数据进行删除,如根据条件删除数据。
- 在使用SQL语句时,需要注意语法的正确性和数据的完整性,以避免出现错误或删除错误的数据。
总的来说,这些SQL语句是对数据库进行操作的重要工具,通过合理使用这些语句可以实现对数据的查询、插入、更新和删除等操作,从而满足不同的需求。
原文地址: https://www.cveoy.top/t/topic/pjdh 著作权归作者所有。请勿转载和采集!