sql查询代码.docx

上传人:b****4 文档编号:5613375 上传时间:2023-05-08 格式:DOCX 页数:17 大小:19.95KB
下载 相关 举报
sql查询代码.docx_第1页
第1页 / 共17页
sql查询代码.docx_第2页
第2页 / 共17页
sql查询代码.docx_第3页
第3页 / 共17页
sql查询代码.docx_第4页
第4页 / 共17页
sql查询代码.docx_第5页
第5页 / 共17页
sql查询代码.docx_第6页
第6页 / 共17页
sql查询代码.docx_第7页
第7页 / 共17页
sql查询代码.docx_第8页
第8页 / 共17页
sql查询代码.docx_第9页
第9页 / 共17页
sql查询代码.docx_第10页
第10页 / 共17页
sql查询代码.docx_第11页
第11页 / 共17页
sql查询代码.docx_第12页
第12页 / 共17页
sql查询代码.docx_第13页
第13页 / 共17页
sql查询代码.docx_第14页
第14页 / 共17页
sql查询代码.docx_第15页
第15页 / 共17页
sql查询代码.docx_第16页
第16页 / 共17页
sql查询代码.docx_第17页
第17页 / 共17页
亲,该文档总共17页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

sql查询代码.docx

《sql查询代码.docx》由会员分享,可在线阅读,更多相关《sql查询代码.docx(17页珍藏版)》请在冰点文库上搜索。

sql查询代码.docx

sql查询代码

、创建数据库和表

1创建数据库

createdatabaseStudent

2创建表student,course和student_course

useStudent;

go

createtablestudents(

Student_idvarchar(32)primarykey,

Student_namevarchar(32),

Student_sexchar

(2),

Student_nationchar(5),

Student_birthdaydatetime,

Student_timedatetime,

Student_classidvarchar(32),

Student_homevarchar(32),

Student_elsevarchar(32)

);

go

createtablecourse(

Course_idvarchar(32)primarykey,

Course_namevarchar(32),

Course_periodint,

Course_creditint,

Course_kindint,

Course_describevarchar(32)

);

go

createtablestudent_course(

IDbigintprimarykey,

Student_idvarchar(32),

Course_idvarchar(32),

Student_gradeint,

Course_yeartinyint

);

3在表student中插入数据

insertintostudentsvalues

('011001','叶海平','男','汉','1989-01-23','2010-09-01','011','山西',null),('011002','景风','男','汉','1989-06-25','2010-09-01','011','重庆',null),('012001','华丽佳','女','汉','1988-05-20','2010-09-01','012','大连',null),('012002','范治华','男','汉','1989-05-20','2010-09-01','012','山东',null),('013001','李佳佳','女','汉','1990-03-01','2010-09-01','013','湖南',null),

('013002','史慧敏','女','汉','1988-10-11','2010-09-01','013','湖北',null),

('014001','安静','女','汉','1991-03-23','2010-09-01','014','山西',null),

('014002','尹强','男','汉','1987-06-02','2010-09-01','014','重庆',null),

('015001','曹洪','男','汉','1989-05-16','2010-09-01','015','大连',null),('015002','杨世英','女','汉','1989-12-03','2010-09-01','015','天津',null)

4在表course中插入数据

insertintocoursevalues

('1001','电子商务基础',72,2,1,null),

('2001','英语',72,3,0,null),

('2002','英语',72,3,0,null),

('3001','网页设计与制作',72,2,1,null),

('4001','网络数据库',72,3,1,null),

('5001','电子商务安全与管理',72,2,1,null)

5在表student_course中插入数据

insertintostudent_coursevalues

(1,'011001','1001',82,1),

(2,'011002','1001',86,1),

(3,'011001','2001',78,1),

(4,'011002','2001',80,1),

(5,'011001','2002',77,2),

(6,'011002','2002',88,2),

(7,'012001','1001',90,1),

(8,'012002','1001',75,1),

(9,'012001','2001',68,1),

(10,'012002','2001',73,1),

(11,'012001','2002',80,2),

(12,'012002','2002',78,2),

(13,'012001','4001',85,2),

(14,'012002','4001',66,2),

(15,'013001','1001',76,1),

(16,'013002','1001',81,1),

(17,'013001','2001',69,1),

(18,'013002','2001',90,1),

(19,'013001','2002',70,2),

(20,'013002','2002',89,2),

(21,'013001','4001',88,2),

(22,'013002','4001',86,2),

(23,'014001','4001',76,2),

(24,'014002','4001',80,2),

(25,'015001','4001',88,2),

(26,'015002','4001',66,2)

7.2选择select查询结果集输出的列

7.2.2不能指定任何列

--用select语句显示常量、函数和表达式的值

select今天是’,getdate(),'我的版本是’,@@version,1+2

7.2.3使用通配符(*)

--查询表student中的所有学生的全部信息

useStudent

select*fromstudents

7.2.4指定输出列

--查询表studes中的所有学生的学号和姓名

selectstudent_id,student_namefromstudents

7.2.5使用列表达式

--在表student_course中查询学生的学号、课程号、成绩和新成绩(在原始成绩分数的基础上加分)

