正文共:4832 字 22 图 预计阅读时间:13 分钟
前文推送
本文目录:
- 5.2 sql笔试50题后25题
5. SQL面试50题
-
26.查询每门课程被选修的学生数
11 2 3 4 5 6 7-- 此题只使用Score单表也可以 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25select 26 27 28 29 30 31 32 33 34 35 36 373 c.cname, 38 39 40 41 42 43 44 45 46 47 48 494 50 51 52 53 54 55count(s.sid) 56 57 58 59 60 61as 62 63 64 65 66 67'选课人数' 68 69 70 71 72 73 74 75 76 77 78 795 80 81 82 83 84 85from Score s, Course c 86 87 88 89 90 91 92 93 94 95 96 976 98 99 100 101 102 103where s.cid = c.cid 104 105 106 107 108 109 110 111 112 113 114 1157 116 117 118 119 120 121group 122 123 124 125 126 127by c.cname 128 129 130 131 132 133 134

sql50_26
-
27.查询出只选修了一门课程的全部学生的学号和姓名
11 2 3 4 5 6 7-- 此题可以在第三题基础上增加限制 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25-- 没有这样的学生。 26 27 28 29 30 31 32 33 34 35 36 373 38 39 40 41 42 43SELECT a.sid,a.sname, 44 45 46 47 48 49 50 51 52 53 54 554 56 57 58 59 60 61count(b.cid) 62 63 64 65 66 67as 68 69 70 71 72 73'选课数' 74 75 76 77 78 79 80 81 82 83 84 855 86 87 88 89 90 91FROM Student a 92 93 94 95 96 97 98 99 100 101 102 1036 104 105 106 107 108 109left 110 111 112 113 114 115join Score b 116 117 118 119 120 121 122 123 124 125 126 1277 128 129 130 131 132 133on a.sid = b.sid 134 135 136 137 138 139 140 141 142 143 144 1458 146 147 148 149 150 151group 152 153 154 155 156 157by a.sid,a.sname 158 159 160 161 162 163 164 165 166 167 168 1699 170 171 172 173 174 175having 176 177 178 179 180 181count(b.cid) = 182 183 184 185 186 1871 188 189 190 191 192 193 194 -
28.查询男生、女生人数
11 2 3 4 5 6 7SELECT 8 9 10 11 12 13 14 15 16 17 18 192 ssex, 20 21 22 23 24 25 26 27 28 29 30 313 32 33 34 35 36 37count( 38 39 40 41 42 43sid) 44 45 46 47 48 49as 50 51 52 53 54 55'人数' 56 57 58 59 60 61 62 63 64 65 66 674 68 69 70 71 72 73FROM Student 74 75 76 77 78 79 80 81 82 83 84 855 86 87 88 89 90 91GROUP 92 93 94 95 96 97BY ssex 98 99 100 101 102 103 104 -
29.查询名字中含有"风"字的学生信息
11 2 3 4 5 6 7SELECT 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25sid, 26 27 28 29 30 31 32 33 34 35 36 373 sname, 38 39 40 41 42 43 44 45 46 47 48 494 sage, 50 51 52 53 54 55 56 57 58 59 60 615 ssex 62 63 64 65 66 67 68 69 70 71 72 736 74 75 76 77 78 79FROM Student 80 81 82 83 84 85 86 87 88 89 90 917 92 93 94 95 96 97WHERE sname 98 99 100 101 102 103like N 104 105 106 107 108 109'%风%' 110 111 112 113 114 115--编码原因加了N,视实际情况而定 116 117 118 119 120 121 122

sql50_29
-
30.查询同名同性学生名单,并统计同名人数
11 2 3 4 5 6 7-- 根据姓名和性别分组即可 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25SELECT 26 27 28 29 30 31 32 33 34 35 36 373 sname, 38 39 40 41 42 43 44 45 46 47 48 494 ssex, 50 51 52 53 54 55 56 57 58 59 60 615 62 63 64 65 66 67count( 68 69 70 71 72 73sid) 74 75 76 77 78 79 80 81 82 83 84 856 86 87 88 89 90 91FROM Student 92 93 94 95 96 97 98 99 100 101 102 1037 104 105 106 107 108 109GROUP 110 111 112 113 114 115BY sname,ssex 116 117 118 119 120 121 122

