楼主: jinaqu

[SQL] 【讨论】大公司面试题,测测你的SQL水平

[复制链接]
论坛徽章:
407
紫蛋头
日期:2012-05-21 10:19:41迷宫蛋
日期:2012-06-06 16:02:49奥运会纪念徽章:足球
日期:2012-06-29 15:30:06奥运会纪念徽章:排球
日期:2012-07-10 21:24:24鲜花蛋
日期:2012-07-16 15:24:59奥运会纪念徽章:拳击
日期:2012-08-07 10:54:50奥运会纪念徽章:羽毛球
日期:2012-08-21 15:55:33奥运会纪念徽章:蹦床
日期:2012-08-21 21:09:51奥运会纪念徽章:篮球
日期:2012-08-24 10:29:11奥运会纪念徽章:体操
日期:2012-09-07 16:40:00
11#
发表于 2014-12-18 21:06 | 只看该作者
哪家公司

使用道具 举报

回复
论坛徽章:
169
SQL数据库编程大师
日期:2016-01-13 10:30:43SQL极客
日期:2013-12-09 14:13:35SQL大赛参与纪念
日期:2013-12-06 14:03:45最佳人气徽章
日期:2015-03-19 09:44:03现任管理团队成员
日期:2015-08-26 02:10:00秀才
日期:2015-07-28 09:12:12举人
日期:2015-07-13 15:30:15进士
日期:2015-07-28 09:12:58探花
日期:2015-07-28 09:12:58榜眼
日期:2015-08-18 09:48:03
12#
发表于 2014-12-18 21:09 | 只看该作者
dingjun123 发表于 2014-12-18 18:23
select a.stu_name,a.class_id,b.lession_name
from t_stu_profile a,t_lession b
where not exists

我也是这么想的。

使用道具 举报

回复
论坛徽章:
169
SQL数据库编程大师
日期:2016-01-13 10:30:43SQL极客
日期:2013-12-09 14:13:35SQL大赛参与纪念
日期:2013-12-06 14:03:45最佳人气徽章
日期:2015-03-19 09:44:03现任管理团队成员
日期:2015-08-26 02:10:00秀才
日期:2015-07-28 09:12:12举人
日期:2015-07-13 15:30:15进士
日期:2015-07-28 09:12:58探花
日期:2015-07-28 09:12:58榜眼
日期:2015-08-18 09:48:03
13#
发表于 2014-12-18 21:32 | 只看该作者
jinaqu 发表于 2014-12-18 18:26
B.2.        找出课程的前三名,以下列形式显示
课程        第一名(姓名+分数)        第二名(姓名+分数)        第三名(姓名+分数)
...

你这结果基本上已经出来了啊。。。。你把你的结果再group by一下,第一名-----第三名的列取个max就好了。

然后如果你不能用分析函数的话,硬上sql的话,就
select t1.*,(select count(*)+1 from t_score t2 where T2.LESSION_ID=t1.lession_id and t2.score>t1.score)rank from t_score t1 )
select lession_id from tmp01

使用道具 举报

回复
论坛徽章:
3
优秀写手
日期:2014-11-11 06:00:15沸羊羊
日期:2015-03-04 14:55:412015年新春福章
日期:2015-03-06 11:59:47
14#
 楼主| 发表于 2014-12-18 21:41 | 只看该作者
我目前还在琢磨第二个,在网上有看到大神贴的代码,甚是佩服,我这里分享给大家,多提意见哈!!

  1. SELECT b.lession_name "课程",
  2.        (SELECT c.stu_name
  3.          FROM t_stu_profile c
  4.          WHERE a.highest_stu_id = c.stu_id) || ' ' || a.highest_score "第一名(姓名+分数)",
  5.        (SELECT c.stu_name
  6.          FROM t_stu_profile c
  7.          WHERE a.second_stu_id = c.stu_id) || ' ' || a.second_score "第二名(姓名+分数)",
  8.        (SELECT c.stu_name
  9.          FROM t_stu_profile c
  10.          WHERE a.third_stu_id = c.stu_id) || ' ' || a.third_score "第三名(姓名+分数)"
  11. FROM t_lession b,
  12.      (SELECT lession_id, MAX(decode(num, 1, stu_id, NULL)) highest_stu_id,
  13.               MAX(decode(num, 1, score, NULL)) highest_score,
  14.               MAX(decode(num, 2, stu_id, NULL)) second_stu_id,
  15.               MAX(decode(num, 2, score, NULL)) second_score,
  16.               MAX(decode(num, 3, stu_id, NULL)) third_stu_id,
  17.               MAX(decode(num, 3, score, NULL)) third_score
  18.        FROM (SELECT stu_id, lession_id, score,
  19.                      row_number() over(PARTITION BY lession_id ORDER BY score DESC) AS num
  20.               FROM t_score)
  21.        WHERE num <= 3
  22.        GROUP BY lession_id) a
  23. WHERE b.lession_id = a.lession_id;