selectStudent_id,Course_id,Student_grade,Student_grade+5

fromstudent_course

7.2.6为查询结果指定列名

--为结果集中的列分别指定中文列名:

学号、课程号、原始成绩、新成绩

select'学号'=Student_id,

Course_idas'课程号',

Student_grade'原始成绩',

Student_grade+5as'新成绩'

fromstudent_course

7.3控制select查询结果集输出的行

7.3.1ALL和Distinct关键字

1.使用ALL参数

--在表students中不带all参数查询所有学生的性别

selectStudent_sexfromstudents

--在表students中查询所有学生的性别

selectallStudent_sexfromstudents

2.使用Distinct参数

--在表students中查询所有学生的性别,要求消除重复数据

selectdistinctStudent_sexfromstudents

7.3.2使用ORDERBY子句对结果集排序

--在表studens中查询所有学生的学生记录,并使显示的结果根据姓名字段(Student_name)按照不同的方式排序

--对结果集不使用任何其他排序查询

select*fromstudents

--使结果集按照姓名字段的默认的升序方式排序

select*fromstudentsorderbyStudent_name

--使结果集按照姓名字段的升序方式排序

select*fromstudentsorderbyStudent_nameasc

--使结果集按照姓名字段的降序方式排序

select*fromstudentsorderbyStudent_namedesc

Student_sex)的降序排序,性