sql50_30
-
31.查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)
11 2 3 4 5 6 7SELECT 8 9 10 11 12 13 14 15 16 17 18 192 * 20 21 22 23 24 25 26 27 28 29 30 313 32 33 34 35 36 37FROM Student 38 39 40 41 42 43 44 45 46 47 48 494 50 51 52 53 54 55WHERE 56 57 58 59 60 61year(sage) = 62 63 64 65 66 671990 68 69 70 71 72 73 74

sql50_31
-
32.查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
11 2 3 4 5 6 7-- 同第十九题 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25select 26 27 28 29 30 31 32 33 34 35 36 373 s.cid, 38 39 40 41 42 43 44 45 46 47 48 494 c.cname, 50 51 52 53 54 55 56 57 58 59 60 615 62 63 64 65 66 67AVG(s.score) 68 69 70 71 72 73as mean_score 74 75 76 77 78 79 80 81 82 83 84 856 86 87 88 89 90 91from Score s, Course c 92 93 94 95 96 97 98 99 100 101 102 1037 104 105 106 107 108 109where s.cid = c.cid 110 111 112 113 114 115 116 117 118 119 120 1218 122 123 124 125 126 127group 128 129 130 131 132 133by s.cid,c.cname 134 135 136 137 138 139 140 141 142 143 144 1459 146 147 148 149 150 151order 152 153 154 155 156 157by 158 159 160 161 162 163AVG(s.score) 164 165 166 167 168 169asc, s.cid 170 171 172 173 174 175desc 176 177 178 179 180 181 182

sql50_32
-
33.查询不及格的课程,并按课程号从大到小排列
1 1 2 3 4 5 6 7select 8 9 10 11 12 13 14 15 16 17 18 19 2 sc.cid, 20 21 22 23 24 25 26 27 28 29 30 31 3 s.sname, 32 33 34 35 36 37 38 39 40 41 42 43 4 c.cname, 44 45 46 47 48 49 50 51 52 53 54 55 5 sc.score 56 57 58 59 60 61 62 63 64 65 66 67 6 68 69 70 71 72 73from Score sc, Course c, Student s 74 75 76 77 78 79 80 81 82 83 84 85 7 86 87 88 89 90 91where sc.cid = c.cid 92 93 94 95 96 97 98 99 100 101 102 103 8 104 105 106 107 108 109and sc.sid = s.sid 110 111 112 113 114 115 116 117 118 119 120 121 9 122 123 124 125 126 127and sc.score < 128 129 130 131 132 13360 134 135 136 137 138 139 140 141 142 143 144 14510 146 147 148 149 150 151order 152 153 154 155 156 157by sc.cid 158 159 160 161 162 163desc 164 165 166 167 168 169 170

sql50_33
-
34.查询课程编号为"01"且课程成绩在60分以上的学生的学号和姓名
11 2 3 4 5 6 7select 8 9 10 11 12 13 14 15 16 17 18 192 s.sid, 20 21 22 23 24 25 26 27 28 29 30 313 s.sname, 32 33 34 35 36 37 38 39 40 41 42 434 sc.score 44 45 46 47 48 49 50 51 52 53 54 555 56 57 58 59 60 61from Score sc, Course c, Student s 62 63 64 65 66 67 68 69 70 71 72 736 74 75 76 77 78 79where sc.cid = c.cid 80 81 82 83 84 85 86 87 88 89 90 917 92 93 94 95 96 97and sc.sid = s.sid 98 99 100 101 102 103 104 105 106 107 108 1098 110 111 112 113 114 115and sc.cid = 116 117 118 119 120 121'01' 122 123 124 125 126 127 128 129 130 131 132 1339 134 135 136 137 138 139and sc.score > 140 141 142 143 144 14560 146 147 148 149 150 151 152

