【SQL】简单查询语句操作实例

一、SQL Server简单查询语句

背景知识:
一、查询:SQL中最基本、最常用的操作,用来对数据库进行查询
二、表达式:
select 属性列 from 表
where 筛选条件
group by 分组属性列
having 分组后筛选条件
orser by 排序

名称解释(不区分大小写)
  • student学生表:
    • 包含属性列:sno学号、sname学生姓名、age年龄、sex性别、dno学院编号、birthday生日
  • sc选课信息表:
    • 包含属性列:sno学号、cno课程号、grade成绩
  • dept学院信息表:
    • 包含属性列:dno学院编号、dname学院名称、dean学院负责人
  • course课程信息表:
    • 包含属性列:cno课程号、cname课程名称、tname老师名称、credit学分、room教室

下面语句可以直接复制到SQL Server运行

  • 运行方法,选中执行语句在这里插入图片描述

方法不唯一,有问题欢迎留言!代码块见文章末尾

二、代码

–1.求学院编号为’0001’的学生的学号、姓名、性别
select SNO,SNAME,SEX
from student
where DNO = ‘0001’

–2.求学院编号为’0001’ 的男生的学号、姓名、性别
select SNO,SNAME,SEX
from student
where DNO = ‘0001’ and SEX = ‘男’

–3.求选修授课班号为‘327401’且成绩在 80~90 之间的学生学号和成绩,并将成绩乘以系数 0.8 输出,且将 SNO 列更名为学号,成绩列更名为处理成绩
select SNO as 学号,GRADE*0.8 as 处理成绩 from sc
where CNO = ‘327401’ and GRADE between 80 and 90

–4.求每个学生的年龄,并输出姓名和年龄,getdate()获取当前日期,如果需要查询函数在对象资源管理器种可编程性的函数中
select SNAME,(YEAR(GETDATE()) - YEAR(BIRTHDAY)) as 年龄 from student

–5.求选修了课程的学生的学号
select distinct sno from sc

–6.求选修授课班号为’327401’的学生学号和成绩,并要求对查询结果按成绩的降序排列,如果成绩相同则按学号的升序排列
select sno,grade from sc
where cno = ‘327401’
order by grade DESC,sno ASC

–7.求缺少了成绩的学生学号和课程号
select sno,cno from sc
where grade is NULL

–8.统计选课学生人数及最高分成绩和最低分成绩
select COUNT(distinct sno) as 选课学生人数,MAX(grade) as MAX,MIN(grade) as MIN from sc

–9.求学院编号为’0001’或‘0002‘中姓张的学生的信息
select * from student
where (dno = ‘0001’ or dno = ‘0002’) and sname like ‘张%’

–10.求姓名中包含’丽’的学生信息
select * from student
where sname like ‘%丽%’

–11.求姓名只有两个字,且第二个字为’丽’的学生信息
select * from student
where sname like’_丽’

–12.求信息学院计算机专业的学生名单,使用substring(string,start_position,length)函数来获取子串
select * from student
where SUBSTRING(sno,5,2) = ‘28’

–13.统计各个学院的人数,descend,ascend单词缩写
select COUNT(distinct sno) as 人数,DNO as 部门编号 from student
group by DNO
order by 人数 DESC

–14.按授课班号统计选修该课程的人数,并按照人数升序排列
select cno,COUNT(distinct sno) as 人数 from sc
group by cno
order by 人数 ASC

–* 15.统计平均成绩超过 80 分的学生的学号及平均成绩
select sno ,avg(grade) as 平均成绩 from sc
group by sno
having avg(grade) > 80
order by 平均成绩 desc

–16.求选修课程超过 6 门课的学生学号,并按选修课程数目升序排列
select sno,count(sno) from sc
group by sno
having COUNT(cno) > 6
order by COUNT(cno) asc

–17.求每个学院学生的平均年龄,并把结果存入当前数据库‘系平均年龄’临时表中
select dno,avg(age) AS 平均 INTO 系平均年龄
from student
group by DNO

–18.分页浏览数据方法
–添加控制元素就可以实现分页浏览方法
select top 10 * from student

select top 10 * from student
where SNO not in (select top 10 sno from student)

–19.查询‘1987-1-1’号以后出生的女生的学生信息
select * from student
where BIRTHDAY > ‘1987-1-1’ and SEX = ‘女’

