Sql面试常考题(持续添加)
最近萌生换工作的念头,于是上网下载了一些公司的面试题,重新看了面试题中的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,记录比赛成绩
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面试常考题(持续添加)的更多相关文章
- java面试常考题
基础知识: 1.C++或Java中的异常处理机制的简单原理和应用. 当JAVA程序违反了JAVA的语义规则时,JAVA虚拟机就会将发生的错误表示为一个异常.违反语义规则包括2种情况.一种是JAVA类库 ...
- leetcode Two Sum II - Input array is sorted <面试常考题>
题目描述 //二分查找的变形 用头尾两个指针进行 面试考察题 class Solution { public: vector<int> twoSum(vector<int> ...
- 前端一面/面试常考题1-页面布局:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应。
题目:假设高度已知,请写出三栏布局,其中左栏.右栏宽度各为300px,中间自适应. [题外话:日常宣读我的目标===想要成为一名优雅的程序媛] 一.分析 1. 题目真的像我们想得这么简单吗? 其实不然 ...
- PHP面试常考之会话控制
你好,是我琉忆,欢迎您来到PHP面试专栏.本周(2019.2-25至3-1)的一三五更新的文章如下: 周一:PHP面试常考之会话控制周三:PHP面试常考之网络协议周五:PHP面试常考题之会话控制和网络 ...
- 【C++面试】常考题复习:排序算法
// Sort.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdlib.h> /*********** ...
- python面试总结3(性能分析优化,GIl常考题)
python性能分析和优化,GIL常考题 什么是Cpython GIL Cpython解释器的内存管理并不是线程安全的 保护多线程情况下对python对象访问 Cpython使用简单的锁机制避免多个线 ...
- python面试总结2(函数常考题和异常处理)
python函数常考题 可变类型为参数 不能类型为参数 python如何传递参数 传递值还是引用呢?都不是.唯一支持的参数传递是共享穿参 Call by Object(Call by Object R ...
- PHP+mysql常考题
PHP+mysql常考题 来自<PHP程序员面试笔试宝典>,涵盖了近三年了各大型企业常考的PHP面试题,针对面试题提取出来各种面试知识也涵盖在了本书. 常考的mysql基础题 问题:设教务 ...
- PHP面试常考内容之Memcache和Redis(2)
你好,是我琉忆.继周一(2019.2-18)发布的"PHP面试常考内容之Memcache和Redis(1)"后,这是第二篇,感谢你的支持和阅读.本周(2019.2-18至2-22) ...
随机推荐
- cannot find the word template:WordToRqm.dot的解决方法
powerdesigner安装后: C:"Program Files"Sybase"PowerDesigner 12"Add-ins"Microsof ...
- VS2010如何调试IIS上的网站
通常,我们在Visual Studio里调试ASP.NET网站,都是加个断点,然后按F5,在VS自带的虚拟服务器下调试的.但有时候,VS自带的服务器弱爆了,无法满足一些特定情况的要求,我们必须把网站放 ...
- vs2010 和 svn的结合运用,svn的安装
转自:http://www.cnblogs.com/joeylee/archive/2012/10/08/2715142.html 1:svn的安装,如何在vs2010里面添加 svn的插件呢? 在这 ...
- ArrayList源码解析
ArrayList简介 ArrayList定义 1 public class ArrayList<E> extends AbstractList<E> implements L ...
- nginx根据域名做http,https分发
omcat端口:8080 做好虚拟主机 参照我的另一篇文章nginx端口:80 根据域名分派 在conf/nginx.conf中的http中增加 include www.huozhe.com.conf ...
- 正则表达式(来源http://deerchao.net/tutorials/regex/regex.htm)
目录 跳过目录 本文目标 如何使用本教程 正则表达式到底是什么东西? 入门 测试正则表达式 元字符 字符转义 重复 字符类 分枝条件 反义 分组 后向引用 零宽断言 负向零宽断言 注释 贪婪与懒惰 处 ...
- object c小代码——日期篇
1.判断两个日期是否是同一天,不要求小时,分钟要一样 用法 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier: ...
- sublimelinter-jshinter
--- ocalhost:~ nihao$ sudo npm install jshint -gPassword:/usr/local/bin/jshint -> /usr/local/lib/ ...
- 五指cms安装说明
1.五指cms目录结构说明 |-- coreframe #框架目录 | |-- app #模块(应用程序)目录 | ...
- Spark在集群中的安装
今天由于所以要安装spark做一些实验.我已有的环境是: 操作系统:CentOS6.5 hadoop:hadoop2.4.1 JDK:1.7 集群环境:四个节点 闲话不说,以下是我的安装步骤: 说 ...