前言
近來感覺SQL語句有些生疏,于是,便有了這次的回憶
采用的是SQLServer 2008
最常見的三張表
Student(sno, name, sex, age, dept )
Course(cno, name, pno, teacher, credit)
SC(sno, cno, grade)
常見SQL語句
1.插入一名學(xué)生的信息'200215126', '張輝', '男', 20, 'IS'
insert into student(sno, name, sex, age, dept )
values( '200215126', '張輝', '男', 20, 'IS' )
2.更新學(xué)號為200215121的學(xué)生的年齡為20,學(xué)院為計算機(jī)院
update student
set age=20,dept='CS'
where sno='200215121'
3.檢索不學(xué)C2課的學(xué)生姓名與年齡
(思路:學(xué)生表里總體的姓名和年齡 “減去” 學(xué)C2的學(xué)生姓名和年齡 等于不學(xué)的)
集合的操作,見SQL語句對結(jié)果集操作
(select name ,age from student)
Except
(select name ,age from student,sc where student.sno=sc.sno and sc.cno='2')
4.檢索學(xué)習(xí)全部課程的學(xué)生學(xué)號
(思路:選課表中全部的學(xué)號和課程號 “除以” 課表中全部的課程號)
除法的操作,見SQL語句實現(xiàn)關(guān)系代數(shù)中的“除法”
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.檢索所學(xué)課程包含學(xué)生95002所學(xué)課程的學(xué)生學(xué)號
(思路:選課表中全部的學(xué)號和課程號“除以”選課表中95002學(xué)生所學(xué)的課程號)
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.統(tǒng)計信息(總?cè)藬?shù),男生人數(shù),女生人數(shù),平均年齡,每個課程的平均分,最高分,95001的總學(xué)分)
詳情見 Sql Server 2008單個存儲過程統(tǒng)計多個信息
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.找出不姓‘王’的學(xué)生記錄。
select sno, name, sex, age, dept
from student
where name not like'王%'
8.統(tǒng)計每個學(xué)生選修課程的個數(shù)
select student.sno, count(cno)SCnumber
from student left join sc on sc.sno=student.sno
group by student.sno
9.統(tǒng)計有學(xué)生選修的課程門數(shù)。
select count(distinct SC.cno)SCCourseNumber from SC;
10.求選修課程號為2的課程名稱,學(xué)生平均年齡,平均成績,均保留兩位小數(shù)
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個的統(tǒng)計信息的方式來求(其中平均年齡可以換個方式)
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.檢索學(xué)號比劉晨同學(xué)大,而年齡比他小的學(xué)生姓名
法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.求年齡大于女同學(xué)平均年齡的男學(xué)生姓名和年齡。
select name ,age from student
where sex='男'
and age>(select round(avg(age),2) from student where sex='女')
13.求年齡大于所有女同學(xué)年齡的男學(xué)生姓名和年齡
select name ,age
from student
where sex='男' and age>(select max(age) from student where sex='女')
14.檢索每一門課程成績都大于等于90分的學(xué)生學(xué)號、姓名和性別
(思路:
1.在SC表中查出最小成績大于90的學(xué)生學(xué)號
按照學(xué)號進(jìn)行分組,然后求小組的最小成績大于90的學(xué)號
2.在Student表中根據(jù)sno查詳細(xì)信息
select sno,name,sex from student
where sno in (select sno from sc group by sno having min(grade)>=90)
收獲:聚合函數(shù)只能用在Select后面和Having后面
group by 對數(shù)據(jù)進(jìn)行分組,然后 having對小組內(nèi)部的數(shù)據(jù)進(jìn)行操作
注意:操作的過程是:
先查,后對查詢結(jié)果分組,分組后,組內(nèi)調(diào)用函數(shù),最后根據(jù)Having后面的條件對其進(jìn)行篩選
注意:
Group By 后面出現(xiàn)的屬性,在Select后面一定要出現(xiàn)
15.把低于總平均成績的女同學(xué)成績提高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.檢索至少選修兩門課程的學(xué)生的學(xué)號
思路:
1.按照學(xué)號將學(xué)生分組
2.統(tǒng)計小組內(nèi)部選修課程的數(shù)量
3.根據(jù)條件檢索
select sc.sno
from sc
group by sc.sno
having count(sc.cno)>=2
17.檢索所修課程平均成績大于等于90分的學(xué)生姓名及其平均成績
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
收獲:
當(dāng)select里有聚合函數(shù)時,select的其他元素也必須是聚合函數(shù)或出現(xiàn)在group by后面
18.查詢最高分的學(xué)生學(xué)號和課程號
select sno,cno from sc
where grade=(select max(grade)from sc)
19.查詢存在有85分以上成績的課程Cno
不能用:select distinct cno from sc where grade>85
,雖然能夠?qū)崿F(xiàn)
要用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.查詢所有選修“數(shù)學(xué)”課程的同學(xué)的學(xué)號和成績
方法一嵌套
select sno,grade from sc
where cno=(select cno from course where course.name='數(shù)學(xué)' )
方法二鏈接
select sno,grade from course, sc
where cno=(select cno from course where course.name='數(shù)學(xué)' )