最近萌生换工作的念头,于是上网下载了一些公司的面试题,重新看了面试题中的Sql部分,这些查询题有时候只是兜一个弯角来考,对于给EF惯坏的孩子来说还是有点难度的(给面试官鄙视了几下的结果),所以列出最近感觉比较有意思的Sql查询题。

1.查询出子节点最多的NodeName,如下图的table,

NodeName 子节点
节点1 1
节点2 2
节点3 1
节点3 1
节点3 1
节点4 2
节点4 3
 declare @t table( id int ,NodeName varchar(50 ),parentId int)

 insert into @t
select 4, '节点1' ,1
union all
select 5, '节点2' ,2
union all
select 6, '节点3' ,1
union all
select 7, '节点3' ,1
union all
select 1, '节点3' ,1
union all
select 2, '节点4' ,2
union all
select 3, '节点4' ,3 select * from @t select top 1 nodename, COUNT(*) from @t group by NodeName order by COUNT(*) desc

2.有表A如下图,需要转换成表B格式

单号   金额                              

Rk1     10

Rk2     20

Rk3     -30

Rk4     -10

  表A

单号   收入   支出

Rk1     10       0

Rk2     20       0

Rk3      0        30

Rk4      0        10

  表B

 declare @t table(danhao nvarchar(20),amount int)
insert into @t
select 'PK1',10 UNION
select 'PK2',20 UNION
select 'PK3',-10 UNION
select 'PK4',-30
select * from @t
select danhao,
(case when amount>0 then amount else 0 end) as N'收入',
(case when amount>0 then 0 else amount end) as N'支出'
from @t

3.有一张表T_Scores,记录比赛成绩

Date                 Name   Score 
2008-8-8          拜仁       胜
2008-8-9          奇才       胜
2008-8-9          湖人       胜
2008-8-10        拜仁       负
2008-8-8          拜仁       负
2008-8-12        奇才       胜
 
要求输出下面的格式:
Name   胜     负
拜仁     1       2
湖人     1       0
奇才     2       0
 
 declare @t table(DateT datetime,name nvarchar(20),Score nvarchar(20))
insert into @t
select '2008-8-8',N'拜仁',N'胜' union all
select '2008-8-8',N'奇才',N'胜' union all
select '2008-8-8',N'湖人',N'胜' union all
select '2008-8-8',N'拜仁',N'负' union all
select '2008-8-8',N'拜仁',N'胜' union all
select '2008-8-8',N'拜仁',N'胜' union all
select '2008-8-8',N'奇才',N'胜' union all
select '2008-8-8',N'湖人',N'负'
select name,
SUM(case Score when N'胜' then 1 else 0 end)as N'胜',
SUM(case Score when N'负' then 1 else 0 end)as N'负'
from @t
group by name

4.根据下图列表求和

id value
1 1
2 2
5 2
6 2
8 3
9 4
 declare @t table( id int ,value int )
insert into @t
select 1, 1
union all
select 2, 2
union all
select 5, 2
union all
select 6, 2
union all
select 8, 3
union all
select 9, 4 select * from @t
--1.按id 排序,取得所有的奇数 (单数) 行value之和 select SUM (m. value) from(
select ROW_NUMBER () over (order by id )row, id,value from @t)m
WHERE m .row% 2=1 --2.取得所有id 为奇数的行 value之和
select SUM (value) from @t where id% 2=1

5.行转列5.1与列转行5.2

5.1如下图所示

name class score
张三 语文 74
张三 数学 83
张三 物理 93
李四 语文 74
李四 数学 84
李四 物理 94

转换成

name 语文 数学 物理
张三 74 83 93
李四 74 84 94
 declare @t table ( name varchar (10), 课程 varchar (10), score int )

 insert into @t
select ' 张三', '语文' ,74
union all
select ' 张三', '数学' ,83
union all
select ' 张三', '物理' ,93
union all
select ' 李四', '语文' ,74
union all
select ' 李四', '数学' ,84
union all
select ' 李四', '物理' ,94 select * from @t select name,
max(case 课程 when '语文' then score else 0 end) 语文 ,
max(case 课程 when '数学' then score else 0 end) 数学 ,
max(case 课程 when '物理' then score else 0 end) 物理
from @t
group by name

5.2列转行

 declare @t table ( 姓名 varchar (10), 语文 int ,数学 int,物理 int)

 insert into @t
select ' 张三', 74,83 ,93
union all
select ' 李四', 74,84 ,94 select * from
(
select 姓名,课程 ='语文 ',分数 =语文 from @t
union all
select 姓名,课程 ='数学 ',分数 =数学 from @t
union all
select 姓名,课程 ='物理 ',分数 =物理 from @t
)m

  后期等待多了之后再用心整理成一份Sql文档,现在题目还少,努力去涵盖面试中遇到的,谢谢观看。

