1 use
2 SqlSchool
3 go
4
5 --选择所有列
6 select * from student
7
8 --查询学生的姓名,性别,专业和系,起友好列名
9 select stuName 学生姓名, stuSex as 性别,
10 stuSpeciality as 专业,
11 stuDept as 系
12 from student
13
14 --选择指定列
15 select stuAvgrade, stuName from student
16
17 --给列取别名
18
19 select stuAvgrade as 平均成绩, stuName as '姓名' from student
20
21
22 select stuAvgrade 平均成绩, stuName '姓名' from student
23
24
25 select stuAvgrade as 平均成绩, stuName as 姓名
26 from student --推荐写法
27
28
29 select * from student
30 --查询全体学生的出生年份
31
32 --year,time,year,
33
34
35
36
37 select year(stuBirth) as 出生年份 from student
38
39 select year('1987-02-23 00:00:00')
40
41 select 200*987
42
43 select * from student
44
45
46
47
48
49 --查询全体学生的出生月份
50
51 select month(stuBirth) from student
52
53
54 select day(stuBirth) from student
55
56
57 --查询全体学生的姓名和年龄
58
59 --0
60
61 select stuName, 年龄 from student
62
63 --1
64 year(当前日期) - year(stuBirth)
65 select getdate()
66 --2
67 year(getdate()) - year(stuBirth)
68 --3
69 select stuName, year(getdate()) - year(stuBirth) as 年龄 from student
70
71
72
73 --使用distinct查询学生所在系的名字
74
75 select distinct stuDept from student
76
77
78
79
80
81 --使用avg函数查询全体学生的平均成绩的平均值
82
83 --下面这些都使用了聚合函数
84
85 select sum(stuAvgrade) as 总成绩 from student
86
87 select avg(stuAvgrade) as 平均成绩 from student
88
89 select max(stuAvgrade) as 最高成绩 from student
90
91 select min(stuAvgrade) as 最小成绩 from student
92
93
94
95 --查询姓名为李好的学员的信息
96
97 select * from student
98 where stuName = '李好'
99
100
101 --查询学生编号为20060205的学员的信息
102
103 select * from student
104 where stuId = '20060205'
105
106
107
108 --查询学生平均成绩在70分以上的学员的信息
109
110 select * from student where stuAvgrade >= 70
111
112
113 --查询大于(小于)全体学生的平均成绩的平均值的学生信息
114
115
116
117 --1 select * from student where stuAvgrade > (全体学生的平均成绩)
118
119
120 -- 2
121 select * from student where stuAvgrade >
122 (
123 --独立子查询
124 select avg(stuAvgrade) from student
125 )
126
127 --使用Between查询所有出生在84年8月1日到86年12月25日之间的学生信息
128
129 select * from student where stuBirth between '1984-08-01' And '1986-12-25'
130
131 select * from student where stuAvgrade between 70 And 90
132
133 --查找 * 从 学生表 当 生日 在 '1984-08-01' 和 '1986-12-25' 之间的时候