sql笔试

学生成绩表(stuscore):
姓名:name     课程:subject     分数:score     学号:stuid
张三     数学     89     1
张三     语文     80     1
张三     英语     70     1
李四     数学     90     2
李四     语文     70     2
李四     英语     80     2

1.计算每个人的总成绩并排名(要求显示字段:姓名,总成绩)

答案:select name,sum(score) as allscore from stuscore group by name order by allscore

2.计算每个人的总成绩并排名(要求显示字段: 学号,姓名,总成绩)

答案:select distinct t1.name,t1.stuid,t2.allscore from  stuscore
t1,(    select stuid,sum(score) as allscore from stuscore group by
stuid)t2where t1.stuid=t2.stuidorder by t2.allscore desc

3.计算每个人单科的最高成绩(要求显示字段: 学号,姓名,课程,最高成绩)

答案:select t1.stuid,t1.name,t1.subject,t1.score from stuscore
t1,(select stuid,max(score) as maxscore from stuscore group by stuid)
t2where t1.stuid=t2.stuid and t1.score=t2.maxscore

4.计算每个人的平均成绩(要求显示字段: 学号,姓名,平均成绩)

答案:select distinct t1.stuid,t1.name,t2.avgscore from stuscore
t1,(select stuid,avg(score) as avgscore from stuscore group by stuid)
t2where t1.stuid=t2.stuid

5.列出各门课程成绩最好的学生(要求显示字段: 学号,姓名,科目,成绩)

答案:select  t1.stuid,t1.name,t1.subject,t2.maxscore from stuscore
t1,(select subject,max(score) as maxscore from stuscore group by
subject) t2where t1.subject=t2.subject and t1.score=t2.maxscore

6.列出各门课程成绩最好的两位学生(要求显示字段: 学号,姓名,科目,成绩)

答案:select distinct t1.* from stuscore t1 where t1.id in (select top 2
stuscore.id from stuscore where subject = t1.subject order by score
desc) order by t1.subject

7.统计如下:学号     姓名     语文     数学     英语     总分     平均分

答案:select stuid as 学号,name as 姓名,sum(case when subject=\’语文\’ then
score else 0 end) as 语文,sum(case when subject=\’数学\’ then score else 0
end) as 数学,sum(case when subject=\’英语\’ then score else 0 end) as
英语,sum(score) as 总分,(sum(score)/count(*)) as 平均分from stuscoregroup by
stuid,name order by 总分desc

8.列出各门课程的平均成绩(要求显示字段:课程,平均成绩)

答案:select subject,avg(score) as avgscore from stuscoregroup by subject

9.列出数学成绩的排名(要求显示字段:学号,姓名,成绩,排名)

答案:

declare @tmp table(pm int,name varchar(50),score int,stuid int)
insert into @tmp select null,name,score,stuid from stuscore where subject=\’数学\’ order by score desc
declare @id int
set @id=0;
update @tmp set @[email protected]+1,[email protected]
select * from @tmp

oracle:
select  DENSE_RANK () OVER(order by score desc) as
row,name,subject,score,stuid from stuscore where subject=\’数学\’order by
score desc
ms sql(最佳选择)
select (select count(*) from stuscore t1
where subject =\’数学\’ and t1.score>t2.score)+1 as row
,stuid,name,score from stuscore t2 where subject =\’数学\’ order by score
desc

10.列出数学成绩在2-3名的学生(要求显示字段:学号,姓名,科目,成绩)

答案:select t3.*  from(select top 2 t2.*  from (select top 3
name,subject,score,stuid from stuscore where subject=\’数学\’order by
score desc) t2 order by t2.score) t3 order by t3.score desc

11.求出李四的数学成绩的排名

答案:

declare @tmp table(pm int,name varchar(50),score int,stuid int)insert
into @tmp select null,name,score,stuid from stuscore where
subject=\’数学\’ order by score descdeclare @id intset @id=0;update @tmp
set @[email protected]+1,[email protected] * from @tmp where name=\’李四\’

12.统计如下:课程     不及格(0-59)个     良(60-80)个     优(81-100)个

答案:select subject, (select count(*) from stuscore where score<60
and subject=t1.subject) as 不及格,(select count(*) from stuscore where
score between 60 and 80 and subject=t1.subject) as 良,(select count(*)
from stuscore where score >80 and subject=t1.subject) as 优from
stuscore t1 group by subject

13.统计如下:数学:张三(50分),李四(90分),王五(90分),赵六(76分)

答案:

declare @s varchar(1000)set @s=\’\’select @s
[email protected]+\’,\’+name+\‘(\’+convert(varchar(10),score)+\’分)\’ from stuscore
where subject=\’数学\’ set @s=stuff(@s,1,1,\’\’)print \’数学:\’[email protected]

