sql基礎語句——從基礎建庫建表到聯合查詢

create database student
on
(
name=student_data,
filename='C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\student_data.mdf',
size=5,
filegrowth=15%,
maxsize=10
),
(
name=student_data1,
filename='C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\student_data.ndf',
size=2,
filegrowth=10%,
maxsize=10
)
log on
(
name=student_log,
filename='C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\student_data.ldf',
size=3,
filegrowth=3,
maxsize=15
)
--1、按要求創建三張表
--建student表
create table student
(
sno char(7),
sname char(10),
ssex char(2) not null  default'男',
sage tinyint,
sdept char(20),
constraint PK_student primary key(sno)
)
--建course表
create table course
(
cno char(10),
cname char(20) not null,
ccredit tinyint default'3',
csemester tinyint,
constraint PK_course primary key(cno)
)
--建sc表
create table sc
(
sno char(7),
cno char(10),
grade real,
constraint PK_sc primary key(sno,cno),
constraint FK_sc_student foreign key(sno) references student(sno),
constraint FK_sc_course foreign key(cno) references course(cno) 
)

--刪除sc表中的主鍵
alter table sc
drop PK_sc
--添加sc表中的主鍵
alter table sc
add constraint PK_sc primary key(sno,cno)
--修改sc表,在sno字段上創建外鍵約束
alter table sc
add constraint FK_sc_student foreign key(sno) references student(sno)
alter table sc
add constraint FK_sc_course foreign key(cno) references course(cno)    //注意是創建外鍵FK
 
--爲student表中ssex列添加檢查約束,只能輸入男或女
alter table student
add constraint check_ssex check(ssex='男' or ssex='女')
--爲student表中的sage列添加默認值19
alter table student
add constraint default_sage default'19' for sage
--爲student表中sage添加檢查約束,只能輸入18到20歲
alter table student
add constraint check_sage check(sage between 18 and 20)


--1、查詢課程表中的全部信息
select *
from course
--2、查詢學生的學號、姓名和所在系
select sno,sname,sdept
from student
--3、查詢全體學生的姓名及出生年份,將出生年份所在的列改名爲“出生年份”
select sname,2018-sage 出生年份
from student
--4、查詢數學系學生的姓名
select sname
from student
where sdept='數學系'
--5、查詢年齡在20歲以下學生的姓名及年齡
select sname,sage
from student
where sage<20
--6、查詢考試成績有不及格的學生的學號,並取消重複記錄
select  distinct sno
from sc
where grade<60
--7、查詢年齡在20-23歲之間學生的姓名、所在系和年齡(用兩種方法)
select sname,sdept,sage
from student
where sage between 20 and 23

select sname,sdept,sage
from student
where sage in(20,21,22,23)
--8、查詢信息系、數學系和計算機系學生的學號和姓名(用兩種方法)
select sno,sname
from student
where sdept in ('信息系','數學系','計算機系')

select sno,sname
from student
where sdept='信息系' or sdept='數學系'  or  sdept='計算機系'
--9、查詢年齡不再20-23歲之間的學生的姓名和年齡(用兩種方法)
select sname,sdept,sage
from student
where sage not between 20 and 23
--10、查詢既不是信息系也不是數學系學生的姓名(用兩種方法)
select sname,sdept
from student
where sdept not in('信息系','數學系')
--11、查詢姓“張”的學生的詳細信息
select *
from student
where sname like ('張%')
--12、查詢學生表中姓“張”、姓“李”的學生的情況
select *
from student
where sname like '[張李]%'
--13、查詢名字中第二個字爲“大”或“小”的學生學號和姓名
select *
from student
where sname like '_[大小]%'

--14、查詢所有不姓“劉”的學生的信息
select *
from student
where sname not like '劉%'
--15、查詢課程號的最後一位不在2-6範圍學生的情況
select *
from course
where cno not like '%[2-6]'

--16、查詢所有姓“李”的並且第二個字是“%”的學生的信息
select *
from student
where sname like ('張[escape%]%')
--17、查詢沒有考試成績的學生的學號
select sno,grade
from sc
where grade is null
--18、查詢所有有考試成績的學生的學號
select sno
from sc
where grade is not null
--19、查詢計算機系年齡在20歲以下學生的姓名
select sname
from student
where sage<20
--20、查詢計算機系和信息系學生中年齡在18-20歲之間的學號和姓名

--21、查詢c01課程的考試情況,列出學號和成績,同時對成績作如下處理:
--當成績大於等於90分時,在結果中顯示“優秀”
--當成績在80到89分之間時,在結果中顯示“良好”
--當成績在70到79分之間時,在結果中顯示“中等”
--當成績在60到69分之間時,在結果中顯示“及格”
--當成績小於60分時,在結果中顯示“不及格”
--成績爲空時,在結果中顯示“缺考”
select sno,grade,
case
when grade>=90 then '優秀'
when grade between 80 and 89 then '良好'
when grade between 70 and 79 then '中等'
when grade between 60 and 69 then '及格'
when grade <60 then '不及格'
else '缺考'
end as 等級
from sc
where cno like 'c01'
--22、查詢所有學生的信息,將學生年齡按升序排序
select *
from student
order by sage asc
--23、查詢選修了c01號課程的學生的學號及其成績,查詢結果按成績降序排序
select sno,grade
from sc
where cno like 'c01'
order by grade desc
--24、查詢全體學生的信息,查詢結果按所在系的系名升序排列,同一系學生按年齡的降序排列
select *
from student
order by sdept asc,sage desc
--25、查詢學生總人數
select count(*) as 總人數
from student
--26、查詢選修了課程的學生的人數
select count(distinct sno) as 選課人數
from sc
--27、計算001號學生的考試總成績
select sum(grade) as 總成績
from sc
where sno='001'
--28、計算c01號課程的考試平均成績
select avg(grade) as 平均成績
from sc
where cno='c01'
--29、查詢選修了c01號課程的學生的最高分和最低分
select max(grade) as 最高分,min(grade) as 最低分
from sc
where cno='c01'