复制代码

使用道具 举报

回复
论坛徽章:
3
优秀写手
日期:2014-11-11 06:00:15沸羊羊
日期:2015-03-04 14:55:412015年新春福章
日期:2015-03-06 11:59:47
15#
 楼主| 发表于 2014-12-18 21:42 | 只看该作者
Naldonado 发表于 2014-12-18 21:32
你这结果基本上已经出来了啊。。。。你把你的结果再group by一下,第一名-----第三名的列取个max就好了。 ...

厉害太感谢了

使用道具 举报

回复
论坛徽章:
737
季节之章:春
日期:2015-07-31 17:16:29ITPUB季度 技术新星
日期:2014-07-17 14:37:00季节之章:秋
日期:2015-07-31 17:16:14季节之章:夏
日期:2015-07-31 17:16:29股神
日期:2014-10-15 09:23:31衰神
日期:2014-10-20 22:47:12季节之章:冬
日期:2015-07-31 17:16:14红钻
日期:2014-12-16 17:51:41洛杉矶湖人
日期:2016-09-23 08:18:15布鲁克林篮网
日期:2016-09-23 08:17:18
16#
发表于 2014-12-18 21:44 | 只看该作者
不会。。。。

使用道具 举报

回复
论坛徽章:
3
优秀写手
日期:2014-11-11 06:00:15沸羊羊
日期:2015-03-04 14:55:412015年新春福章
日期:2015-03-06 11:59:47
17#
 楼主| 发表于 2014-12-18 21:45 | 只看该作者
经过上面大神指点之后,完全对上了,哈哈,太感谢了
  1. select  r.lession_name "课程",max(case r.srank when 1 then r.stu_name||'+'||r.score end) "第一名(姓名+分数)",max(case r.srank when 2 then r.stu_name||'+'||r.score end) "第二名(姓名+分数)",max(case r.srank when 3 then r.stu_name||'+'||r.score end) "第三名(姓名+分数)" from (select p.stu_name,s.score ,l.lession_name,rank() over(partition by l.lession_id order by s.score desc) srank from t_lession l,t_stu_profile p,t_score s
  2. where s.lession_id=l.lession_id and s.stu_id=p.stu_id) r where r.srank<4
  3. group by r.lession_name;
复制代码

33.png (6.26 KB, 下载次数: 88)

33.png

使用道具 举报

回复
论坛徽章:
3
优秀写手
日期:2014-11-11 06:00:15沸羊羊
日期:2015-03-04 14:55:412015年新春福章
日期:2015-03-06 11:59:47
18#
 楼主| 发表于 2014-12-18 22:06 | 只看该作者
3).找出0611班所有人成绩,以下列格式显示    姓名  语文  数学  英语  物理  化学  总分
    以最简单SQL语句显示,最好不要使用游标与变量
  1. select info.stu_name "姓名",max(decode(info.lession_id,'L001',info.score)) "语文",max(decode(info.lession_id,'L002',info.score)) "数据",max(decode(info.lession_id,'L003',info.score)) "英语",max(decode(info.lession_id,'L004',info.score)) "物理",max(decode(info.lession_id,'L005',info.score)) "化学" from (select p.stu_name,s.score,l.lession_id from t_stu_profile p,t_score s,t_lession l where l.lession_id=s.lession_id and s.stu_id=p.stu_id and p.class_id=0611) info
  2.     group by info.stu_name;
复制代码



使用道具 举报

回复
论坛徽章:
3
优秀写手
日期:2014-11-11 06:00:15沸羊羊
日期:2015-03-04 14:55:412015年新春福章
日期:2015-03-06 11:59:47
19#
 楼主| 发表于 2014-12-18 22:08 | 只看该作者
上面少了个总分,加一个sum就行了
  1. select info.stu_name "姓名",max(decode(info.lession_id,'L001',info.score)) "语文",max(decode(info.lession_id,'L002',info.score)) "数据",max(decode(info.lession_id,'L003',info.score)) "英语",max(decode(info.lession_id,'L004',info.score)) "物理",max(decode(info.lession_id,'L005',info.score)) "化学",sum(info.score) "总分" from (select p.stu_name,s.score,l.lession_id from t_stu_profile p,t_score s,t_lession l where l.lession_id=s.lession_id and s.stu_id=p.stu_id and p.class_id=0611) info
  2.     group by info.stu_name;
复制代码

使用道具 举报

回复
论坛徽章:
3
优秀写手
日期:2014-11-11 06:00:15沸羊羊
日期:2015-03-04 14:55:412015年新春福章
日期:2015-03-06 11:59:47
20#
 楼主| 发表于 2014-12-18 22:09 | 只看该作者
一共有8个SQL题,我只记得这3个,还是在网上找到相同的,放上来的,后面看看还有没线索找到其他剩余的题

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表