–20.创建’计算机系学生’视图,用于浏览计算机系学生的学号、姓名和年龄
create view 计算机系学生
as
select sno,sname,age from student
where SUBSTRING(sno,5,2) = ‘28’

–1.查询分数在 70 和 90 之间的学生学号
select grade from sc
where grade between 70 and 90

–2.查询少于 10 名同学选修的授课班号
select cno from sc
group by cno
having COUNT(sno) < 10

–3.查询选课表中的最高分
select max(grade) from sc

–4.查询授课编号为‘153701’的课程的平均分
select avg(grade) from sc
where cno = ‘153701’

–5.查询课程平均分超过 85 的授课班号,输出结果按课程平均分升序排列
select cno from sc
group by cno
having avg(grade) > 85
order by avg(grade) asc

–6.查询课程名称为’线性代数’的排课情况
select * from course
where cname = ‘线性代数’

–7.查询选修授课班号为‘218801’的学生学号
select sno from sc
where cno = ‘218801’

–8.按授课班号查询课程的平均分,输出授课班号和平均成绩
SELECT CNO,AVG(GRADE)
FROM sc
GROUP BY CNO
–9.在 sc 中输出成绩在 90-100 之间的学生信息
SELECT * FROM sc WHERE CONVERT(char(20),GRADE) LIKE ‘9%’

–查询‘周芬’老师,这个学期的上课安排情况
select * from course
where tname = ‘周芬’

–查询姓‘周’的教师的排课情况
select * from course
where tname like ‘周%’

–按教室分组统计排课门数情况
select room,count(cno) from course
group by room

–查询排课门数超过 8 门的教室名单及其排课门数
select room,count(cno) from course
group by room
having count(cno) > 8

–创建机电学院女生的视图
create view 机电院女生
as
select * from student
where dno = ‘0001’ and sex = ‘女’

–查询学分超过 4 分的课程,输出课程名和学分,并要求按学分升序
select distinct cname,credit from course
where credit > 4
order by CREDIT asc

–按教室明细并汇总排课情况
select * from course

–查询课程号为“203402”的成绩最高的前 5 名学生的学号及成绩,结果按成绩降序
select top 5 sno,grade from sc
where cno = ‘203402’
order by grade desc

–查询年龄小于 20 岁的学生学号
select sno from student
where age < 20

–查询有 90 人以上选修的课程号
select cno from sc
group by cno
having count(sno) > 90

–查询全体男生的姓名,要求查询结果按所在系升序排列,对相同系的学生按姓名升序排列
select sname from student
order by dno,sname

–查询成绩在 70-90 范围内的学生学号
select sno from sc
where grade between 70 and 90

三、SQL代码块

--1.求学院编号为’0001’的学生的学号、姓名、性别
select SNO,SNAME,SEX 
from student
where DNO = '0001'

--2.求学院编号为’0001’ 的男生的学号、姓名、性别
select SNO,SNAME,SEX
from student
where DNO = '0001' and SEX = '男'

--3.求选修授课班号为‘327401’且成绩在 80~90 之间的学生学号和成绩,并将成绩乘以系数 0.8 输出,且将 SNO 列更名为学号,成绩列更名为处理成绩 
select SNO as 学号,GRADE*0.8 as 处理成绩 from sc
where CNO = '327401' and GRADE between 80 and 90

--4.求每个学生的年龄,并输出姓名和年龄,getdate()获取当前日期,如果需要查询函数在对象资源管理器种可编程性的函数中
select SNAME,(YEAR(GETDATE()) - YEAR(BIRTHDAY)) as 年龄 from student

--5.求选修了课程的学生的学号
select distinct sno from sc

--6.求选修授课班号为’327401’的学生学号和成绩,并要求对查询结果按成绩的降序排列,如果成绩相同则按学号的升序排列
select sno,grade from sc
where cno = '327401' 
order by grade DESC,sno ASC

--7.求缺少了成绩的学生学号和课程号
select sno,cno from sc
where grade is NULL

--8.统计选课学生人数及最高分成绩和最低分成绩
select COUNT(distinct sno) as 选课学生人数,MAX(grade) as MAX,MIN(grade) as MIN from sc

--9.求学院编号为’0001’或‘0002‘中姓张的学生的信息
select * from student
where (dno = '0001' or dno = '0002') and sname like '张%'