sql50_34
-
35.查询所有学生的课程及分数情况
1 1 2 3 4 5 6 7-- 查看每个人的年龄,性别,三门课成绩 8 9 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25-- 就是在开头使用的用于便捷判断结果的 all_info 26 27 28 29 30 31 32 33 34 35 36 37 3 38 39 40 41 42 43-- 利用了pivot来行转列 44 45 46 47 48 49 50 51 52 53 54 55 4 56 57 58 59 60 61select 62 63 64 65 66 67 68 69 70 71 72 73 5 74 75 76 77 78 79sid,sname,sage,ssex,[语文],[数学],[英语] 80 81 82 83 84 85 86 87 88 89 90 91 6 92 93 94 95 96 97from 98 99 100 101 102 103 104 105 106 107 108 109 7( 110 111 112 113 114 115 116 117 118 119 120 121 8 122 123 124 125 126 127select a.sid,a.sname,a.sage,a.ssex,c.cname,b.score 128 129 130 131 132 133 134 135 136 137 138 139 9 140 141 142 143 144 145from Student a 146 147 148 149 150 151 152 153 154 155 156 15710 158 159 160 161 162 163left 164 165 166 167 168 169join Score b 170 171 172 173 174 175 176 177 178 179 180 18111 182 183 184 185 186 187on a.sid=b.sid 188 189 190 191 192 193 194 195 196 197 198 19912 200 201 202 203 204 205left 206 207 208 209 210 211join Course c 212 213 214 215 216 217 218 219 220 221 222 22313 224 225 226 227 228 229on b.cid = c.cid 230 231 232 233 234 235 236 237 238 239 240 24114) source_table 242 243 244 245 246 247 248 249 250 251 252 25315 254 255 256 257 258 259pivot( 260 261 262 263 264 265 266 267 268 269 270 27116 272 273 274 275 276 277sum(score) 278 279 280 281 282 283for 284 285 286 287 288 289 290 291 292 293 294 29517cname 296 297 298 299 300 301in ( 302 303 304 305 306 307 308 309 310 311 312 31318 [语文],[数学],[英语] 314 315 316 317 318 319 320 321 322 323 324 32519) 326 327 328 329 330 331 332 333 334 335 336 33720 ) t 338 339 340 341 342 343 344

sql50_35
-
36.查询任何一门课程成绩在70分以上的姓名、课程名称和分数
11 2 3 4 5 6 7select 8 9 10 11 12 13 14 15 16 17 18 192 s.sname, 20 21 22 23 24 25 26 27 28 29 30 313 c.cname, 32 33 34 35 36 37 38 39 40 41 42 434 sc.score 44 45 46 47 48 49 50 51 52 53 54 555 56 57 58 59 60 61from Score sc, Course c, Student s 62 63 64 65 66 67 68 69 70 71 72 736 74 75 76 77 78 79where sc.cid = c.cid 80 81 82 83 84 85 86 87 88 89 90 917 92 93 94 95 96 97and sc.sid = s.sid 98 99 100 101 102 103 104 105 106 107 108 1098 110 111 112 113 114 115and sc.score > 116 117 118 119 120 12170 122 123 124 125 126 127 128

sql50_36
-
37.查询课程名称为"数学",且分数低于60的学生姓名和分数
11 2 3 4 5 6 7select 8 9 10 11 12 13 14 15 16 17 18 192 s.sname, 20 21 22 23 24 25 26 27 28 29 30 313 sc.score 32 33 34 35 36 37 38 39 40 41 42 434 44 45 46 47 48 49from Score sc, Course c, Student s 50 51 52 53 54 55 56 57 58 59 60 615 62 63 64 65 66 67where sc.cid = c.cid 68 69 70 71 72 73 74 75 76 77 78 796 80 81 82 83 84 85and sc.sid = s.sid 86 87 88 89 90 91 92 93 94 95 96 977 98 99 100 101 102 103and sc.score < 104 105 106 107 108 10960 110 111 112 113 114 115 116 117 118 119 120 1218 122 123 124 125 126 127and c.cname = N 128 129 130 131 132 133'数学' 134 135 136 137 138 139 140

