最近萌生换工作的念头,于是上网下载了一些公司的面试题,重新看了面试题中的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. JavaScript下拉框去除重复内容

    下拉框去除重复内容 <script type="text/javascript" src="http://www.joleye.com/libraries/java ...

  2. Farewell, 2015, welcome 2016

    Farewell, 2015, welcome 2016.   ##事出必有因 从2014年10月份开始, 在投资的路上越走越远.  盲目的行为, 付出了惨重的代价. 投资无所谓对错, 但投资失败带来 ...

  3. 经典sql总结(1)

    1.表示info 信息,字段为Id和res,如何得到如下结果

  4. 2015美团网 哈工大 第k个排列

    leetcode 上的Permutation Sequence 下面是可执行代码 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1 以1 开头 123,132,共2!个数 2 开 ...

  5. 1到n数组,和为指定数所有序列问题

    (1)方法一,背包问题解法 #include <iostream> using namespace std; #include <vector> #include <li ...

  6. scp 详细总结

    转自:http://www.ha97.com/4169.html scp命令跟cp命令类似,只不过cp命令是在同一台机器上用的,scp是在两台机器上复制传输数据的命令.scp实质相当于利用SSH协议来 ...

  7. 洛谷P1120 小木棍

    洛谷1120 小木棍 题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50.     现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长 ...

  8. CF_216_Div_2

    比赛链接:http://codeforces.com/contest/369 369C - Valera and Elections: 这是一个树上问题,用深搜,最开始贪心想得是只加叶子节点,找到一个 ...

  9. 让SecureCRT vi中显示多色彩

    方法1: 1.打开SecureCRT___选项__会话选项___仿真 右边的中端选择linux,把ANSI 颜色和使用颜色方案打上钩. 2.选择外观,右边的使用颜色和闪烁都打上钩. 3.重新连接使所该 ...

  10. C#创建、安装一个Windows服务

    关于WIndows服务的介绍,之前写过一篇: http://blog.csdn.net/yysyangyangyangshan/article/details/7295739.可能这里对如何写一个服务 ...