--10.求姓名中包含’丽’的学生信息
select * from student
where sname like '%丽%'

--11.求姓名只有两个字,且第二个字为’丽’的学生信息
select * from student
where sname like'_丽'

--12.求信息学院计算机专业的学生名单,使用substring(string,start_position,length)函数来获取子串
select * from student
where SUBSTRING(sno,5,2) = '28'

--13.统计各个学院的人数,descend,ascend单词缩写
select COUNT(distinct sno) as 人数,DNO as 部门编号 from student
group by DNO
order by 人数 DESC

--14.按授课班号统计选修该课程的人数,并按照人数升序排列
select cno,COUNT(distinct sno) as 人数 from sc
group by cno
order by 人数 ASC

--* 15.统计平均成绩超过 80 分的学生的学号及平均成绩
select sno ,avg(grade) as 平均成绩 from sc
group by sno
having avg(grade) > 80
order by 平均成绩 desc

--16.求选修课程超过 6 门课的学生学号,并按选修课程数目升序排列
select sno,count(sno) from sc
group by sno
having COUNT(cno) > 6
order by COUNT(cno) asc

--17.求每个学院学生的平均年龄,并把结果存入当前数据库‘系平均年龄’临时表中
select dno,avg(age) AS 平均 INTO 系平均年龄
from student
group by DNO

--18.分页浏览数据方法
--添加控制元素就可以实现分页浏览方法
select top 10 * from student

select top 10 * from student
where SNO not in (select top 10 sno from student)

--19.查询‘1987-1-1’号以后出生的女生的学生信息
select * from student
where BIRTHDAY > '1987-1-1' and SEX = '女'

--20.创建'计算机系学生'视图,用于浏览计算机系学生的学号、姓名和年龄
create view 计算机系学生
as 
select sno,sname,age from student
where SUBSTRING(sno,5,2) = '28'

--1.查询分数在 70 和 90 之间的学生学号
select grade from sc
where grade between 70 and 90

--2.查询少于 10 名同学选修的授课班号
select cno from sc
group by cno
having COUNT(sno) < 10

--3.查询选课表中的最高分
select max(grade) from sc

--4.查询授课编号为‘153701’的课程的平均分
select avg(grade) from sc
where cno = '153701'

--5.查询课程平均分超过 85 的授课班号,输出结果按课程平均分升序排列
select cno from sc
group by cno
having avg(grade) > 85
order by avg(grade) asc

--6.查询课程名称为’线性代数’的排课情况
select * from course
where cname = '线性代数'

--7.查询选修授课班号为‘218801’的学生学号
select sno from sc
where cno = '218801'

--8.按授课班号查询课程的平均分,输出授课班号和平均成绩
SELECT CNO,AVG(GRADE)
FROM sc
GROUP BY CNO
--9.在 sc 中输出成绩在 90-100 之间的学生信息
SELECT * FROM sc WHERE CONVERT(char(20),GRADE) LIKE '9%'

--查询‘周芬’老师,这个学期的上课安排情况
select * from course
where tname = '周芬'

--查询姓‘周’的教师的排课情况
select * from course
where tname like '周%'

--按教室分组统计排课门数情况
select room,count(cno) from course
group by room

--查询排课门数超过 8 门的教室名单及其排课门数
select room,count(cno) from course
group by room
having count(cno) > 8

--创建机电学院女生的视图
create view 机电院女生
as
select * from student
where dno = '0001' and sex = '女'

--查询学分超过 4 分的课程,输出课程名和学分,并要求按学分升序
select distinct cname,credit from course
where credit > 4
order by CREDIT asc

--按教室明细并汇总排课情况
select * from course

--查询课程号为“203402”的成绩最高的前 5 名学生的学号及成绩,结果按成绩降序
select top 5 sno,grade from sc
where cno = '203402'
order by grade desc

--查询年龄小于 20 岁的学生学号
select sno from student
where age < 20

--查询有 90 人以上选修的课程号
select cno from sc
group by cno
having count(sno) > 90

--查询全体男生的姓名,要求查询结果按所在系升序排列,对相同系的学生按姓名升序排列
select sname from student
order by dno,sname

--查询成绩在 70-90 范围内的学生学号
select sno from sc
where grade between 70 and 90
  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小子挺不错

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值