sql50_37
-
38.查询课程编号为03且课程成绩在80分以上的学生的学号和姓名
1 1 2 3 4 5 6 7-- 和第三十四题是一样的,混进来的题目? 8 9 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25select 26 27 28 29 30 31 32 33 34 35 36 37 3 s.sid, 38 39 40 41 42 43 44 45 46 47 48 49 4 s.sname, 50 51 52 53 54 55 56 57 58 59 60 61 5 sc.score 62 63 64 65 66 67 68 69 70 71 72 73 6 74 75 76 77 78 79from Score sc, Course c, Student s 80 81 82 83 84 85 86 87 88 89 90 91 7 92 93 94 95 96 97where sc.cid = c.cid 98 99 100 101 102 103 104 105 106 107 108 109 8 110 111 112 113 114 115and sc.sid = s.sid 116 117 118 119 120 121 122 123 124 125 126 127 9 128 129 130 131 132 133and sc.cid = 134 135 136 137 138 139'03' 140 141 142 143 144 145 146 147 148 149 150 15110 152 153 154 155 156 157and sc.score > 158 159 160 161 162 16380 164 165 166 167 168 169 170

sql50_38
-
39.求每门课程的学生人数
11 2 3 4 5 6 7-- 混进来的题目? 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25select 26 27 28 29 30 31 32 33 34 35 36 373 cid, 38 39 40 41 42 43 44 45 46 47 48 494 50 51 52 53 54 55count( 56 57 58 59 60 61sid) 62 63 64 65 66 67 68 69 70 71 72 735 74 75 76 77 78 79from Score 80 81 82 83 84 85 86 87 88 89 90 916 92 93 94 95 96 97group 98 99 100 101 102 103by cid 104 105 106 107 108 109 110 -
40.查询选修“张三”老师所授课程的学生中,成绩最高的学生姓名及其成绩
11 2 3 4 5 6 7-- 利用 top 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25select 26 27 28 29 30 31 32 33 34 35 36 373 top 38 39 40 41 42 431 s.sid, s.sname, sc.score 44 45 46 47 48 49 50 51 52 53 54 554 56 57 58 59 60 61from Score sc, Course c, Teacher t, Student s 62 63 64 65 66 67 68 69 70 71 72 735 74 75 76 77 78 79where sc.cid = c.cid 80 81 82 83 84 85 86 87 88 89 90 916 92 93 94 95 96 97and c.tid=t.tid 98 99 100 101 102 103 104 105 106 107 108 1097 110 111 112 113 114 115and sc.sid = s.sid 116 117 118 119 120 121 122 123 124 125 126 1278 128 129 130 131 132 133and t.tname=N 134 135 136 137 138 139'张三' 140 141 142 143 144 145 146

sql50_40
-
41.查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
1 1 2 3 4 5 6 7-- 同表级联查询 8 9 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25select 26 27 28 29 30 31 32 33 34 35 36 37 3 38 39 40 41 42 43distinct 44 45 46 47 48 49 50 51 52 53 54 55 4 s1.sid, 56 57 58 59 60 61 62 63 64 65 66 67 5 s1.cid, 68 69 70 71 72 73 74 75 76 77 78 79 6 s1.score 80 81 82 83 84 85 86 87 88 89 90 91 7 92 93 94 95 96 97from Score s1, Score s2 98 99 100 101 102 103 104 105 106 107 108 109 8 110 111 112 113 114 115where s1.sid = s2.sid 116 117 118 119 120 121 122 123 124 125 126 127 9 128 129 130 131 132 133and s1.score = s2.score 134 135 136 137 138 139 140 141 142 143 144 14510 146 147 148 149 150 151and s1.cid != s2.cid 152 153 154 155 156 157 158

