SQL语句面试问答(一)

一、单表查询面试题:

CREATE TABLE students (
	studentNo int(10) primary key auto_increment, 
	name varchar(10),  
	sex varchar(1), 
	hometown varchar(20), 
	age int(4), 
	class varchar(10), 
	card varchar(20)
);

INSERT INTO students (name, sex, hometown, age, class, card)
VALUES('王一','男','北京','20','1班','123456'), 
	('诸二','男','上海','18','2班','123789'), 
	('张三','男','南京','124','3班','123147'), 
	('白四','男','安徽','22','4班','123258'), 
	('杨五','女','天津','19','3班','123369'), 
	('孙六','女','河北','18','1班','145236'), 
	('赵七','男','山西','20','2班','125698'), 
	('小八','女','河南','15','3班',NULL), 
	('久久','男','湖南','21','1班',''), 
	('史十','女','广东','26','2班','159875'),  
	('李十一','男','北京','30','4班','147896'),  
	('孙十二','男','新疆','26','3班','125632');
 
CREATE TABLE courses (
courseNo int(10) PRIMARY KEY AUTO_INCREMENT, 
name varchar(10)
);
 
INSERT INTO courses
VALUES ('1','数据库'), 
	('2','qtp'), 
	('3','Linux'), 
	('4','系统测试'), 
	('5','单元测试'), 
	('6','测试过程');

CREATE TABLE scores (
	id int(10) PRIMARY KEY AUTO_INCREMENT, 
	courseNo int(10), 
	studentNo int(10),  
	score int(4)
);

INSERT INTO scores
VALUES ('1','1',1,'90'), 
	('2','2',2,'98'), 
	('3','1',2,'75'), 
	('4','3',1,'86'), 
	('5','3',3,'80'), 
	('6','4',4,'79'), 
	('7','5',5,'96'), 
	('8','6',6,'80');

1、查询学生"张三”的基本信息

select * from students where name='张三'

2、查询学生“李十一“或“孙十二”的基本信息

select * from students where name='李十一' or name='孙十二'

3、查询姓“张”学生的姓名,年龄,班级

select name,age,class from students where name='张%'

4、查询姓名中含有“一”字的学生的基本信息

select * from student where name='%一%'

5、查询姓名长度为三个字,姓“孙”的学生的学号,姓名,年龄,班级,身份证号

select studentNo,name, age, class, card from students where name like '孙__'

select studentNo,name, age, class, card from students 
where char_length(name) = 3  -- 确保姓名长度为3个字符
    and 姓名 like '孙%';   -- 确保姓"孙"

6、查询姓“白”或者姓“孙”的学生的基本信息

select * from students where name like '白%' or name like '孙%'

7、查询姓"白"并且家乡是"山西”的学生信息

select * from students where name like '白%' and hometown='山西'

8、查询家乡是“北京”、“新疆”、“山东"或者"上海"的学生的信息

select * from students where hometown='北京' or hometown='新疆' or hometown='山东' or hometown='上海'

select * from students where hometown in ('北京','新疆','山东','上海')

9、查询姓“孙”,但是家乡不是“河北”的学生信息

select * from students where name like '孙%' and honmetown!='河北'

10、查询家乡不是“北京”、“新疆”、"山东”、”上海”的学生的信息

select * from students 
where hometown!='北京' or hometown!='新疆' or hometown!='山东' or hometown!='上海'

select * from students where hometown not in ('北京','新疆','山东','上海')

11、查询全部学生信息,并按照“性别”排序

select * from students order by sex

12、查询现有学生都来自于哪些不同的省份

select honetown from students group by hometown

13、查询所有男生,并按年龄升序排序

select sex from students where sex='男' order by age asc

14、统计共有多少个学生

select count(*) from students 

15、统计年龄大于20岁的学生有多少个

select count(*) from students where age>'20'

16、统计男生的平均年龄

select avg(age) from students where sex='男'

17、查询1班学生中的最大年龄是多少

select max(age) from students where class='1班'

18、统计2班男女生各有多少人

select sum(sex='男') as '男生总数',sum(sex='女') as '女生总数' from students where class='2班'

select sex,count(*) from students where class='2班' group by sex

19、统计每个班级中每种性别的学生人数,并按照班级升序排序

select class,sex,count(*) from students group by class,sex order by class

问题来源:米兔软件测试

#mysql面试题##测试#
测试岗面经 文章被收录于专栏

整理面试过程中的测试问答,常看常新,多多学习!有些问题是从其他人那里转载而来,会在文章下面注明出处,希望大家多多支持~~

全部评论

相关推荐

一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
1. ThreadLocal在什么情况下会导致out of memory?2. 阿里规范中为什么要求用static来修饰ThreadLocal变量?3. 存储100万数据时,选HashMap还是ConcurrentHashMap?4. 多线程情况下使用HashMap会导致什么问题?5. HashMap扩容时需要注意什么?6. 仿大众点评项目中缓存空值具体是怎么实现的?7. 布隆过滤器的误判率大概是多少?8. 仿大众点评项目中,类似微博大V发博客的场景,使用推模式还是拉模式?大粉丝量时推模式会有什么问题?9. Redis分布式锁为什么要使用Lua脚本?纯Java代码为什么不可以?10. 为什么选用Websocket?除了Websocket,是否考虑过其他协议(如谷歌的相关协议)?11. 分布式环境下用schedule的关单会有什么问题?12. 若系统扩大为分布式且单量增大,解决定时任务问题的思路有哪些?13. 日志框架用的是log4j还是log4j2?日志应在哪些地方重点打印?14. 碰到老应用、老系统日志不全时,如何最快地加上基础日志打点?15. 为什么选用Mybatis plus而不选用JPA?16. 线程池在系统里用在哪些位置?17. 线程池的核心参数有哪些?从核心线程数达到最大线程数需要什么条件?18. 在优惠券秒杀场景下,阻塞队列应设置多大?19. RabbitMQ与Redis stream的推流最大区别是什么?20. 仿大众点评项目中,最重要的几张表是哪几张?订单表应加什么索引?21. MySQL的日志指的是哪些?集群模式最主要依赖的日志是什么?22. 生产环境中如何避免主从同步延迟带来的故障?23. JDK 11、17有哪些新特性?手撕:策略模式
陀思妥耶夫斯基基:三个月,接近二十场,终于面进去了
查看24道真题和解析
点赞 评论 收藏
分享
评论
4
3
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务