博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库知识点
阅读量:4605 次
发布时间:2019-06-09

本文共 7678 字,大约阅读时间需要 25 分钟。

基本概念(1)any/all,构成 where 子句的条件判断,any:表示或(or)的概念,all:则表示与(and)的概念,这两个关键字的出现是为了语句的简化;(2)先分组再做聚合,逻辑上也应当如此,聚合(取最值)之后便无分组的必要;select region, sum(population), sum(area) from bbc group by region;(3)group by having,having 对分组后的数据进行筛选,这是 where 所做不到的;1. 不使用 min,找出表 ppp 中 num(列)最小的数select num from ppp where num <= all(select num from ppp);不可以使用 min 函数,但可以实用 order by 和 limit 相组合呀;select * from ppp order by num desc limit 1;举一反三自然,不使用 max,找出表 ppp 中 num 最大的数:select num from ppp where num >= all(select num from ppp);select num from ppp order by num limit 1;2. 选择表 ppp 中的重复记录select * from ppp   where num in (select num from ppp group by num having count(num) > 1);注意,如下的语句只返回单独的一条记录select * from ppp group by num having count(num) > 1;举一反三查询表中出现 四 次的记录,group by havingselect * from ppp    where num in (select num from ppp group by num having count(num) = 4);3. 用一条SQL语句查询出每门课都大于80分的学生姓名name kecheng fenshu 张三 语文 81 张三 数学 75 李四 语文 76 李四 数学 90 王五 语文 81 王五 数学 100 王五 英语 90select distinct name from stu where name not in (select distinct name from stu where fenshu <= 80);4. 影分身,一表当做两表用表形式如下: Year Salary 2000 1000 2001 2000 2002 3000 2003 4000 想得到如下形式的查询结果 Year Salary 2000 1000 2001 3000 2002 6000 2003 10000 sql语句怎么写? select b.year, sum(a.salary) from hell0 a, hello b where a.year <= b.year group by b.y2007年07月27日 星期五 上午 08:421.一道SQL语句面试题,关于group by表内容:2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2005-05-10 负如果要生成下列结果, 该如何写sql语句?            胜 负2005-05-09 2 22005-05-10 1 2------------------------------------------create table #tmp(rq varchar(10),shengfu nchar(1))insert into #tmp values('2005-05-09','胜')insert into #tmp values('2005-05-09','胜')insert into #tmp values('2005-05-09','负')insert into #tmp values('2005-05-09','负')insert into #tmp values('2005-05-10','胜')insert into #tmp values('2005-05-10','负')insert into #tmp values('2005-05-10','负')1)select rq, sum(case when shengfu='胜' then 1 else 0 end)'胜',sum(case when shengfu='负' then 1 else 0 end)'负' from #tmp group by rq2) select N.rq,N.勝,M.負 from (select rq,勝=count(*) from #tmp where shengfu='胜'group by rq)N inner join(select rq,負=count(*) from #tmp where shengfu='负'group by rq)M on N.rq=M.rq3)select a.col001,a.a1 胜,b.b1 负 from(select col001,count(col001) a1 from temp1 where col002='胜' group by col001) a,(select col001,count(col001) b1 from temp1 where col002='负' group by col001) bwhere a.col001=b.col0012.请教一个面试中遇到的SQL语句的查询问题表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。------------------------------------------select (case when a>b then a else b end ),(case when b>c then b esle c end)from table_name3.面试题:一个日期判断的sql语句?请取出tb_send表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间)------------------------------------------select * from tb where datediff(dd,SendTime,getdate())=04.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):     大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。         显示格式:         语文              数学                英语         及格              优秀                不及格    ------------------------------------------select(case when 语文>=80 then '优秀'        when 语文>=60 then '及格'else '不及格') as 语文,(case when 数学>=80 then '优秀'        when 数学>=60 then '及格'else '不及格') as 数学,(case when 英语>=80 then '优秀'        when 英语>=60 then '及格'else '不及格') as 英语,from table7.请用一个sql语句得出结果从table1,table2中取出如table3所列格式数据,注意提供的数据及结果不准确,只是作为一个格式向大家请教。如使用存储过程也可以。table1月份mon 部门dep 业绩yj-------------------------------一月份      01      10一月份      02      10一月份      03      5二月份      02      8二月份      04      9三月份      03      8table2部门dep      部门名称dname--------------------------------      01      国内业务一部      02      国内业务二部      03      国内业务三部      04      国际业务部table3 (result)部门dep 一月份      二月份      三月份--------------------------------------      01      10        null      null      02      10         8        null      03      null       5        8      04      null      null      9------------------------------------------1)select a.部门名称dname,b.业绩yj as '一月份',c.业绩yj as '二月份',d.业绩yj as '三月份'from table1 a,table2 b,table2 c,table2 dwhere a.部门dep = b.部门dep and b.月份mon = '一月份' anda.部门dep = c.部门dep and c.月份mon = '二月份' anda.部门dep = d.部门dep and d.月份mon = '三月份' and2)select a.dep,sum(case when b.mon=1 then b.yj else 0 end) as '一月份',sum(case when b.mon=2 then b.yj else 0 end) as '二月份',sum(case when b.mon=3 then b.yj else 0 end) as '三月份',sum(case when b.mon=4 then b.yj else 0 end) as '四月份',sum(case when b.mon=5 then b.yj else 0 end) as '五月份',sum(case when b.mon=6 then b.yj else 0 end) as '六月份',sum(case when b.mon=7 then b.yj else 0 end) as '七月份',sum(case when b.mon=8 then b.yj else 0 end) as '八月份',sum(case when b.mon=9 then b.yj else 0 end) as '九月份',sum(case when b.mon=10 then b.yj else 0 end) as '十月份',sum(case when b.mon=11 then b.yj else 0 end) as '十一月份',sum(case when b.mon=12 then b.yj else 0 end) as '十二月份',from table2 a left join table1 b on a.dep=b.dep8.华为一道面试题一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。------------------------------------------select id, Count(*) from tb group by id having count(*)>1select * from(select count(ID) as count from table group by ID)T where T.count>1 表形式如下: Year      Salary 2000        1000 2001        2000 2002        3000 2003        4000 想得到如下形式的查询结果 Year      Salary 2000      1000 2001      3000 2002      6000 2003      10000 sql语句怎么写? 连接查询 SELECT b.YEAR, SUM(a.salary) salary FROM hello a, hello b WHERE a.YEAR <= b.YEAR GROUP BY b.YEAR 子查询 select year ,(select sum(salary) from hello as B where B.year<=A.year ) from hello as A   1.用一条SQL语句查询出每门课都大于80分的学生姓名name   kecheng   fenshu张三     语文       81张三     数学       75李四     语文       76李四     数学       90王五     语文       81王五     数学       100王五     英语       90A: select distinct name from table where name not in (select distinct name from table where fenshu<=80)2.学生表 如下:自动编号   学号   姓名课程编号课程名称分数1        2005001 张三 0001      数学    692        2005002 李四 0001      数学    893        2005001 张三 0001      数学    69删除除了自动编号不同,其他都相同的学生冗余信息A: delete tablename where 自动编号 not in(select min(自动编号) from tablename group by 学号,姓名,课程编号,课程名称,分数)一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.你先按你自己的想法做一下,看结果有我的这个简单吗?答:select a.name, b.namefrom team a, team bwhere a.name < b.name 请用SQL语句实现:从TestDB数据表中查询出所有月份的发生额都比101科目相应月份的发生额高的科目。请注意:TestDB中有很多科目,都有1-12月份的发生额。AccID:科目代码,Occmonth:发生额月份,DebitOccur:发生额。数据库名:JcyAudit,数据集:Select * from TestDB答:select a.*from TestDB a,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) bwhere a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur************************************************************************************面试题:怎么把这样一个表儿year   month amount1991   1     1.11991   2     1.21991   3     1.31991   4     1.41992   1     2.11992   2     2.21992   3     2.31992   4     2.4查成这样一个结果year m1   m2   m3   m41991 1.1 1.2 1.3 1.41992 2.1 2.2 2.3 2.4答案一、select year,(select amount from   aaa m where month=1   and m.year=aaa.year) as m1,(select amount from   aaa m where month=2   and m.year=aaa.year) as m2,(select amount from   aaa m where month=3   and m.year=aaa.year) as m3,(select amount from   aaa m where month=4   and m.year=aaa.year) as m4from aaa   group by year 这个是Oracle  中做的:select * from (select name, year b1, lead(year) over(partition by name order by year) b2, lead(m,2) over(partition by name order by year) b3,rank()over(partition by name order by year) rk from t) where rk=1;重点https://wenku.baidu.com/view/b1a772ee19e8b8f67c1cb922.htmlhttp://www.cnblogs.com/GT_Andy/archive/2009/12/25/1921911.htmlhttps://wenku.baidu.com/view/36246a4133687e21af45a977.html

 

转载于:https://www.cnblogs.com/by-1642146903/p/7236945.html

你可能感兴趣的文章
自定义分页
查看>>
[转]DELPHI——调试(1)
查看>>
JS秒数转成分秒时间格式
查看>>
xp_cmdshell 命令的开启与关闭,和状态查询
查看>>
Linux sudoers
查看>>
MySQL详解(18)-----------分页方法总结
查看>>
bzoj 4595 激光发生器
查看>>
multi cookie & read bug
查看>>
js时间转换
查看>>
(转载) Android Studio你不知道的调试技巧
查看>>
POJ2231 Moo Volume 递推 C语言
查看>>
struts2类型转换的具体流程
查看>>
Hdu 1203 I NEED A OFFER!
查看>>
php文件上传类
查看>>
CF219D Choosing Capital for Treeland
查看>>
luogu P3809 【模板】后缀排序
查看>>
JVM 调优工具
查看>>
SCTF 2014 pwn题目分析
查看>>
集合以及特殊集合
查看>>
USACO 2.2 Runaround Numbers
查看>>