--30、統計每門課程的選課人數,列出課程號和選課人數
select cno,count(sno) as 選課人數
from sc
group by cno
--31、查詢每名學生的選課門數和平均成績
select sno,count(cno) as 選課門數,avg(grade) as平均成績
from sc
group by sno 
--32、統計每個系的學生人數和平均年齡
select sdept,count(sno) as 學生人數,avg(sage) as 平均年齡
from student
group by sdept
--33、統計每個系的女生人數
select sdept,count(*) as 女生人數
from student
where ssex='女'
group by sdept
--34、統計每個系的男生人數和女生人數以及男生的最大年齡和女生的最大年齡
select sdept,ssex,count(*) as 人數,max(sage) as 最大年齡,min(sage) as 最小年齡
from student
group by sdept,ssex

--35、查詢選修了3門以上課程的學生的學號和選課門數
select sno,count(cno) as 選課門數
from sc
group by sno
having count(cno)>2
--36、查詢選課門數等於或小於4門的學生的平均成績和選課門數
select sno,count(cno) as 選課門數,avg(grade)as 平均成績
from sc
group by sno
having count(cno)<=4
--37、對c01號課程的成績進行明細彙總
select sno,cno,grade
from sc
where cno='c01'
compute sum by grade
--38、對每門課程的成績進行明細彙總
select sno,cno,grade
from sc
where cno
compute sum by grade
--39、分別從student表中返回前5個,返回表中前20%的學生信息
select top 5 *
from student

select top 20 percent *
from student
--40、查詢sc表中,分數最高的6個學生的學號
select top 6 sno,grade
from sc
order by grade desc


--1、查詢每個學生及其選修課的情況(內連接兩種方式實現)
select *
from sc,student
where student.sno=sc.sno 

select *
from student inner join sc on student.sno=sc.sno 
--2、查詢計算機系學生的選修課情況,要求列出學生的名字、所修課程的課程號和成績。(內連接兩種方式實現)
select sname,cno,grade
from sc,student
where student.sno=sc.sno and sdept='計算機系'

select sname,cno,grade
from student inner join sc on student.sno=sc.sno and sdept='計算機系'
--3、查詢信息系選修了VB課程的學生的選修課成績,列出學生的姓名、課程名和成績。(內連接兩種方式實現)
select sname,cname,grade
from sc,student,course
where student.sno=sc.sno and sc.cno=course.cno  and cname='VB' and sdept='信息系'

select sname ,cname,grade
from student inner join sc on student.sno=sc.sno inner join course on sc.cno=course.cno 
where cname='VB' and sdept='信息系'
--4、查詢每個系的學生的考試平均成績。(內連接兩種方式實現)
--5、查詢計算機系每門課程的選課人數、平均成績、最高成績和最低成績。(內連接兩種方式實現)
--6、查詢學生的選修課情況,包括選修了課程的學生和沒有選修課程的學生。(左外連接和右外連接實現)
select student.sno,sname,grade
from student left outer join sc on student.sno=sc.sno

select student.sno,sname,grade
from sc right outer join student on student.sno=sc.sno
--7、查詢哪些課程沒有人選,列出課程名
--8、將對計算機系學生的查詢結果與信息系學生的查詢結果合併爲一個結果集
--9、查詢要求同上,但將查詢結果按年齡從大到小排序
--10、將計算機系的學生信息存入到#computer局部臨時表中
--11、將選修了VB課程的學號及成績存入全局臨時表##computer中
--12、將計算機系學生的姓名、選修課的課程名和成績存入到永久表computer中
--13、查詢年齡最大的三個學生的姓名、年齡及所在系
--14、查詢VB課程考試成績的前三名的學生的姓名和成績

--1.查詢與“張一”在同一個系學習的學生。
select sdept
from student
where sname='張一'

select *
from student
where sdept in (select sdept
             from student
             where sname='張一')


--2.查詢成績大於90分的學生的學號和姓名。
select sno
from sc
where grade>90

select sno,sname
from student
where sno in(select sno
           from sc
           where grade>90)
--3.查詢計算機系選修了“數據庫基礎”課程的學生的學號和姓名。
select cno
from course
where cname like '數據庫'

select sno
from sc
where cno in (select cno
from course
where cname like '數據庫')

select sno,sname
from student
where sdept='計算機系'and sno in (select sno
              from sc
              where cno in (select cno
              from course
              where cname like '數據庫'))
--4.查詢修了‘C02’課程、且成績高於此課程的平均成績的學生的學號和成績。
select avg(grade) as 平均成績
from sc
where cno='c02'

select sno,grade
from sc
where grade>(select avg(grade)
             from sc
             where cno='c02')
--5.查詢計算機系年齡大於學生總平均年齡的學生的姓名和年齡。
select avg(sage)
from student

select sname,sage
from student
where sdept ='計算機系' and sage>(select avg(sage)
                                  from student) 

--6.查詢‘c02’課程考試成績最高的學生的姓名和所在系。
select max(grade)
from sc
where cno='c02'

select sno
from sc
where grade in (select max(grade)
from sc
where cno='c02')

select sname,sdept
from student
where sno in (select sno
from sc
where grade in (select max(grade)
from sc
where cno='c02'))

--7.查詢選修了‘C01’號課程的學生姓名。
select sno
from sc
where cno='c01'

select sname 
from student
where sno in (select sno
              from sc
              where cno='c01') 


 

發佈了2 篇原創文章 · 獲贊 6 · 訪問量 824
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章