sql50_41
-
42.查询每门功课成绩最好的前两名
1 1 2 3 4 5 6 7-- 同第二十二题和第二十五题 8 9 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25-- 26 27 28 29 30 31 32 33 34 35 36 37 3 38 39 40 41 42 43-- row_number() over(partition by 分组字段 order by 排序字段 排序方式) as 别名 44 45 46 47 48 49 50 51 52 53 54 55 4 56 57 58 59 60 61select * 62 63 64 65 66 67from ( 68 69 70 71 72 73 74 75 76 77 78 79 5 80 81 82 83 84 85select 86 87 88 89 90 91 92 93 94 95 96 97 6 sc.sid, 98 99 100 101 102 103 104 105 106 107 108 109 7 s.sname, 110 111 112 113 114 115 116 117 118 119 120 121 8 s.ssex, 122 123 124 125 126 127 128 129 130 131 132 133 9 s.sage, 134 135 136 137 138 139 140 141 142 143 144 14510 c.cname, 146 147 148 149 150 151 152 153 154 155 156 15711 sc.score, 158 159 160 161 162 163 164 165 166 167 168 16912 ROW_NUMBER() 170 171 172 173 174 175over( 176 177 178 179 180 181partition 182 183 184 185 186 187BY sc.cid 188 189 190 191 192 193order 194 195 196 197 198 199by score 200 201 202 203 204 205desc) 206 207 208 209 210 211as myrank 212 213 214 215 216 217 218 219 220 221 222 22313 224 225 226 227 228 229from Score sc,Student s,Course c 230 231 232 233 234 235 236 237 238 239 240 24114 242 243 244 245 246 247where sc.sid = s.sid 248 249 250 251 252 253 254 255 256 257 258 25915 260 261 262 263 264 265and sc.cid = c.cid) t 266 267 268 269 270 271 272 273 274 275 276 27716 278 279 280 281 282 283where t.myrank < 284 285 286 287 288 2893 290 291 292 293 294 295 296

sql50_42
-
43.统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
11 2 3 4 5 6 7select 8 9 10 11 12 13 14 15 16 17 18 192 cid, 20 21 22 23 24 25 26 27 28 29 30 313 32 33 34 35 36 37count( 38 39 40 41 42 43sid) 44 45 46 47 48 49as 50 51 52 53 54 55'选修人数' 56 57 58 59 60 61 62 63 64 65 66 674 68 69 70 71 72 73from Score 74 75 76 77 78 79 80 81 82 83 84 855 86 87 88 89 90 91group 92 93 94 95 96 97by cid 98 99 100 101 102 103 104 105 106 107 108 1096 110 111 112 113 114 115having 116 117 118 119 120 121count( 122 123 124 125 126 127sid) > 128 129 130 131 132 1335 134 135 136 137 138 139 140 141 142 143 144 1457 146 147 148 149 150 151order 152 153 154 155 156 157by 158 159 160 161 162 163count( 164 165 166 167 168 169sid) 170 171 172 173 174 175desc, cid 176 177 178 179 180 181asc 182 183 184 185 186 187 188

sql50_43
-
44.检索至少选修两门课程的学生学号
11 2 3 4 5 6 7select 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25sid, 26 27 28 29 30 31 32 33 34 35 36 373 38 39 40 41 42 43count(cid) 44 45 46 47 48 49as 50 51 52 53 54 55'选修课程数' 56 57 58 59 60 61 62 63 64 65 66 674 68 69 70 71 72 73from Score 74 75 76 77 78 79 80 81 82 83 84 855 86 87 88 89 90 91group 92 93 94 95 96 97by 98 99 100 101 102 103sid 104 105 106 107 108 109 110 111 112 113 114 1156 116 117 118 119 120 121having 122 123 124 125 126 127count(cid) >= 128 129 130 131 132 1332 134 135 136 137 138 139 140

sql50_44
-
45.查询选修了全部课程的学生信息
11 2 3 4 5 6 7-- 同第十题(条件相反) 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25SELECT a.sid,a.sname, 26 27 28 29 30 31 32 33 34 35 36 373 38 39 40 41 42 43count(b.cid) 44 45 46 47 48 49as 50 51 52 53 54 55'选课数' 56 57 58 59 60 61 62 63 64 65 66 674 68 69 70 71 72 73FROM Student a 74 75 76 77 78 79 80 81 82 83 84 855 86 87 88 89 90 91left 92 93 94 95 96 97join Score b 98 99 100 101 102 103 104 105 106 107 108 1096 110 111 112 113 114 115on a.sid = b.sid 116 117 118 119 120 121 122 123 124 125 126 1277 128 129 130 131 132 133group 134 135 136 137 138 139by a.sid,a.sname 140 141 142 143 144 145 146 147 148 149 150 1518 152 153 154 155 156 157having 158 159 160 161 162 163count(b.cid) = ( 164 165 166 167 168 169select 170 171 172 173 174 175count( 176 177 178 179 180 181distinct cid) 182 183 184 185 186 187from Course) 188 189 190 191 192 193 194 195 196 197 198 1999 200 201 202 203 204 205order 206 207 208 209 210 211by a.sid 212 213 214 215 216 217 218

