数据库简单查询笔记
1.select后面永远是字段
2.查找一张表里面的全部信息
select *
3.查询姓张王李赵的男生
我的做法:
select *
from dbo.Student
where Name like '张%'and SEX ='男'
select *
from Student
where Name like '王%'and SEX ='男'
select *
from Student
where Name like '李%'and SEX ='男'
select *
from Student
where Name like '赵%'and SEX ='男'
正确答案的做法:
'[张王李赵]%'
4.限制取值 查询课程编号为101,102,103三门课程的课程号和课程名。
select CourseID ,CourseName
from Course
where CourseID IN('101','102','103')
5.消除结果中的重复行:DISTINCT
限制结果返回行数:TOP: select top %50 NAME,SEX
6.查询1996至2000年出生的学生学号,姓名,和出生日期。
select StudentID,Name,Birthday
from Student
where Birthday between '1996' and '2000'
7.根据学生出生日期,将学生分为:低龄(17岁以下),正常(17至30),高龄(30岁以上),显示学号,姓名,出生日期,年龄段4列。
use college
go
select StudentID,Name,Birthday,RANK=
CASE
when Birthday>'2004' then'低龄'
when Birthday between '1992' and '2004' then'中龄'
else'高龄'
end
from Student
go
8.根据学生的出生日期,生成年龄列。
use college
go
select Name,YEAR(GETDATE())-YEAR(Birthday) AS '年龄'
from Student
go