`

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 @id=@id+1,pm=@id
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 @id=@id+1,pm=@idselect * 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 =@s+\’,\’+name+\'(\’+convert(varchar(10),score)+\’分)\’ from stuscore where subject=\’数学\’ set @s=stuff(@s,1,1,\’\’)print \’数学:\’+@s

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

分享到:
评论

相关推荐

    sql面试题目和答案sql面试题目和答案sql面试题目和答案

    根据提供的文件信息,我们可以整理出以下几个关键的 SQL 面试题目及解答: ### 1. 使用 `GROUP BY` 处理数据 **题目描述**: 假设有一个表 #tmp,包含两列:日期 rq 和胜负标记 shengfu(其中 shengfu 可能为空)...

    数据库 sql 面试题目及答案解析.docx

    根据给定文件的信息,我们可以总结出...以上几个SQL面试题目涵盖了常见的SQL操作,包括基本的查询、条件筛选、联表查询以及聚合函数等。对于准备参加数据库相关职位面试的求职者来说,熟练掌握这些知识点是非常必要的。

    数据库 sql 面试题目及答案解析.zip

    数据库SQL面试是求职者在申请数据相关职位时必须面对的重要环节。这涉及到对数据库理论、SQL语言、数据库设计以及性能优化等多方面的理解和应用能力。以下是一些可能出现在面试中的关键知识点,以及对它们的详细解释...

    SQL 面试题目汇总

    在SQL面试中,掌握核心概念和技术是至关重要的。以下是一些关键知识点的详细解析: 1. **SQL(Structured Query Language)**:SQL是用于管理关系数据库的标准编程语言,包括数据查询、更新、插入和删除等操作。...

    SQL_Server 面试笔试试题及答案

    "SQL Server 面试笔试试题及答案" 本资源摘要提供了 SQL Server 面试笔试试题及答案,涵盖了数据库管理、数据模型、数据结构、数据库优化、SQL 语言等多方面的知识点。通过本资源,读者可以快速掌握 SQL Server 的...

    SQL面试题(很全,带答案)

    根据给定的文件标题、描述、标签以及部分内容,我们可以从中提炼出以下详细的IT知识点,主要聚焦于SQL语言的基础操作及面试常考知识点: ### SQL语言基础分类与功能 #### 数据定义语言(DDL) - `CREATE DATABASE`:...

    SQL面试题目-sqlServer

    在这篇文章中,我们将总结一些常见的 SQL Server 面试题目,并提供相应的答案和解释。这些题目涵盖了基本的 SQL 语句、数据处理、数据分析等方面的知识。 题目 1: 用一条 SQL 语句查询出每门课都大于 80 分的学生...

    SQL面试(SQL面试的一些题目和答案)

    以下是一些常见的SQL面试题目及其解答,这些题目涵盖了数据建模、子查询和条件查询等核心概念。 首先,创建学生表S、课程表C和学生课程表SC的建表语句如下: ```sql CREATE TABLE S ( id INTEGER PRIMARY KEY, ...

    SQL数据库面试题目及其答案

    ### SQL数据库面试题目及其答案解析 #### 1. 触发器的作用? **知识点解析:** 触发器是一种特殊类型的存储过程,它会在特定的数据库事件(如数据的插入、更新或删除)发生时自动执行。触发器的主要作用在于增强...

    SQL面试题目,有例子。

    根据提供的信息,我们可以详细解析这些SQL面试题目以及给出的示例答案,并且深入理解每一道题目背后的原理与应用场景。 ### 風险1: 查询身份证号码为440401430103082的申请日期 #### 问题描述 此题目要求查询在`g_...

    Hive面试题SQL测试题目1数据

    文章Hive面试题SQL测试题目所需数据,包含建表语句 测试数据等等...................

    sql笔试面试题目(oracle,含答案)

    题目包含:1.基本SQL-SELECT语句 2.过滤和排序数据 3.单行函数 4.多表查询 5.分组函数 6.子查询 7.创建和管理表 8.数据处理 9.约束 10.视图 11.数据库对象 12.企业sql笔试题目 等

    SQL 经典面试题及答案

    根据给定文件的信息,我们可以提炼出以下几个重要的SQL知识...以上内容总结了SQL面试题中的一些经典知识点,包括数据汇总、条件判断、日期处理、事务处理以及数据导入导出等方面,这些技能都是在实际工作中非常有用的。

    经典sql面试题附答案

    根据给定的SQL题目及其答案,我们可以总结出一系列重要的SQL知识点和技巧,这些知识点对于学习SQL及准备面试都非常有帮助。 ### 1. 比较两个不同表中的记录 **知识点**: 子查询和连接操作是解决此类问题的关键技术...

    SQL经典面试题及答案

    根据给定文件的信息,我们可以提炼出以下几个重要的SQL面试题及其解答思路: ### 1. 使用 `GROUP BY` 进行分组统计 **题目描述**: 假设有一个表 #tmp,包含两列:日期 rq 和胜负标志 shengfu(其中 shengfu 可能...

    oracle面试题目及答案

    ### Oracle 面试题目详解 #### 1. 创建表空间 **题目描述:** 创建一个名为`neuspace`的表空间,其中包含一个数据文件`neudata.dbf`,该文件位于`D:\data`目录下,并且初始大小为200MB。配置该数据文件可以自动扩展...

    经典sql面试题及答案

    以下是对给定的SQL面试题目的详细解答: 1. **找出选修过李明老师讲授课程的所有学生姓名** - 步骤:首先,我们需要找到与李明老师关联的课程,然后找出选修这些课程的学生。这可以通过联接`学生`表和`课程`表,再...

    sql面试题(适合软件测试)

    根据提供的文件内容,我们可以整理出以下几个SQL面试题及其解答,这些问题主要聚焦于SQL查询技巧、数据处理以及数据结构的理解。 ### 1. 查询每门课都大于80分的学生姓名 **题目描述**:给定一张成绩表,其中包含...

    数据库SQL面试题及答案

    数据库SQL面试题及答案 本资源摘要信息涵盖了数据库SQL面试题及答案,包括Oracle和SQL Server数据库应用主要知识点。我们将详细解释每个问题的答案和相关知识点。 1. 磁盘柜上有 14 块 73G 的磁盘,数据库为 200G ...

Global site tag (gtag.js) - Google Analytics