Sql面试常考题(持续添加)的更多相关文章

  1. java面试常考题

    基础知识: 1.C++或Java中的异常处理机制的简单原理和应用. 当JAVA程序违反了JAVA的语义规则时,JAVA虚拟机就会将发生的错误表示为一个异常.违反语义规则包括2种情况.一种是JAVA类库 ...

  2. leetcode Two Sum II - Input array is sorted <面试常考题>

    题目描述 //二分查找的变形   用头尾两个指针进行  面试考察题 class Solution { public: vector<int> twoSum(vector<int> ...

  3. 前端一面/面试常考题1-页面布局:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应。

    题目:假设高度已知,请写出三栏布局,其中左栏.右栏宽度各为300px,中间自适应. [题外话:日常宣读我的目标===想要成为一名优雅的程序媛] 一.分析 1. 题目真的像我们想得这么简单吗? 其实不然 ...

  4. PHP面试常考之会话控制

    你好,是我琉忆,欢迎您来到PHP面试专栏.本周(2019.2-25至3-1)的一三五更新的文章如下: 周一:PHP面试常考之会话控制周三:PHP面试常考之网络协议周五:PHP面试常考题之会话控制和网络 ...

  5. 【C++面试】常考题复习:排序算法

    // Sort.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> /*********** ...

  6. python面试总结3(性能分析优化,GIl常考题)

    python性能分析和优化,GIL常考题 什么是Cpython GIL Cpython解释器的内存管理并不是线程安全的 保护多线程情况下对python对象访问 Cpython使用简单的锁机制避免多个线 ...

  7. python面试总结2(函数常考题和异常处理)

    python函数常考题 可变类型为参数 不能类型为参数 python如何传递参数 传递值还是引用呢?都不是.唯一支持的参数传递是共享穿参 Call by Object(Call by Object R ...

  8. PHP+mysql常考题

    PHP+mysql常考题 来自<PHP程序员面试笔试宝典>,涵盖了近三年了各大型企业常考的PHP面试题,针对面试题提取出来各种面试知识也涵盖在了本书. 常考的mysql基础题 问题:设教务 ...

  9. PHP面试常考内容之Memcache和Redis(2)

    你好,是我琉忆.继周一(2019.2-18)发布的"PHP面试常考内容之Memcache和Redis(1)"后,这是第二篇,感谢你的支持和阅读.本周(2019.2-18至2-22) ...

随机推荐

  1. 通过 Azure 媒体管理门户开始使用直播流媒体

    Jason Suess Azure媒体服务首席项目经理 几个月前,我们宣布发布 Azure媒体服务直播服务的公共预览版.其实这些直播服务早已被美国国家广播公司体育台用于多项重大体育赛事的多平台直播 ...

  2. HTTP分段下载

    现代WEB服务器都支持大文件分段下载,加快下载速度,判断WEB服务器是否支持分段下载通过返回头是否有 Accept-Ranges: bytes 字段.分段下载分为两种,一种就是一次请求一个分段,一种就 ...

  3. 执行sql update use c#

    今天犯了个大错 public static void ChangeGoodsCounts(int GoodsID, int changCounts) { int lastCount; using (S ...

  4. Appium 小白从零安装 ,Appium连接真机测试。

    以下是我个人在初次安装使用Appium时的过程,过程中遇到了一些问题,在这里也一一给出解决办法. Appium安装过程 先安装了 Node.js.在node的官网上下载的exe安装文件. 在node的 ...

  5. 在今天,我们为什么还要做一个CMS

    我们今天看到,在这个移动大潮席卷来的这几年,互联网以惊人的速度改变着这个世界.包括我们这个在中国互联网史上有重大影响力的“站长”,也几乎全军覆没.当然随着站长们兴起的开源CMS,到今天也都穷途末路了. ...

  6. unix时间戳和localtime

    今天看代码的时候看到这么一段 void user::setHelpday() { int time = ::getTickCount(); m_helpday = (time +( * ))/( * ...

  7. 如果iis的配置文件 applicationHost.config坏掉了, 会在 C:\inetpub\history\ 中存储历史备份。复制过去还原就可以了-摘自网络

    You will usually get the error ‘Configuration file is not well-formed XML’ ‘C:\Windows\system32\inet ...

  8. Android: Dragging Popup Window 可移动浮动View

    final View cv = new View(this); setContentView(cv); TextView tv = new TextView(this); tv.setBackgrou ...

  9. hdoj 1532 Drainage Ditches【最大流模板题】

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  10. zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】

    Burning Bridges Time Limit: 5 Seconds      Memory Limit: 32768 KB Ferry Kingdom is a nice little cou ...