SQL 语句实例详解:数据查询、统计、更新和删除操作

本文将通过13个具体的 SQL 语句实例,详细介绍 SQL 语句在数据查询、统计、更新和删除操作等方面的应用。每个实例都将附带注释,以帮助您更好地理解语句的语法和功能。

1. 数据插入

select sno,sname,ssex,sage 
into JSJSTUDENT
from Student 
select* 
from JSJSTUDENT

该语句首先从 Student 表中选择 snosnamessexsage 列,并将其插入到新表 JSJSTUDENT 中。接着,使用 select * from JSJSTUDENT 语句查看新表中的数据。

2. 数据插入(限制条数)

select top 3 * 
into #student
from student
order by sage desc
select * 
from #student

该语句从 student 表中选择所有列,并按照 sage 列降序排序,只取前 3 行数据,将其插入到临时表 #student 中。最后,使用 select * from #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

该语句演示了两种方法实现内连接查询:显式内连接和使用派生表。第一种方法直接使用 inner join 连接 StudentscCourse 表,并根据条件筛选数据。第二种方法先创建派生表 A,将 sdept 为'计算机系' 的学生的 sno 信息存储到该表中,然后使用 inner join 连接 coursescA 表,并根据条件筛选数据。

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

该语句使用 left join 连接 studentsc 表,并根据 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)

该语句演示了两种方法实现外连接查询:左连接和嵌套查询。第一种方法使用 left join 连接 coursesc 表,并根据 sno 为空来筛选数据,即查询没有被选的课程信息。第二种方法使用 not in 子查询,从 sc 表中选择所有被选课程的 cno,然后从 course 表中筛选出不在子查询结果中的课程信息。

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

该语句统计每门课程的选课人数和平均分,并根据 course.cnocnameccredit 分组。

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

该语句统计每个课程、每个性别的选课人数和平均分,使用 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='计算机系'

该语句查询计算机系学生选修数据库课程的成绩,并根据成绩等级进行分类。

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 分的选课记录。

分析总结

以上 13 个 SQL 语句实例涵盖了数据查询、统计、更新和删除等常见操作。通过这些例子,您应该能够更好地理解 SQL 语句的语法和用法。在实际应用中,您可以根据自己的需求对这些语句进行修改和调整,以满足不同的数据库操作需求。

注意:

  • 以上示例语句仅供参考,实际应用中可能需要根据具体情况进行调整。
  • 不同的数据库系统可能存在一些语法差异。
  • 请谨慎操作数据库,避免误操作导致数据丢失。
SQL 语句实例详解:数据查询、统计、更新和删除操作

原文地址: https://www.cveoy.top/t/topic/pjc4 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录