mysql 查询语句学生表、课程表、 成绩表、教师表

阅读: 评论:0

2024年1月26日发(作者:)

mysql 查询语句学生表、课程表、 成绩表、教师表

【引用】学生表、课程表、 成绩表、教师表50个常用sql语句

2011-08-07 15:01:48| 分类: 数据库 | 标签: |字号大中小 订阅

本文引用自大河之舟《学生表、课程表、 成绩表、教师表50个常用sql语句》

001

--Student(S#,Sname,Sage,Ssex) --学生表

002

--Course(C#,Cname,T#) --课程表

003

--SC(S#,C#,score) --成绩表

004

--Teacher(T#,Tname) --教师表

005

006

create

table

Student(S# varchar(20),Sname varchar(10),Sage int,Ssex

varchar(2))

007

--前面加一列序号:

008

if

009

exists(select

table_name from

information_

010

where

table_name='Temp_Table')

011

drop

table

Temp_Table

012

go

013

select

排名=identity(int,1,1),* INTO

Temp_Table from

Student

014

go

015

select

* from

Temp_Table

016

go

017

018

drop

database

[ ] --删除空的没有名字的数据库

019

--问题:

020

--1、查询“”课程比“”课程成绩高的所有学生的学号;

021

select

a.S# from

(select

s#,score from

SC where

C#='001')

a,(select

s#,score

022

from

SC where

C#='002') b

023

where

> and

a.s#=b.s#;

024

025

--2、查询平均成绩大于分的同学的学号和平均成绩;

026

select

S#,avg(score)

027

from

sc

028

group

by

S# having

avg(score) >60;

029

030

--3、查询所有同学的学号、姓名、选课数、总成绩;

031

select

Student.S#,,count(SC.C#),sum(score)

032

from

Student left

Outer

join

SC on

Student.S#=SC.S#

033

group

by

Student.S#,Sname

034

035

--4、查询姓“李”的老师的个数;

036

select

count(distinct(Tname))

037

from

Teacher

038

where

Tname like

'李%';

039

040

--5、查询没学过“叶平”老师课的同学的学号、姓名;

041

select

Student.S#,

042

from

Student

where

S# not

in

(select

distinct( SC.S#) from

043

SC,Course,Teacher where

SC.C#=Course.C# and

Teacher.T#=Course.T#

and

='叶平');

044

045

--6、查询学过“”并且也学过编号“”课程的同学的学号、姓名;

select

Student.S#, from

Student,SC where

046

Student.S#=SC.S# and

SC.C#='001'and

exists( Select

* from

SC as

SC_2

where

SC_2.S#=SC.S# and

SC_2.C#='002');

047

048

--7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

049

select

S#,Sname

050

from

Student

where

S# in

(select

S# from

SC ,Course ,Teacher where

SC.C#=Course.C# and

Teacher.T#=Course.T# and

='叶平'

051

group

by

S# having

count(SC.C#)=(select

count(C#) from

Course,Teacher where

Teacher.T#=Course.T# and

Tname='叶平'));

052

053

--8、查询课程编号“”的成绩比课程编号“”课程低的所有同学的学号、姓名;

Select

S#,Sname from

(select

054

Student.S#,,score ,(select

score from

SC SC_2 where

SC_2.S#=Student.S# and

SC_2.C#='002') score2

055

from

Student,SC where

Student.S#=SC.S# and

C#='001') S_2 where

score2

056

057

--9、查询所有课程成绩小于分的同学的学号、姓名;

058

select

S#,Sname

059

from

Student

060

where

S# not

in

(select

Student.S# from

Student,SC where

S.S#=SC.S#

and

score>60);

061

062

--10、查询没有学全所有课的同学的学号、姓名;

063

select

Student.S#,

064

from

Student,SC

065

where

Student.S#=SC.S# group

by

Student.S#,

having

count(C#) <(select

count(C#) from

Course);

066

067

068

--11、查询至少有一门课与学号为“”的同学所学相同的同学的学号和姓名;

select

S#,Sname from

Student,SC where

Student.S#=SC.S# and

C#

in

select

C# from

SC where

S#='1001';

069

070

--12、查询至少学过学号为“”同学所有一门课的其他同学学号和姓名;

071

select

distinct

SC.S#,Sname

072

from

Student,SC

073

where

Student.S#=SC.S# and

C# in

(select

C# from

SC where

S#='001');

074

075

--13、把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

076

update

SC set

score=(select

avg(SC_)

077

from

SC SC_2

where

SC_2.C#=SC.C# ) from

Course,Teacher where

078

Course.C#=SC.C# and

Course.T#=Teacher.T# and

='叶平');

079

080

--14、查询和“”号的同学学习的课程完全相同的其他同学学号和姓名;

081

082

select

S# from

SC where

C# in

(select

C# from

SC where

S#='1002')

group

by

S# having

count(*)=(select

count(*) from

SC where

S#='1002');

083

084

--15、删除学习“叶平”老师课的SC表记录;

085

Delect SC

086

from

course ,Teacher

087

where

Course.C#=SC.C# and

Course.T#= Teacher.T# and

Tname='叶平';

088

089

--16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“”课程的同学学号、、

090

号课的平均成绩;

091

Insert

SC select

S#,'002',(Select

avg(score)

092

from

SC where

C#='002') from

Student where

S# not

in

(Select

S#

from

SC where

C#='002');

093

--17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英094

语”三门的课程成绩,按如下形式显示:学生ID,,数据库,企业管理,英语,有效课程数,有效平均分

095

SELECT

S# as

学生ID

096

097

098

,(SELECT

score FROM

SC WHERE

SC.S#=t.S# AND

C#='004')

AS

数据库

,(SELECT

score FROM

SC WHERE

SC.S#=t.S# AND

C#='001')

AS

企业管理

,(SELECT

score FROM

SC WHERE

SC.S#=t.S# AND

C#='006')

AS

英语

099

,COUNT(*) AS

有效课程数, AVG() AS

平均成绩

100

FROM

SC AS

t

101

GROUP

BY

S#

102

ORDER

BY

avg()

103

104

--18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

105

SELECT

L.C# As

课程ID, AS

最高分, AS

最低分

106

FROM

SC L ,SC AS

R

107

WHERE

L.C# = R.C# and

108

= (SELECT

MAX()

109

110

FROM

SC AS

IL,Student

AS

IM

WHERE

L.C# = IL.C# and

IM.S#=IL.S#

111

GROUP

BY

IL.C#)

112

AND

113

= (SELECT

MIN()

114

FROM

SC AS

IR

115

WHERE

R.C# = IR.C#

116

GROUP

BY

IR.C#

117

);

118

119

--19、按各科平均成绩从低到高和及格率的百分数从高到低顺序

120

121

SELECT

t.C# AS

课程号,max()AS

课程名,isnull(AVG(score),0) AS

平均成绩

,100 * SUM(CASE

WHEN

isnull(score,0)>=60 THEN

1 ELSE

0 END)/COUNT(*) AS

及格百分数

122

FROM

SC T,Course

123

where

t.C#=course.C#

124

GROUP

BY

t.C#

125

ORDER

BY

100 * SUM(CASE

WHEN

isnull(score,0)>=60 THEN

1 ELSE

0

END)/COUNT(*) DESC

126

127

128

--20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(),马克思(),OO&UML (),数据库()

SELECT

SUM(CASE

WHEN

C# ='001'

THEN

score ELSE

0 END)/SUM(CASE

C# WHEN

'001'

THEN

1 ELSE

0 END) AS

企业管理平均分

,100 * SUM(CASE

WHEN

C# = '001'

AND

score >= 60 THEN

1

129

ELSE

0 END)/SUM(CASE

WHEN

C# = '001'

THEN

1 ELSE

0 END) AS

企业管理及格百分数

130

,SUM(CASE

WHEN

C# = '002'

THEN

score ELSE

0

END)/SUM(CASE

C# WHEN

'002'

THEN

1 ELSE

0 END) AS

马克思平均分

,100 * SUM(CASE

WHEN

C# = '002'

AND

score >= 60 THEN

1

131

ELSE

0 END)/SUM(CASE

WHEN

C# = '002'

THEN

1 ELSE

0 END) AS

马克思及格百分数

132

,SUM(CASE

WHEN

C# = '003'

THEN

score ELSE

0

END)/SUM(CASE

C# WHEN

'003'

THEN

1 ELSE

0 END) AS

UML平均分

,100 * SUM(CASE

WHEN

C# = '003'

AND

score >= 60 THEN

1

133

ELSE

0 END)/SUM(CASE

WHEN

C# = '003'

THEN

1 ELSE

0 END) AS

UML及格百分数

134

,SUM(CASE

WHEN

C# = '004'

THEN

score ELSE

0

END)/SUM(CASE

C# WHEN

'004'

THEN

1 ELSE

0 END) AS

数据库平均分

,100 * SUM(CASE

WHEN

C# = '004'

AND

score >= 60 THEN

1

135

ELSE

0 END)/SUM(CASE

WHEN

C# = '004'

THEN

1 ELSE

0 END) AS

数据库及格百分数

136

FROM

SC

137

138

--21、查询不同老师所教不同课程平均分从高到低显示

139

SELECT

max(Z.T#) AS

教师ID,MAX() AS

教师姓名,C.C# AS

课程ID,MAX() AS

课程名称,AVG(Score) AS

平均成绩

140

FROM

SC AS

T,Course AS

C ,Teacher AS

Z

141

where

T.C#=C.C# and

C.T#=Z.T#

142

GROUP

BY

C.C#

143

ORDER

BY

AVG(Score) DESC

144

145

--22、查询如下课程成绩第名到第名的学生成绩单:企业管理(),马克思(),UML (),数据库()

146

[学生ID],[学生姓名],企业管理,马克思,UML,数据库,平均成绩

147

SELECT

DISTINCT

top

3

148

SC.S# As

学生学号,

149

AS

学生姓名,

150

AS

企业管理,

151

AS

马克思,

152

AS

UML,

153

AS

数据库,

154

ISNULL(,0) + ISNULL(,0) +

ISNULL(,0) + ISNULL(,0) as

总分

ON

SC.S# = T1.S# AND

T1.C# = '001'

ON

SC.S# = T2.S# AND

T2.C# = '002'

ON

SC.S# = T3.S# AND

T3.C# = '003'

ON

SC.S# = T4.S# AND

T4.C# = '004'

155

FROM

Student,SC LEFT

JOIN

SC AS

T1

156

157

LEFT

JOIN

SC AS

T2

158

159

LEFT

JOIN

SC AS

T3

160

161

LEFT

JOIN

SC AS

T4

162

163

WHERE

student.S#=SC.S# and

164

ISNULL(,0) + ISNULL(,0) +

ISNULL(,0) + ISNULL(,0)

165

NOT

IN

166

(SELECT

167

DISTINCT

168

TOP

15 WITH

TIES

169

ISNULL(,0) + ISNULL(,0) +

ISNULL(,0) + ISNULL(,0)

170

FROM

sc

171

LEFT

JOIN

sc AS

T1

172

ON

sc.S# = T1.S# AND

T1.C# = 'k1'

ON

sc.S# = T2.S# AND

T2.C# = 'k2'

ON

sc.S# = T3.S# AND

T3.C# = 'k3'

ON

sc.S# = T4.S# AND

T4.C# = 'k4'

ORDER

BY

ISNULL(,0) + ISNULL(,0) +

ISNULL(,0) + ISNULL(,0) DESC);

173

LEFT

JOIN

sc AS

T2

174

175

LEFT

JOIN

sc AS

T3

176

177

LEFT

JOIN

sc AS

T4

178

179

180

181

--23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

,SUM(CASE

WHEN

score BETWEEN

85 AND

100 THEN

1 ELSE

0

END) AS

[100 - 85]

,SUM(CASE

WHEN

score BETWEEN

70 AND

85 THEN

1 ELSE

0

END) AS

[85 - 70]

,SUM(CASE

WHEN

score BETWEEN

60 AND

70 THEN

1 ELSE

0

END) AS

[70 - 60]

182

SELECT

SC.C# as

课程ID, Cname as

课程名称

183

184

185

186

,SUM(CASE

WHEN

score < 60 THEN

1 ELSE

0 END) AS

[60 -]

187

FROM

SC,Course

188

where

SC.C#=Course.C#

189

GROUP

BY

SC.C#,Cname;

190

191

--24、查询学生平均成绩及其名次

192

SELECT

1+(SELECT

COUNT( distinct

平均成绩)

193

FROM

(SELECT

S#,AVG(score) AS

平均成绩

194

FROM

SC

195

GROUP

BY

S#

196

) AS

T1

197

WHERE

平均成绩> T2.平均成绩) as

名次,

198

S# as

学生学号,平均成绩

199

FROM

(SELECT

S#,AVG(score) 平均成绩

200

FROM

SC

201

GROUP

BY

S#

202

) AS

T2

203

ORDER

BY

平均成绩desc;

204

205

--25、查询各科成绩前三名的记录:(不考虑成绩并列情况)

206

SELECT

t1.S# as

学生ID,t1.C# as

课程ID,Score as

分数

207

FROM

SC t1

208

WHERE

score IN

(SELECT

TOP

3 score

209

FROM

SC

210

WHERE

t1.C#= C#

211

ORDER

BY

score DESC

212

)

213

ORDER

BY

t1.C#;

214

215

--26、查询每门课程被选修的学生数

216

select

c#,count(S#) from

sc group

by

C#;

217

218

--27、查询出只选修了一门课程的全部学生的学号和姓名

219

select

SC.S#,,count(C#) AS

选课数

220

from

SC ,Student

221

where

SC.S#=Student.S# group

by

SC.S# , having

count(C#)=1;

222

223

--28、查询男生、女生人数

224

225

Select

count(Ssex) as

男生人数from

Student group

by

Ssex

having

Ssex='男';

Select

count(Ssex) as

女生人数from

Student group

by

Ssex

having

Ssex='女';

226

227

--29、查询姓“张”的学生名单

228

SELECT

Sname FROM

Student WHERE

Sname like

'张%';

229

230

--30、查询同名同性学生名单,并统计同名人数

231

select

Sname,count(*) from

Student group

by

Sname

having

count(*)>1;;

232

233

--31、年出生的学生名单(注:Student表中Sage列的类型是datetime)

234

select

Sname, CONVERT(char

(11),DATEPART(year,Sage)) as

age

235

from

student

236

where

CONVERT(char(11),DATEPART(year,Sage))='1981';

237

238

239

--32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列

Select

C#,Avg(score) from

SC group

by

C# order

by

Avg(score),C#

DESC

;

240

241

--33、查询平均成绩大于的所有学生的学号、姓名和平均成绩

242

select

Sname,SC.S# ,avg(score)

243

from

Student,SC

244

where

Student.S#=SC.S# group

by

SC.S#,Sname

having

avg(score)>85;

245

246

--34、查询课程名称为“数据库”,且分数低于的学生姓名和分数

247

Select

Sname,isnull(score,0)

248

from

Student,SC,Course

249

where

SC.S#=Student.S# and

SC.C#=Course.C#

and

='数据库'and

score <60;

250

251

--35、查询所有学生的选课情况;

252

SELECT

SC.S#,SC.C#,Sname,Cname

253

FROM

SC,Student,Course

254

where

SC.S#=Student.S# and

SC.C#=Course.C# ;

255

256

--36、查询任何一门课程成绩在分以上的姓名、课程名称和分数;

257

SELECT

distinct

student.S#,,SC.C#,

258

FROM

student,Sc

259

WHERE

>=70 AND

SC.S#=student.S#;

260

261

--37、查询不及格的课程,并按课程号从大到小排列

262

select

c# from

sc where

scor e <60 order

by

C# ;

263

264

--38、查询课程编号为且课程成绩在分以上的学生的学号和姓名;

265

select

SC.S#, from

SC,Student where

SC.S#=Student.S# and

Score>80 and

C#='003';

266

267

--39、求选了课程的学生人数

268

select

count(*) from

sc;

269

270

--40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩

271

select

,score

272

from

Student,SC,Course C,Teacher

where

Student.S#=SC.S# and

SC.C#=C.C# and

C.T#=Teacher.T# and

273

='叶平'

and

=(select

max(score)from

SC where

C#=C.C# );

274

275

--41、查询各个课程及相应的选修人数

276

select

count(*) from

sc group

by

C#;

277

278

--42、查询不同课程成绩相同的学生的学号、课程号、学生成绩

279

select

distinct

A.S#, from

SC A ,SC B where

= and

A.C# <>B.C# ;

280

281

--43、查询每门功成绩最好的前两名

282

SELECT

t1.S# as

学生ID,t1.C# as

课程ID,Score as

分数

283

FROM

SC t1

284

WHERE

score IN

(SELECT

TOP

2 score

285

FROM

SC

286

WHERE

t1.C#= C#

287

ORDER

BY

score DESC

288

)

289

ORDER

BY

t1.C#;

290

--44、统计每门课程的学生选修人数(超过人的课程才统计)。要求输出课291

程号和选修人数,查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列

292

select

C# as

课程号,count(*) as

人数

293

from

sc

294

group

by

C#

295

order

by

count(*) desc,c#

296

297

--45、检索至少选修两门课程的学生学号

298

select

S#

299

from

sc

300

group

by

s#

301

having

count(*) > = 2

302

303

--46、查询全部学生都选修的课程的课程号和课程名

304

select

C#,Cname

305

from

Course

306

where

C# in

(select

c# from

sc group

by

c#)

307

308

--47、查询没学过“叶平”老师讲授的任一门课程的学生姓名

select

Sname from

Student where

S# not

in

(select

S# from

309

Course,Teacher,SC where

Course.T#=Teacher.T# and

SC.C#=course.C# and

Tname='叶平');

310

311

--48、查询两门以上不及格课程的同学的学号及其平均成绩

312

select

S#,avg(isnull(score,0)) from

SC where

S# in

(select

S#

from

SC where

score <60 group

by

S# having

count(*)>2)group

by

S#;

313

314

--49、检索“”课程分数小于,按分数降序排列的同学学号

315

select

S# from

SC where

C#='004'and

score <60 order

by

score

desc;

316

317

--50、删除“”同学的“”课程的成绩

318

delete

from

Sc where

S#='001'and

C#='001';

319

320

--学生表(学号、姓名、性别、年龄、所在系)

321

--课程表(课程号、课程名、先修课号、学分)

322

--学生选课表(学号、课程号、成绩)

323

325

327

329

331

332

324

--1:查询全体学生的学号和姓名

326

--2:查询全体学生的姓名、学号和所在系

328

--3: 查询全体学生的详细记录

330

--4: 查询全体学生的姓名及其出生年份

--5:查询全体学生姓名、出生年份和所在系,要求用小写字母表示所有系名

333

335

337

339

341

343

344

334

--6:查询选修了课程的学生学号

336

--7:查询计算机系(IS)所有学生的名单

338

--8:查询所有年龄在20以下学生的姓名和年龄

340

--9: 查询考试成绩有不及格的学生的学号

342

--10: 查询年龄在20-23 (包括20和23)之间的学生的姓名、系别和年龄

--11: 查询信息系(IS)、数学系(MA)和计算机科学系(CS)学生的姓名和性别

345

347

346

--12: 查询学号为95001的学生的详细情况

348

--13: 查询所有姓林的学生的姓名、学号和性别

349

351

353

355

357

359

361

363

364

350

--14: 查询姓“欧阳”且全名为三个汉字的学生的姓名

352

--15:查询名字中第二个字为“燕”字的学生姓名和学号

354

--16:查询所有不姓“刘”的学生的姓名

356

--17:查询课程名为“DB_DESIGN”的课程号的学分

358

--18:查询缺少成绩的学生的学号和相应的课程号(成绩字段值为Null)

360

--19: 查询所有有成绩的学生的学号和课程号

362

--20: 查询所有计算机系年龄在20以下的学生姓名

--21: 查询选修了3号课程的学生的学号和成绩,查询结果按分数降序排列

365

366

--22: 查询全体学生情况,查询结果按所在系的系号升序排列,同一系中的学生按年龄降序排列

367

369

371

373

375

377

379

381

368

--23: 查询学生总人数

370

--24: 查询选修了课程的学生人数

372

--25: 计算1号课程的学生的平均成绩

374

--26: 计算1号课程的学生的最高成绩分数

376

--27:求各个课程号及相应的选课人数

378

--28: 查询选修了三门以上课程的学生学号

380

--29:查询每个学生及其选修课情况

382

--30:查询每一门课的间接先行课

383

385

387

389

391

393

395

397

399

401

403

404

384

--31:选修2号课程且成绩在90以上的学生的学号和姓名

386

--32:查询每个学生的学号、姓名、选修的课程名及成绩

388

--33:查询与’林燕芳’在同一个系学习的学生姓名

390

--34: 查询其他系中比信息系某一学生小的学生姓名和年龄

392

--35:查询所有选修了1号课程的学生的学生姓名

394

--36:查询选修了全部课程的学生姓名

396

--37:至少选修了学生95002选修全部课程的学生号码

398

select

Sno,Sname from

Student

400

select

Sno,Sname,Sdept from

Student

402

select

* from

Student /*也可以逐一列出列名并用逗号分隔*/

--其中getdate是获取当前系统时间。这是一个获取到的结果 :2008-12-11

16:02:17.967

405

406

--datepart从获取到的系统时间中分离出需要的部分,这里是分离出年份,更多信息请查看SQL Server联机帮助

407

409

410

408

--下面的 出生年 指定了别名来替代原来结果页出现的文字

select

Sname , (datepart( year,getdate())- Sage) '出生年'

from

Student

411

412

--该实例利用了小写转换函数lower() 提示:通过查询分析器的 公用对象

的 字符串函数中你可以找到这个函数

413

414

select

Sname '姓名'

, (datepart( year,getdate())- Sage) '出生年',lower(Sdept) '所在系'

from

Student

415

417

418

416

select

Sno from

sc --这里将返回全部结果,有重复的值

select

distinct

Sno from

sc--加入关键字distinct就可以去除重复结果,只留1个

419

420

--sql 中默认对字符串大小写不敏感的,所以下面的sdept=’IS’你也可以写成sdept=’is’。如果你要启用大小写敏感,可以用下面的方法

421

42if 敏感

2

42 select

* from

table

where

field1="AAA"

COLLATE3

Chinese_PRC_CS_AS

42 else

4

42 select

* from

table

where

field1="AAA"

COLLA5

TE

Chinese_PRC_CI_AS

426

COLLATE

的中文排序规则参数可用下列方式查到

42 SELECT

* FROM

::fn_helpcollations() where

name

like

7

'Chinese%'

428

430

432

434

436

438

439

429

select

Sname from

student where

sdept='IS'

431

select

Sname,Sage from

student where

Sage<20

433

select

Sno from

sc where

grade<60

435

--如果要查询不在这个区间的记录,那只要改成 not between就可以了

437

select

Sname,Sdept,Sage from

student where

Sage between

20 and

23

--如果要查询不属于信息系、数学系和计算机科学系的,可以在in前面加上NOT

440

441

--也可以这样子写:select Sname,Ssex from student where Sdept='is' or

sdept='ma' or sdept='cs'

442

443

select

Sname,Ssex from

student where

Sdept in('IS','MA','CS')

444

446

448

450

452

454

456

458

460

462

464

466

468

470

472

474

476

478

445

--或者是select * from student where sno = '95001'

447

select

* from

student where

sno like

'95001':--like用于字符串匹配

449

--百分号匹配多个字符,包括0个

451

select

Sname,Sno,Sage from

student where

sname like

'林%'

453

-- 一个下划线匹配单个字符

455

select

sname from

student where

sname like

'欧阳_'

457

select

sname,sno from

student where

sname like

'_燕%'

459

select

sname from

student where

sname not

like

'刘%'

461

select

Ccredit from

course where

cname like

'DB_DESIGN'

escape''

463

--注意:这里不用使用 = null

465

select sno,cno from sc where grade is null

467

select sno,cno from sc where grade is not null

469

select sname from student where sdept='CS' and sage<20

471

select sno,grade from sc where cno=3 order by grade desc

473

select * from student order by sdept,sage desc

475

select count(*) from student

477

select count(distinct sno) from sc

479

select avg(grade) from sc where cno='1'

480

482

484

486

487

481

select max(grade) from sc where cno='1'

483

group by 按照它后面的列值进行分组,相同的值被分在一组

485

select cno,count(sno) from sc group by cno

--having后面的条件是根据group by 分组后的结果再进行筛选,最后只给出满足条件的分组

488

490

492

494

496

498

500

502

504

506

508

510

512

489

--where筛选的对象是整个表,而having则是分组

491

select sno from sc group by sno having count(sno)>=3

493

select , from student a ,sc b where =

495

或者

497

select , from student a left outer join sc b

499

on = where is not null

501

--自身连接

503

select , from course a,course b where =

505

--31:

507

select ,

509

from student,sc

511

where = and

513

='2' and

514

516

518

520

522

524

526

528

530

532

534

536

538

540

542

544

546

548

515

>=90

517

--32:

519

select ,,,

521

from (student left join sc on =)

523

left join course on =

525

--或者:

527

--忽略cname和grade都为null的行

529

Select ,sname,cname,grade

531

From student,sc,course

533

Where = and =

535

--33:

537

select sname from student

539

where sdept=(select sdept from student where sname='林燕芳')

541

--34:

543

select sname,sage

545

from student

547

where sage

549

select sage from student

550

552

554

556

558

560

562

564

566

568

570

572

574

576

578

580

582

584

551

where sdept='is'

553

) and sdept<>'IS'

555

--35:利用 exists的查询

557

--exists根据是否存在行返回true/false

559

--如果要查询没有选修1号课程的学生姓名,只要使用NOT Exists即可

561

select *

563

from student

565

where exists(

567

select 1 from sc

569

where = and cno='1'

571

)

573

--或者你可以使用连接查询

575

select * from student left join sc on =

577

where ='1'

579

--36:

581

declare @temp1 int

583

declare @temp2 int

585

select @temp1=count(*) from course

586

588

590

592

594

596

598

600

602

604

606

608

610

611

587

select @temp2=sno from sc group by sno

589

having count(sno)=@temp1

591

select sname from student where sno in (@temp2)

593

--或者

595

--就是转换成查询没有一门课程没有选修的学生姓名

597

--如果把两个not都去掉就是查询所有有选修课程的学生

599

Select sname from student where not exists(

601

Select 1 from course where not exists(

603

Select 1 from sc where = and =

605

)

607

)

609

--37:

--同样要进行转换:查询这样的学生,没有95002选修的课程而学生X没有选修的

612

614

616

618

620

613

Select distinct sno

615

From sc scx

617

Where not exists

619

(

621

Select 1 from sc scy

622

624

626

628

629

623

Where ='95002' and not exists

625

(

627

Select 1 from sc scz

Where = and

=

630

632

634

636

638

640

642

644

646

648

650

652

654

631

)

633

)

635

and sno!='95002'

637

--插入语句:

639

--对每一个系求平均年龄,并把结果存入数据库

641

--需要创建一个表用来存储结果

643

Create table Deptage

645

(

647

Sdept char(15),

649

Avgage smallint

651

);

653

--插入子查询结果

655

insert into

656

658

660

662

664

666

668

670

672

674

676

678

680

682

684

686

688

690

657

Deptage(Sdept,Avgage)

659

select sdept,avg(sage)

661

from student

663

group by sdept

665

--查看结果

667

select * from deptage

669

--修改语句;

671

--1:将学生95001的年龄改为22岁

673

Update student

675

Set sage=22

677

Where sno='95001'--注意如果不带where,则修改全部记录

679

--2:将所有的学生年龄加1岁(修改多个元组的值)

681

Update student

683

Set sage=sage+1;

685

--3:将计算机系全体同学的成绩置零(带子查询的修改语句)

687

Update sc

689

Set grade=0

691

Where 'cs'=(

692

694

696

698

700

702

704

706

708

710

712

714

716

718

720

722

723

693

Select sdept from student

695

Where =)

697

--删除语句:

699

--1:删除学号为95009的学生记录(删除后将无法回复)

701

Delete from student

703

Where sno='95009'--没有加where的话将删除该表全部记录

705

--2:删除计算机科学系所有学生的选课记录

707

Delete from sc

709

Where 'cs'=(

711

Select sdept

713

From student

715

Where =

717

)

719

--例1:查询至少选秀1号课程和3号课程号的学生号码。

721

--答案

select a.学号 from sc a,sc b where a.学号=b.学号 and a.课程号='1' and b.课程号='3'

724

726

725

--例2:查询至少选修了一门直接先行课为5号课程的学生姓名。

727

--答案:

728

select 姓名 from student where 学号 in( SELECT [学号] FROM

729

[test].[dbo].[SC] where 课程号 in(SELECT 课程号 from Course where

先行课='5'))

730

732

734

736

738

739

731

--例子3:查询选修了全部课程的学生号码和姓名。

733

declare @t1 int

735

declare @t2 int

737

select @t2=count(*) from Course

select @t1=学号 from SC group by 学号 having count(学号)=@t2

740

742

744

745

741

--print '@t1='+cast(@t1 as varchar)

743

Select 学号,姓名 from student where 学号=@t1

--例子4:查询信息系年龄最大的三个学生的学号及其年龄,结果按年龄降序排列。

746

747

select top 3 学号,年龄 from Student where 所在系='IS' order by

年龄 desc

748

750

751

749

--例子5:查询选修了2号课程的学生名字

select 姓名 from student where 学号 in( select 学号 from sc where 课程号=2)

752

754

755

753

--例子6:查询成绩为90分以上的学生名字和课程名字

select st.姓名,c.课程名 from student st left join sc s on st.学号=s.学号 left join Course c on s.课程号=c.课程号

756

757

where st.学号 in (select st.学号 from sc where s.成绩>=90)

758

760

762

764

766

768

770

772

774

776

778

780

759

sql 取中间几条记录(select top 表达式)

761

--查询从第M条至N条的记录,写到存储过程中就是输入参数

763

declare @m int--

765

declare @n int--

767

declare @x int

769

declare @y int

771

--设置测试值

773

set @m=3

775

set @n=10

777

set @x=(@n-@m+1)

779

set @y=(@m-1)

781

/*

782

语法

783

784

Select top (n-(m-1)) * from [表名] where [parimary key] not in(select

top (m-1)

[主键] from [表名] order by [排序字段及排序方法]) order by [排序字段及排序方法

785

];

786

*/

787

--测试用例,因为T-sql top 后不支持表达式,故采取下面的方法

788

789

exec('select

top

'+@x+'* from

kf.T_Community where

[C_ID] not

in

(select

top

'+@y+'

[C_ID] from

kf.T_Community order

by

[C_ID]) order

by

mysql 查询语句学生表、课程表、 成绩表、教师表

本文发布于:2024-01-26 02:22:47,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/1706206967600.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:学生   课程   查询   姓名   成绩   选修
留言与评论(共有 0 条评论)
   
验证码:
排行榜

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23