2021年SQL经典面试题及答案

2021年SQL经典面试题及答案
2021年SQL经典面试题及答案

1. 用一条SQL 语句查询出每门课都不不大于80 分学生姓名

name kecheng fenshu

张三语文81

张三数学75

李四语文76

李四数学90

王五语文81

王五数学100

王五英语90

A:select distinct name from table where name not in (select distinct name from table where fenshu<=80)

select name from table group by name having min(fenshu)>80

select name from table group by name having count(kecheng)>=3 and min(fenshu)>=80

2. 学生表如下:

自动编号学号姓名课程编号课程名称分数

1 001 张三0001 数学69

2 002 李四0001 数学89

3 001 张三0001 数学69

删除除了自动编号不同,其她都相似窗生冗余信息

A:delete tablename where 自动编号not in(select min( 自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数)

3. 面试题:怎么把这样一种表儿

year month amount

1991 1 1.1

1991 2 1.2

1991 3 1.3

1991 4 1.4

1992 1 2.1

1992 2 2.2

1992 3 2.3

1992 4 2.4

查成这样一种成果

year m1 m2 m3 m4

1991 1.1 1.2 1.3 1.4

1992 2.1 2.2 2.3 2.4

答案一、

select year,

(select amount from aaa m where month=1 and m.year=aaa.year) as m1, (select amount from aaa m where month=2 and m.year=aaa.year) as m2, (select amount from aaa m where month=3 and m.year=aaa.year) as m3, (select amount from aaa m where month=4 and m.year=aaa.year) as m4 from aaa group by year

4. 阐明:拷贝表( 拷贝数据,源表名:a 目的表名:b)

SQL:insert into b(a,b,c) select d,e,f from a;

5.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表达语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按如下条件显示出来(并写出您思路):

不不大于或等于80表达先进,不不大于或等于60表达及格,不大于60分表达不及格。

显示格式:

语文数学英语

及格先进不及格

------------------------------------------

select

(case when 语文>=80 then '先进'

when 语文>=60 then '及格'

else '不及格') as 语文,

(case when 数学>=80 then '先进'

when 数学>=60 then '及格'

else '不及格') as 数学,

(case when 英语>=80 then '先进'

when 英语>=60 then '及格'

else '不及格') as 英语,

from table

6、编写SQL语句

1) 创立一张学生表,包括如下信息,学号,姓名,年龄,性别,家庭住址,联系电话

Create table stu (学号int ,

姓名varchar(8),

年龄int,

性别varchar(4),

家庭地址varchar(50),

联系电话int

);

2) 修改学生表构造,添加一列信息,学历

Alter table stu add 学历varchar(6);

3) 修改学生表构造,删除一列信息,家庭住址

Alter table stu drop column 家庭地址

4) 向学生表添加如下信息:

学号姓名年龄性别联系电话学历

1A22男123456小学

2B21男119中学

3C23男110高中

4D18女114大学

Insert into stu values(1,’A’,22,’男’,123456,’小学’)

Insert into stu values(2,’B’,21,’男’,119,’中学’)

Insert into stu values(3,’C’,23,’男’,110,’高中’)

Insert into stu values(4,’D’,18,’女’,114,’大学’)

5) 修改学生表数据,将电话号码以11开头学员学历改为“大专”

Update stu set 学历=’大专’ where 联系电话like ‘11%’

6) 删除学生表数据,姓名以C开头,性别为‘男’记录删除

Delect from stu where 性别=’男’ an d 姓名like ‘c%’

7) 查询学生表数据,将所有年龄不大于22岁,学历为“大专”,学生姓名和学号示出来

Select 姓名,学号from stu where 年龄<22 and 学历=’大专’

8) 查询学生表数据,查询所有信息,列出前25%记录

Select top 25 percent * from stu

相关主题
相关文档
最新文档