14.计算科科及格的人的平均成绩

答案: select distinct t1.stuid,t2.avgscore  from stuscore t1,(select
stuid,avg(score) as avgscore from stuscore   group by stuid  )
t2,(select stuid from stuscore where score<60 group by stuid) t3
where t1.stuid=t2.stuid and t1.stuid!=t3.stuid;
select 
name,avg(score) as avgscore   from stuscore s  where (select sum(case
when i.score>=60 then 1 else 0 end) from stuscore i where  i.name=
s.name)=3   group by name

时间: 2024-07-29 20:08:39

sql笔试的相关文章

几道经典的SQL笔试题目

几道经典的SQL笔试题目(有答案) (1)表名:购物信息 购物人      商品名称     数量 A            甲          2 B            乙          4 C            丙          1 A            丁          2 B            丙          5 …… (其他用户实验的记录大家可自行插入) 给出所有购入商品为两种或两种以上的购物人记录 答:select * from 购物信息 wher

企业SQL笔试

Student(SID, Sname, Sage, Ssex) 学生表 Course(CID, Cname, TID) 课程表 SC(SID, CID, score) 成绩表 Teacher(TID, Tname) 教师表 <Teacher 教师表> TID Tname <Student 学生表> SID Sname Sage <SC  成绩表> SID CID score <Course  课程表> CID Cname TID 创建表注意:1.课程从001

java面试题笔试常见选择题大全含答案

java面试题笔试常见选择题大全含答案 1.9道常见的java笔试选择题 2.java多线程面试题选择题大全含答案 3.java异常处理面试题常见选择题含答案 4.java笔试常见的选择题(坑你没商量) 5.java笔试题大全之IO流常见选择题 6.java集合类笔试选择题整理含答案 7.常见的javaweb笔试选择题含答案 8.常见的SQL笔试选择题含答案 原文地址:https://www.cnblogs.com/javamianshiti/p/10922290.html

Oracle 经典SQL 专为笔试准备

相信把这99条sql搞定,Oracle基本笔试简直就像玩的一样(史上最全,最经典的入门级Oracle查询语句) 1. select * from emp; 2. select empno, ename, job from emp; 3. select empno 编号, ename 姓名, job 工作 from emp; 4. select job from emp; 5. select distinct job from emp; 6. select distinct empno, job

阿里巴巴2016笔试第一题 sql语句关键字匹配

阿里巴巴这道题和微软的笔试题比较类似,相对来讲更简单一些,可以将解决那道题的思路用在这道题上. /*题目简单描述: 根据给出的每个sql语句的执行时间,计算单个关键字的执行时间 *//* 样例输入:32.5 insert into tableA values(1,"columnA","columnB")12.5 insert into tableB values(2,"columnA","columnB","colu

SQL Server笔试准备 Day2

今天着重整理一下SQL Server的理论基础 一.各概念的自我理解(参考:http://www.cnblogs.com/wuzhiming/p/3629589.html) 定义都比较长,面试时也不可能记的全,要有理解,试着用一两句话概括一下各个概念 1.触发器:一种根据条件(事件)执行的存储过程,在项目中没用过,一般业务逻辑用程序来控制,数据库尽量只使用数据存储,不进行业务逻辑. 2.存储过程:一段预编译的SQL语句,因为编译过,所以执行起来比普通SQL快.在项目中主要用于一连串事务性的操作

java笔试要点(java.sql包)

提供JAVA存取数据库能力的包是 ( ) A: java.sql B: java.awt C: java.lang D: java.swing 解析: A,java.sql包提供Java存取数据库能力 B,java.awt包是java图形界面的包 C,java.lang是java语言的核心好,lang是language的简写 D,java.swing是经过java封装的与操作系统无关的图形界面包 注解: java.sql提供使用 JavaTM 编程语言访问并处理存储在数据源(通常是一个关系数据库

面试题: 数据库笔试 sql操作 未看5

2018/5/31 oracle数据库面试笔试试题总结http://www.yjbys.com/qiuzhizhinan/show-308759.html 1/4Oracle数据库1.基础测试选择在部门 30 中员工的所有信息Select * from emp where deptno=30;列出职位为(MANAGER)的员工的编号,姓名Select empno,ename from emp where job = ?Manager ?;找出奖金高于工资的员工Select * from emp

【笔试/面试】SQL 经典面试题

基本概念 (1)any/all,构成 where 子句的条件判断,any:表示或(or)的概念,all:则表示与(and)的概念,这两个关键字的出现是为了语句的简化: (2)先分组再做聚合,逻辑上也应当如此,聚合(取最值)之后便无分组的必要: select region, sum(population), sum(area) from bbc group by region; 1 (3)group by having,having 对分组后的数据进行筛选,这是 where 所做不到的: 1. 不