SQL 查询语句示例:从基本操作到高级分析
SQL 查询语句示例:从基本操作到高级分析
以下是一些 SQL 查询语句示例,涵盖了从基本数据检索到高级数据分析的各种操作:
1. 数据插入和检索
select sno,sname,ssex,sage
into JSJSTUDENT
from Student
select*
from JSJSTUDENT
该语句将 Student 表中的 sno, sname, ssex, sage 列数据插入到一个名为 JSJSTUDENT 的新表中,随后从 JSJSTUDENT 表中检索所有数据。
2. 提取前三条记录并插入临时表
select top 3*
into #student
from student
order by sage desc
select *
from #student
该语句从 student 表中按照 sage 列降序排列,选择前三条记录插入到名为 #student 的临时表中,随后从 #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
第一个查询使用显式内连接将 Student 和 sc 表连接,并加入 Course 表,根据 sdept='计算机系' 和 Course.cno=Sc.cno 条件筛选数据。第二个查询使用派生表,先查询出计算机系所有学生的 sno,然后进行连接操作,实现相同结果。
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
该语句使用左连接将 student 和 sc 表连接,根据 sc.sno 为空筛选出没有选课的学生信息。
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)
第一个查询使用左连接将 course 和 sc 表连接,根据 sno 为空筛选出没有被选修的课程信息。第二个查询使用子查询,先查询出所有被选修的课程,再根据 not in 条件筛选出未被选修的课程信息。
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
该语句统计每个学生的选课门数和平均成绩,并根据选课门数降序排列,只显示前三条记录。
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,cname,ccredit 分组,排除 cno 为 2 的课程。
8. 多维分组聚合分析
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
该语句统计每门课程、每种性别的学生人数和平均成绩,使用 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='计算机系'
该语句根据成绩范围对学生成绩进行分类,并根据 cno,cname,sdept 条件进行筛选。
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 打下基础。
注意:
这些示例代码只是简单的示例,实际应用中可能需要根据具体情况进行调整。
更多示例:
您可以访问其他网站或书籍,学习更多关于 SQL 查询语句的示例。
希望本页面对您有所帮助!
原文地址: https://www.cveoy.top/t/topic/pjcU 著作权归作者所有。请勿转载和采集!