sql50_45
-
46.查询各学生的年龄
11 2 3 4 5 6 7-- 利用SYSDATETIME()/getdate() 获取当前时间 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25SELECT SYSDATETIME(); 26 27 28 29 30 31 32 33 34 35 36 373 38 39 40 41 42 43SELECT 44 45 46 47 48 49 50 51 52 53 54 554 56 57 58 59 60 61sid, 62 63 64 65 66 67 68 69 70 71 72 735 sname, 74 75 76 77 78 79 80 81 82 83 84 856 86 87 88 89 90 91year(SYSDATETIME()) - 92 93 94 95 96 97year(sage) 98 99 100 101 102 103AS 104 105 106 107 108 109'年龄' 110 111 112 113 114 115 116 117 118 119 120 1217 122 123 124 125 126 127FROM Student 128 129 130 131 132 133 134

sql50_46
-
47.查询本周过生日的学生
11 2 3 4 5 6 7select 8 9 10 11 12 13getdate(); 14 15 16 17 18 19 20 21 22 23 24 252 26 27 28 29 30 31select 32 33 34 35 36 37DATEADD(wk, 38 39 40 41 42 43DATEDIFF(wk, 44 45 46 47 48 490, 50 51 52 53 54 55getdate()), 56 57 58 59 60 610); 62 63 64 65 66 67-- 本周周一 68 69 70 71 72 73 74 75 76 77 78 793 80 81 82 83 84 85select 86 87 88 89 90 91DATEADD(wk, 92 93 94 95 96 97DATEDIFF(wk, 98 99 100 101 102 1030, 104 105 106 107 108 109getdate()), 110 111 112 113 114 1157) ; 116 117 118 119 120 121-- 下周周一 122 123 124 125 126 127 128 129 130 131 132 1334 134 135 136 137 138 139SELECT 140 141 142 143 144 145 146 147 148 149 150 1515 * 152 153 154 155 156 157 158 159 160 161 162 1636 164 165 166 167 168 169FROM Student 170 171 172 173 174 175 176 177 178 179 180 1817 182 183 184 185 186 187where 188 189 190 191 192 193DATEADD( 194 195 196 197 198 199year, 200 201 202 203 204 205year( 206 207 208 209 210 211getdate())- 212 213 214 215 216 217year(sage), sage) 218 219 220 221 222 223between 224 225 226 227 228 229 230 231 232 233 234 2358 236 237 238 239 240 241DATEADD(wk, 242 243 244 245 246 247DATEDIFF(wk, 248 249 250 251 252 2530, 254 255 256 257 258 259getdate()), 260 261 262 263 264 2650) 266 267 268 269 270 271 272 273 274 275 276 2779 278 279 280 281 282 283and 284 285 286 287 288 289DATEADD(wk, 290 291 292 293 294 295DATEDIFF(wk, 296 297 298 299 300 3010, 302 303 304 305 306 307getdate()), 308 309 310 311 312 3137) 314 315 316 317 318 319 320

