首页 > 我的文章 > PHP开发 > 思考 > sql经典面试试题,让你更好的了解和运用sql语言

sql经典面试试题,让你更好的了解和运用sql语言

浏览:838 编辑:绿萝 来源:绿萝
1. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名 name kecheng fenshu 张三 语文 81

sql经典面试试题,让你更好的了解和运用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

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 2005001 张三 0001 数学 69

2 2005002 李四 0001 数学 89

3 2005001 张三 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 性别=’男’ and 姓名 like ‘c%’

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

Select 姓名,学号 from stu where 年龄

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

Select top 25 percent * from stu

9) 查询出所有学生的姓名,性别,年龄降序排列

Select 姓名,性别 from stu order by 年龄 desc

10) 按照性别分组查询所有的平均年龄

Select avg(年龄) from stu group by 性别

7、查询A(ID,Name)表中第31至40条记录,ID作为主键可能是不是连续增长的列,完整的查询语句如下:

select top 10 * from A where ID >(select max(ID) from (select top 30 ID from A order by A ) T) order by A

8、查询表A中存在ID重复三次以上的记录,完整的查询语句如下:

select * from(select count(ID) as count from table group by ID)T where T.count>3

9、说出以下聚合数的含义:avg ,sum ,max ,min , count ,count(*)

AVG:求平均值

SUM:求和

MAX:求最大值

MIN:求最小值

COUNT(*):返回所有行数

*****************************************************************

事务 Transaction 触发器 TRIGGER 继续 continue 唯一 unqiue

主键 primary key 标识列 identity 外键 foreign key 检查 check

约束 constraint

*****************************************************************

10、说明:随机取出10条数据

select top 10 * from tablename order by newid()

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

select stuId,avg(score)

from Scores

group by stuId having avg(score) >60;