在面试大公司的时候,很多情况下都有笔试题,笔试题经常包含软件测试的基础知识点、逻辑题、等等,有时候也会碰到关于sql语句的题目,今天来列举一些简单的sql语句,希望对大家有所帮助。
创建表语句
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[stuscore]( [name] varchar COLLATE Chinese_PRC_CI_AS NULL, [subject] varchar COLLATE Chinese_PRC_CI_AS NULL, [score] [int] NULL, [stuid] [int] NULL) ON [PRIMARY] GOSET ANSI_PADDING OFF
插入数据
insert into dbo.stuscore values (‘张三’,‘数学’,89,1);insert into dbo.stuscore values (‘张三’,‘语文’,80,1);insert into dbo.stuscore values (‘张三’,‘英语’,70,1);insert into dbo.stuscore values (‘李四’,‘数学’,90,2);insert into dbo.stuscore values (‘李四’,‘语文’,70,2);insert into dbo.stuscore values (‘李四’,‘英语’,80,2);
查询语句
select * from dbo.stuscore
问题:
1.计算每个人的总成绩并排名(要求显示字段:姓名,总成绩)
select name,SUM(score) as allscore from dbo.stuscore
group by name
order by allscore;
2.计算每个人的总成绩并排名(要求显示字段: 学号,姓名,总成绩)
select stuid,name,SUM(score) as allscore from dbo.stuscore
group by name,stuid
order by allscore;
3.计算每个人单科的最高成绩(要求显示字段: 学号,姓名,课程,最高成绩)
select t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,
(select stuid,max(score) as maxscore from stuscore group by stuid) t2
where t1.stuid=t2.stuid and t1.score=t2.maxscore;
4.计算每个人的平均成绩(要求显示字段: 学号,姓名,平均成绩)
select stuid,name,AVG(score) avgscore from dbo.stuscore
group by stuid,name;
5.列出各门课程成绩最好的学生(要求显示字段: 学号,姓名,科目,成绩)
select t1.stuid,t1.name,t1.subject,t1.score from stuscore t1,(
select subject,MAX(score) as maxscore from stuscore group by subject)t2
where t1.subject = t2.subject and t1.score = t2.maxscore;
6.列出各门课程成绩最好的两位学生(要求显示字段: 学号,姓名,科目,成绩)
select t1.* from stuscore t1 where t1.stuid in (
select top 2 stuid from stuscore where subject = t1.subject order by score desc)
order by t1.subject;
7.列出数学成绩的排名(要求显示字段:学号,姓名,成绩,排名)
select stuid,name,score,
(select count(*) from stuscore t1 where subject =‘数学’ and t1.score > t2.score)+1 as 名次 from stuscore t2
where subject=‘数学’ order by score desc;
最后: 欢迎大家关注公众号:【 伤心的辣条 】,领取一份300页pdf文档的Python自动化测试工程师核心知识点总结!
公众号里大部分资料都是面试时面试官必问的知识点,也包括了很多测试行业常见知识,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。
如果你测试中有许多的困惑,那么我整理的视频资源和文档会是你的良师益友,或许可以给你带来一些实际性的帮助与突破。同行交流学习Q群社区:902061117 你也想知道同行都在怎样致富吧!
如果对你有一点点帮助,各位的「点赞」就是小编创作的最大动力,我们下篇文章见!