sql50_47
-
48.查询下周过生日的学生
1 1 2 3 4 5 6 7-- 同第四十七题 8 9 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25select 26 27 28 29 30 31getdate(); 32 33 34 35 36 37 38 39 40 41 42 43 3 44 45 46 47 48 49select 50 51 52 53 54 55DATEADD(wk, 56 57 58 59 60 61DATEDIFF(wk, 62 63 64 65 66 670, 68 69 70 71 72 73getdate()), 74 75 76 77 78 790); 80 81 82 83 84 85-- 本周周一 86 87 88 89 90 91 92 93 94 95 96 97 4 98 99 100 101 102 103select 104 105 106 107 108 109DATEADD(wk, 110 111 112 113 114 115DATEDIFF(wk, 116 117 118 119 120 1210, 122 123 124 125 126 127getdate()), 128 129 130 131 132 1337) ; 134 135 136 137 138 139-- 下周周一 140 141 142 143 144 145 146 147 148 149 150 151 5 152 153 154 155 156 157SELECT 158 159 160 161 162 163 164 165 166 167 168 169 6 * 170 171 172 173 174 175 176 177 178 179 180 181 7 182 183 184 185 186 187FROM Student 188 189 190 191 192 193 194 195 196 197 198 199 8 200 201 202 203 204 205where 206 207 208 209 210 211DATEADD( 212 213 214 215 216 217year, 218 219 220 221 222 223year( 224 225 226 227 228 229getdate())- 230 231 232 233 234 235year(sage), sage) 236 237 238 239 240 241between 242 243 244 245 246 247 248 249 250 251 252 253 9 254 255 256 257 258 259DATEADD(wk, 260 261 262 263 264 265DATEDIFF(wk, 266 267 268 269 270 2710, 272 273 274 275 276 277getdate()), 278 279 280 281 282 2837) 284 285 286 287 288 289 290 291 292 293 294 29510 296 297 298 299 300 301and 302 303 304 305 306 307DATEADD(wk, 308 309 310 311 312 313DATEDIFF(wk, 314 315 316 317 318 3190, 320 321 322 323 324 325getdate()), 326 327 328 329 330 33114) 332 333 334 335 336 337 338 -
49.查询本月过生日的学生
11 2 3 4 5 6 7-- 利用getdate() 获取当前时间, month()获得月份 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25SELECT 26 27 28 29 30 31getdate(); 32 33 34 35 36 37 38 39 40 41 42 433 44 45 46 47 48 49select 50 51 52 53 54 55 56 57 58 59 60 614 62 63 64 65 66 67sid, 68 69 70 71 72 73 74 75 76 77 78 795 sname, 80 81 82 83 84 85 86 87 88 89 90 916 sage, 92 93 94 95 96 97 98 99 100 101 102 1037 ssex 104 105 106 107 108 109 110 111 112 113 114 1158 116 117 118 119 120 121from Student 122 123 124 125 126 127 128 129 130 131 132 1339 134 135 136 137 138 139where 140 141 142 143 144 145month(sage) = 146 147 148 149 150 151month( 152 153 154 155 156 157getdate()) 158 159 160 161 162 163 164

sql50_49
-
50.查询下月过生日的学生
11 2 3 4 5 6 7-- 同第四十九题 8 9 10 11 12 13 14 15 16 17 18 192 20 21 22 23 24 25SELECT 26 27 28 29 30 31getdate(); 32 33 34 35 36 37 38 39 40 41 42 433 44 45 46 47 48 49select 50 51 52 53 54 55 56 57 58 59 60 614 62 63 64 65 66 67sid, 68 69 70 71 72 73 74 75 76 77 78 795 sname, 80 81 82 83 84 85 86 87 88 89 90 916 sage, 92 93 94 95 96 97 98 99 100 101 102 1037 ssex 104 105 106 107 108 109 110 111 112 113 114 1158 116 117 118 119 120 121from Student 122 123 124 125 126 127 128 129 130 131 132 1339 134 135 136 137 138 139where 140 141 142 143 144 145month(sage) = 146 147 148 149 150 151month( 152 153 154 155 156 157getdate())+ 158 159 160 161 162 1631 164
本文项目地址:
https://github.com/firewang/sql50
(喜欢的话,Star一下)
阅读原文,或者访问该链接可以在线观看(该系列将更新至GitHub,并且托管到read the docs)
https://sql50.readthedocs.io/zh\_CN/latest/
参考网址:
PS:
1. 后台回复“线性代数”,“SQL” 等任一关键词获取资源链接
2. 后台回复“联系“, “投稿“, “加入“ 等任一关键词联系我们
3. 后台回复 “红包” 领取红包

零维领域,由内而外深入机器学习
dive into machine learning
微信号:零维领域
英文ID:lingweilingyu

本文分享自微信公众号 - 零维领域(lingweilingyu)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。