SQL语句大(bu)全

前言

近来感觉SQL语句有些生疏,于是,便有了这次的回忆

采用的是SQLServer 2008

最常见的三张表

Student(sno, name, sex, age, dept )
Course(cno, name, pno, teacher, credit)
SC(sno, cno, grade)

常见SQL语句

1.插入一名学生的信息'200215126', '张辉', '男', 20, 'IS'

insert into student(sno, name, sex, age, dept )
values( '200215126', '张辉', '男', 20, 'IS' )

2.更新学号为200215121的学生的年龄为20,学院为计算机院

update student 
set age=20,dept='CS' 
where sno='200215121'

3.检索不学C2课的学生姓名与年龄

(思路:学生表里总体的姓名和年龄 “减去” 学C2的学生姓名和年龄 等于不学的)

集合的操作,见SQL语句对结果集操作

(select name ,age from student) 
Except
(select name ,age from student,sc where student.sno=sc.sno and sc.cno='2') 

4.检索学习全部课程的学生学号

(思路:选课表中全部的学号和课程号 “除以” 课表中全部的课程号)

除法的操作,见SQL语句实现关系代数中的“除法”

select  distinct  sno  from  sc  sc1
where not exists
(
    select  cno from course
    where not exists
    (
        select * from sc sc2
        where sc2.sno=sc1.sno and sc2.cno=course.cno
    )
)

5.检索所学课程包含学生95002所学课程的学生学号

(思路:选课表中全部的学号和课程号“除以”选课表中95002学生所学的课程号)

select  distinct  sno  from  sc  sc1
where not exists
(
    select  cno from sc sc2
    where sno='95002'
    and not exists
    (
        select * from sc sc3
        where sc3.sno=sc1.sno and sc3.cno=sc2.cno
    )
)

6.统计信息(总人数,男生人数,女生人数,平均年龄,每个课程的平均分,最高分,95001的总学分)

详情见 Sql Server 2008单个存储过程统计多个信息

select
(select count(sno)from student) sumPersno,
(select count(sno)from student where sex='男') boynumber,
(select count(sno)from student where sex='女') girlnumber,
(select avg(age)from student) avgOfAge,
(select avg(grade)from sc where cno='1' ) avgOfCourse1,
(select avg(grade)from sc where cno='2' ) avgOfCourse2,
(select avg(grade)from sc where cno='3' ) avgOfCourse3,
(select max(grade)from sc where cno='1' ) maxOfCourse1,
(select max(grade)from sc where cno='2' ) maxOfCourse2,
(select max(grade)from sc where cno='3' ) maxOfCourse3,
(select sum(credit)from course, sc where course.cno=sc.cno and sc.sno='95001' ) sumCreditOf95001

7.找出不姓‘王’的学生记录。

select sno, name, sex, age, dept 
from student 
where name not like'王%'

8.统计每个学生选修课程的个数

select student.sno, count(cno)SCnumber 
from student left join sc on sc.sno=student.sno  
group by student.sno

9.统计有学生选修的课程门数。

select count(distinct SC.cno)SCCourseNumber from SC;

10.求选修课程号为2的课程名称,学生平均年龄,平均成绩,均保留两位小数

select course.name ,round(avg(age),2) avgOfAge ,round(avg(grade),2) avgGrade 
from student ,sc,course
where student.sno=sc.sno 
and course.cno=sc.cno
and sc.cno='2' 
group by course.name;

还可以用第6个的统计信息的方式来求(其中平均年龄可以换个方式)

select
    (select course.name from course where course.cno='2') courseName,
    (select round(avg(age),2)avgOfAge from student 
    where student.sno in(select sno from sc where cno='2'))avgOfAge,
    (select round(avg(grade),2) from sc where sc.cno='2')avgGrade

11.检索学号比刘晨同学大,而年龄比他小的学生姓名

法1:暴力法

select student.name from student
where sno>(select sno from student where student.name='刘晨')
and age<(select age from student where student.name='刘晨')

法2:嵌套法

select name from student x 
where sno> some(select sno from student  y where y.name='刘晨' and x.age<y.age)

12.求年龄大于女同学平均年龄的男学生姓名和年龄。

select name ,age from student 
where sex='男' 
and age>(select round(avg(age),2) from student where sex='女')

13.求年龄大于所有女同学年龄的男学生姓名和年龄

select name ,age 
from student 
where sex='男' and age>(select max(age) from student where sex='女')

14.检索每一门课程成绩都大于等于90分的学生学号、姓名和性别

(思路:
1.在SC表中查出最小成绩大于90的学生学号
按照学号进行分组,然后求小组的最小成绩大于90的学号
2.在Student表中根据sno查详细信息

select sno,name,sex from student 
where sno in (select sno from sc group by sno having min(grade)>=90)

收获:聚合函数只能用在Select后面和Having后面
group by 对数据进行分组,然后 having对小组内部的数据进行操作

注意:操作的过程是:
先查,后对查询结果分组,分组后,组内调用函数,最后根据Having后面的条件对其进行筛选

注意:
Group By 后面出现的属性,在Select后面一定要出现


15.把低于总平均成绩的女同学成绩提高5%

(思路:
1.首先求总平均成绩
2.修改低于平均成绩的女生的成绩(此时的修改没有改变已求出的平均成绩)

update sc 
set grade=grade*1.05 
where sc.grade<(select avg(grade) from sc )
and sc.sno in(select sno from student where sex='女') 

16.检索至少选修两门课程的学生的学号

思路:
1.按照学号将学生分组
2.统计小组内部选修课程的数量
3.根据条件检索

select sc.sno 
from sc 
group by sc.sno
having count(sc.cno)>=2 

17.检索所修课程平均成绩大于等于90分的学生姓名及其平均成绩

select sc.sno, student.name,avg(grade) 
from sc,student
where student.sno=sc.sno
group by sc.sno,student.name
having avg(grade)>90

收获:
当select里有聚合函数时,select的其他元素也必须是聚合函数或出现在group by后面


18.查询最高分的学生学号和课程号

select sno,cno from sc
where grade=(select max(grade)from sc)

19.查询存在有85分以上成绩的课程Cno

不能用:select distinct cno from sc where grade>85,虽然能够实现

要用select distinct cno from sc where grade in(select grade from sc where grade>85)
或者
select cno from sc where grade>85 group by cno
再或者
select cno from sc group by cno having max(grade)>85


20.查询所有选修“数学”课程的同学的学号和成绩

方法一嵌套

select sno,grade from sc 
where cno=(select cno from course where course.name='数学' )

方法二链接

select sno,grade from course, sc 
where cno=(select cno from course where course.name='数学' )

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容