单表查询 多表查询 单表查询.doc
文本预览下载声明
单表查询 多表查询 单表查询
导读:就爱阅读网友为您分享以下“单表查询”资讯,希望对您有所帮助,感谢您对92的支持!
单表查询
1 查询全体学生的学号与姓名
select sno,sname from student
2 查询全体学生的姓名,学号和所在的系
select sname,sno,sdept from student
3 查询全体学生的姓名与出生年份
select sname,2012-sage from student
4 查询全体学生的姓名和出生年份,并在出生年份列前加一个新列,新列的每一行数据均为 出生年份常量值
select sname,’出生年份’,2012-sage from student
选择表中的若干元组
一 消除取值相同的行
去掉查询结果中重复的行
select distinct sno from sc
二 查询满足条件的元组
1 比较大小
例1 查询计算机系全体学生的姓名
select sname from student where sdept=‘计算机系’
例2 查询所有年龄在20岁以下的学生的姓名和年龄
select sname ,sage from student where sagelt;20
例3 查询考试成绩有不及格的学生的学号
select distinct sno from sc where gradelt;60
2 确定范围
例1 查询年龄在20-23岁之间的学生的姓名,所在的系和年龄
select sname,sdept,sage from student where sage between 20 and 23
例2 查询年龄不再20-23岁之间的学生的姓名,所在的系和年龄
select sname,sdept,sage from student where sage not between 20 and 23
3 确定集合
例1 查询信息系,数学系和计算机系的学生的姓名和性别
select sname,ssex from student where sdept in(‘计算机系’,’数学系’,’信息系’)
例2 查询既不是信息系,数学系和计算机系的学生和性别
select sname,ssex from student where sdept not in(‘计算机系’,’数学系’,’信息系’)
4 字符串匹配
例1 查询姓张的学生的详细信息
select *from student where sname like’张%’
例2 查询学生表中姓张,姓李,姓刘的学生的详细信息
select *from student where sname like ‘[张李刘]%’
例3 查询所有不姓王也不姓张的学生姓名lect sno,cno from sc where grade is null
例2 查询计算机系年龄在20岁以下的学生姓名
select sname from student where sdept=‘计算机系’ and sagelt;20
例3 查询计算机系和信息系年龄大于等于20岁的学生姓名,所在的系和年龄。
select sname,sdept ,sage from student where (sdept=‘计算机系’ or sdept=‘信息系’) and sagegt;=20
三 对查询结果进行排序
例1 将学生年龄的升序排
列
select *from student order by sage asc
例2 查询选修了c02号课程的学生的学号及其成绩,结果按成绩降序排列 select sno,grade from sc where cno=‘c02’ order by grade desc
例3 查询全体学生的信息,结果按所在系名的升序排列,同一系的按年龄降序排列
select *from student order by sdept,sage desc
四 使用聚合函数汇总数据
例1 统计学生总人数
select count(*)from student
例2 统计选修了课程的学生人数
select count(distinct sno)from sc
例3 计算“9512101”学生的选课门数和考试成绩
select count(*)as 选课门数,sum (grade)as 总成绩 from sc where sno=‘9512101’
例4 查询“C01”课程的考试最高分和最低分
select max(grade)as 最高分,min (grade) as 最低分 fro
显示全部