最近萌生换工作的念头,于是上网下载了一些公司的面试题,重新看了面试题中的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. mysql备份脚本

    [root@AY130828161048465847Z ~]# vi mysqlbak.sh #!/bin/bash USERNAME=rootPASSWORD=mysqlDBNAME=test DA ...

  2. c# 提取word文件中的图片问题

    最近遇到一个项目就是要从一份word中提取出所有的图片信息,功能看起来不是很难,只要使用office自带的Microsoft.Office.Interop.Word就可以解决问题.网上也有不少的文章来 ...

  3. bugfree如何修改Bug7种解决方案的标注方法

    Bug有7种解决方案的标注方法 By Design- 就是这么设计的,无效的Bug Duplicate - 这个问题别人已经发现了,重复的Bug External - 是个外部因素(比如浏览器.操作系 ...

  4. 在Python中的格式化

    str= '@SES/%i/'%-1print strstr1='@SES/%i/'%1print str1str2='@SES/%i/'%2print str2 打印出的结果: @SES/-1/@S ...

  5. Windows 服务 创建 和 安装 -摘自网络

    What a Windows Service is Enables you to create long-running executable applications that run in the ...

  6. c++ de-mangle 反编译器命名工具:c++filt

    nm *.so | c++filt c++filt  symblo

  7. Linux概念架构的理解

    摘要 Linux kernel成功的两个原因:(1)架构设计支持大量的志愿开发者加入到开发过程中:(2)每个子系统,尤其是那些需要改进的,都支持很好的扩展性.正式这两个原因使得Linux kernel ...

  8. setTimeout中0毫秒延时

    先来看段代码,思考一下执行的结果. alert(1); setTimeout(function(){alert(2);}, 0); alert(3); 估计很多人认为执行结果为1,2,3,原因就是认为 ...

  9. VC++从入门到精通视频教程网址

    视频教程网址:http://www.pconline.com.cn/pcedu/empolder/gj/vc/0607/820674.html

  10. makeKeyAndVisible的功能

    makeKeyAndVisible的作用 [self.window makeKeyAndVisible] 由于iPhone是单窗口程序,所以也就只有这么一个Window对象,而且是UIWindow,不 ...