--在表students中查询所有的学生记录,要求首先按照性别字段(别相同的按照姓名(Student_name)的升序排序

select*fromstudentsorderbyStudent_sexdesc*Student_nameasc

733使用TOP关键字输出部分记录

--在表Student中查询所有的学生记录,但是只显示前面的3条记录selecttop3*fromstudents

--在表Student中查询所有的学生记录,但是只显示结果集记录总数33%的记录

selecttop33percent*fromstudents

--在表Student中查询所有的学生记录,结果集按性别的升序排序,但是只是显示前3条记录

selecttop3*fromstudentsorderbyStudent_sex

--在表Student中查询所有学生的记录,结果集按性别字段的升序排序,除了要显示3前条记录

外,还要显示出与第3条记录的性别一样的所有其他记录selecttop3withties*fromstudentsorderbyStudent_sex--没有ORDERBY不能使用WITHTIES

7.4使用WHER子句查询

常用比较运算符

>

大于

<

小于

=

等于

<=

小于或等于

>=

大于或等于

!

=

不等于

<>

不等于

!

<

不小于

!

>

不大于

7.4.2使用单一查询条件

--在成绩表中查询所有学生的课程号为1001的课程的成绩

selectStudent_id,Course_id,Student_grade

fromstudent_course

whereCourse_id='1001'

7.4.3逻辑运算符

1.AND逻辑运算符

--查询课程号为1001的课程且成绩在分80以上的所有学生的成绩selectStudent_id,Course_id,Student_grade

fromstudent_course

whereCourse_id='1001'andStudent_grade>=80

2.OR逻辑运算符

--在成绩表中查询成绩在90分以上(含90)或成绩在70分以下(不含70)的所有学生的成绩selectStudent_id,Course_id,Student_grade

fromstudent_course

whereStudent_grade>=90orStudent_grade<70

3.NOT逻辑运算符

--在成绩表中查询成绩在90分以上(含90)和成绩在70分以下(不含70),也就是不在70~90分之间

selectStudent_id,Course_id,Student_grade

fromstudent_course

wherenot(Student_grade>=70andStudent_grade<90)

4.BETWEEN和NOTBETWEEN逻辑运算符

--查询成绩在70~75分之间的成绩,则就是起始值,就是终止值selectStudent_id,Course_id,Student_grade

fromstudent_course

whereStudent_gradebetween70and75

--查询成绩在70分以下(不含0)和90分以上(含90)的所有学生的成绩

selectStudent_id,Course_id,Student_grade

fromstudent_course

whereStudent_gradenotbetween70and89.9

5.IN和NOTIN逻辑运算符

--用IN查询学号为011001、013001、014001的三个学生的信息

select*fromstudents

whereStudent_idin('011001','013001','014001')

--用OR查询学号为011001、013001、014001的三个学生的信息

select*fromstudents

whereStudent_id='011001'orStudent_id='013001'orStudent_id='014001'

--查询除了学号为011001、013001、014001的三个学生以外的其他所有学生信息

select*fromstudents

whereStudent_idnotin('011001','013001','014001')

7.4.4使用LIKE和通配符经行迷糊查询

1.百分号(%)通配符

--查询所有姓“叶”的同学

select*fromstudents

whereStudent_namelike'叶%'

2.下划线(_)通配符

--查询所有姓“叶”的同学,且名字只有三个字

select*fromstudents

whereStudent_namelike'叶__'

3.方括号([])通配符

--查询所有“叶”姓和“安”姓的同学信息

select*fromstudents

whereStudent_namelike'[叶安]%'--'[叶安]%'与'[叶,安]%'作用相同

--查询所有“华”姓和“杨”姓之间的所有学生的信息

select*fromstudents

whereStudent_namelike'[华-杨]%'

4.[A]通配符

--查询除了“华”姓和“杨”姓之外的所有学生信息

select*fromstudents

whereStudent_namelike'[A华,杨]%'

7.5FROM子句

7.5.2多表连接查询

1.内连接

--查询学生的学号、姓名、课程号和成绩

selectstudents.Student_id,students.Student_name,student_course.Course_id,student_course.Student_grade

fromstudentsinnerjoinstudent_course

onstudents.Student_id=student_course.Student_id

2.外连接

(1).左链接

--查询所有课程的课程名和相关学生的成绩,包括没有登记过成绩的课程,左连接

selectcourse.Course_name,student_course.Student_id,student_course.Student_gradefromcourseleftjoinstudent_course

onstudent_course.Course_id=course.Course_id

orderbyStudent_grade

(2).右连接

--查询所有课程的课程名和相关学生的成绩,包括没有登记过成绩的课程,右连接

selectcourse.Course_name,student_course.Student_id,student_course.Student_gradefromstudent_courserightjoincourse

onstudent_course.Course_id=course.Course_id

orderbyStudent_grade

(3).完全连接

--查询所有课程的课程名和相关学生的成绩,包括没有登记过成绩的课程,完全连接selectcourse.Course_name,student_course.Student_id,student_course.Student_gradefromstudent_coursefulljoincourse--“学生成绩表”为主表,而“课程表”为从表onstudent_course.Course_id=course.Course_id

orderbyStudent_grade--返回结果按照“学生课程表”中的数据经行匹配

4.自连接

--查询有相同名字的学生信息

select*fromstudentsa

wherea.Student_namein

(selectb.student_namefromstudentsb

groupbyb.Student_name

havingCOUNT(*)>1)

orderbya.Student_name

4.交叉连接

--交叉连接

select*fromstudents,course

7.5.3为查询数据源指定别名

--查询学生的姓名、课程名和成绩

selectST.Student_name,CS.Course_name,SC.Student_grade

fromstudentsasSTinnerjoinstudent_courseasSC

onST.Student_id=SC.Student_id

innerjoincourseCS

onCS.Course_id=SC.Course_id

7.6聚合函数

761使用SUM函数计算列的累加和

--查看成绩表中的所有学生成绩并排序

selectStudent_gradefromstudent_courseorderbyStudent_grade

--求所有成绩的总和

selectSUM(allStudent_grade)as总成绩fromstudent_course

--排除成绩中的所有重复值以后求和

selectSUM(distinctStudent_grade)as总成绩fromstudent_course

--查询并计算出学号为的学生的各科成绩的总成绩

selectSUM(Student_grade)as总成绩fromstudent_course

whereStudent_id='011001'

762使用AVG函数计算列的平均值

--查询并计算出学号为的学生的各科平均成绩

selectAVG(Student_grade)as平均成绩fromstudent_coursewhereStudent_id='011001'

763使用COUN■函数统计行的数目

--统计所有学生人数

selectCOUNT(Student_id)as学生总数fromstudents

764使用MAX函数计算列的最大值

--查询课程代码为的课程的最高分

selectMAX(Student_grade)as最高分fromstudent_coursewhereCourse_id='1001'

765使用MIN函数计算列的最小值

--查询课程代码为的课程的最低分

selectMIN(Student_grade)as最高分fromstudent_coursewhereCourse_id='1001'

7.7分组查询

7.7.1简单分组查询

--查询每个同学的所有课程的总分,并按总成绩的升序排序

selectStudent_id,SUM(Student_grade)as总分

fromstudent_course

groupbyStudent_id

orderbySUM(Student_grade)

7.7.2对分组进行汇总计算

1.WITHCUBE运算符

--求每个同学的平均成绩,一般查询

selectSt.Student_idas学号,Student_classidas班级,AVG(Student_grade)as平均分fromstudentsasStinnerjoinstudent_courseasSConSt.Student_id=SC.Student_idgroupbySt.Student_id,Student_classid

--求每个同学的平均成绩,使用WITHCUBE

selectSt.Student_idas学号,Student_classidas班级,AVG(Student_grade)as平均分fromstudentsasStinnerjoinstudent_courseasSC

onSt.Student_id=SC.Student_id

groupbySt.Student_id,Student_classidwithcube

2.WITHROLLUP运算符

--查询每个学生的平均分、每个班级的平均分、所有学生的平均分

selectStudent_classidas班级,St.Student_idas学号,AVG(Student_grade)as平均分fromstudentsasStinnerjoinstudent_courseasSC

onSt.Student_id=SC.Student_id

groupbyStudent_classid,StStudent_idwithrollup

--查询每个学生的平均分、每个班级的平均分、所有学生的平均分,分组表达式中的列的顺序交换

selectStudent_classidas班级,St.Student_idas学号,AVG(Student_grade)as平均分

fromstudentsasStinnerjoinstudent_courseasSC

onSt.Student_id=SC.Student_id

groupbySt.Student_id,Student_classidwithrollup

7.7.3分组条件查询(HAVING子句)

--求平均分高于分的所有学生

selectSt.Student_idas学号,Student_classidas班级,AVG(Student_grade)as平均分

fromstudentsasStinnerjoinstudent_courseasS

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2