Oracle面试SQL

1、关于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   2
   2005-05-10  1   2

答案:

   

1) select rq,sum(case when shengfu='' then 1 else 0 end) as,sum(case when shengfu='' then 1 else 0 end) asfrom tab3 group by rq
2)
select N.rq,N. ,M. from
(select rq,count(*) from tab3 where shengfu=''group by rq)N inner join
(select rq,count(*) from tab3 where shengfu=''group by rq)M on N.rq=M.rq
3)
select a.rq,a.   as,b.  as  from
(select rq,count(shengfu) from tab3 where shengfu='' group by rq) a,
(select rq,count(shengfu) from tab3 where shengfu='' group by rq) b
where a.rq=b.rq;



1.关于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  2
2005-05-10  1  2
--------------------------------------------------------------------------------------------

1) select rq,sum(case when shengfu='' then 1 else 0 end) as,sum(case when shengfu='' then 1 else 0 end) asfrom tab3 group by rq
2)
select N.rq,N. ,M. from

(select rq,count(*) from tab3 where shengfu=''group by rq)N inner join
(select rq,count(*) from tab3 where shengfu=''group by rq)M on N.rq=M.rq
3)
select a.rq,a.   as,b.  as  from

(select rq,count(shengfu) from tab3 where shengfu='' group by rq) a,
(select rq,count(shengfu) from tab3 where shengfu='' group by rq) b
where a.rq=b.rq;

2.表中有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 else c end) from tab4

3.一个日期判断的sql语句

请取出tab5表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间)
---------------------------------------------------------------------------------------------
select * from tab5 t where to_char(t.SendTime,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd')

4.有一张表,里面有3个字段:语文,数学,英语。其中有3条记录分别表示语文70分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路):  
   大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。  
       显示格式:  
       语文              数学                英语  
       及格              优秀                不及格    
-------------------------------------------------------------------------------------------
select
(case when语文>=80 then '优秀' when语文>60 then '及格' else '不及格' end) as 语文,
(case when 数学>=80 then '优秀' when数学>60 then '及格' else '不及格' end) as数学,
(case when英语>=80 then '优秀' when英语>60 then '及格' else '不及格' end) as 英语
from tab5


5.请用一个sql语句得出结果
从table1,table2中取出如table3所列格式数据


table1

月份mon   部门dep   业绩yj
-------------------------------
一月份      01        10
一月份      02        10
一月份      03         5
二月份      02         8
二月份      04         9
三月份      03         8

table2

部门dep      部门名称depname
--------------------------------
      01      国内业务一部
      02      国内业务二部
      03      国内业务三部
      04      国际业务部

table3 (result)

部门dep     一月份      二月份      三月份
---------------------------------------------------
      01      10        null         null
      02      10         8           null
      03      5         null          8
      04      null       9           null

-------------------------------------------------------------------------------------------
1)
select t.depname,
(select yj from tab6 where mon='一月份' and dep=t.dep) 一月份,
(select yj from tab6 where mon='二月份' and dep=t.dep) 二月份,
(select yj from tab6 where mon='三月份' and dep=t.dep) 三月份
from tab7 t

---------------------------------------------------------
2)求总销售额
select
sum(case when t1.mon='一月份' then t1.yj else 0 end) 一月份,
sum(case when t1.mon='二月份' then t1.yj else 0 end) 二月份,
sum(case when t1.mon='三月份' then t1.yj else 0 end) 三月份
 from tab7 t,tab6 t1 where t.dep=t1.dep

6.一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。
-------------------------------------------------------------------------------
select id,count(*) from tab8 group by id having count(*)>1

select * from (select tab8,count(id) as num from tab8 group by id) t where t.num>1

 

7.用一条SQL语句 查询出每门课都大于80分的学生姓名 

name   kecheng   fenshu
张三     语文       81
张三     数学       75
李四     语文       76
李四     数学       90
王五     语文       81
王五     数学       100
王五     英语       90

a): select distinct name from tab9  where  name not in (select distinct name from tab9 where fengshu<=80)

b): select * from tab9 t7 where t7.name not in (select t5.name from (select * from (select t1.kecheng from tab9 t1 group by t1.kecheng),(select t2.name from tab9 t2 group by t2.name)) t4,(select * from tab9) t5 where t4.name = t5.name and t4.kecheng = t5.kecheng and t5.fengshu < 80)
8.一个叫department的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.

select t.bh||'vs'||t1.bh from tab10 t,tab10 t1 where t.bh<>t1.bh这个是分主客场的

select t.bh||'vs'||t1.bh from tab10 t,tab10 t1 where t.bh<>t1.bh and t.bh>t1.bh这个是不分的

9.怎么把这样一个表儿
year  month amount
1991   1     1.1
1991   2     1.2
1991   3     1.3
1991   4     1.4
1992   1     2.1
1992   2     2.2
1992   3     2.3
1992   4     2.4
查成这样一个结果
year m1  m2  m3  m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4

a):
select t.year,
(select a.amout from tab11 a where a.month=1 and a.year=t.year) m1,
(select b.amout from tab11 b where b.month=2 and b.year=t.year) m2,
(select c.amout from tab11 c where c.month=3 and c.year=t.year) m3,
(select d.amout from tab11 d where d.month=4 and d.year=t.year) m4
from tab11 t group by t.year

 

10.拷贝表(拷贝数据,源表名:a 目标表名:b)

SQL: insert into b(a, b, c) select d,e,f from b;

create table test as select * from dept; --从已知表复制数据和结构   

create table test as select * from dept where 1=2; --从已知表复制结构但不包括数据  



11.显示文章、提交人和最后回复时间

  select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b


12.日程安排提前五分钟提醒

13.两张关联表,删除主表中已经在副表中没有的信息
  delete from  fubiao a where a.fid not in(select id from zhubiao)
14.有两个表tab12和tab13,均有key和value两个字段,如果tab13的key在tab12中也有,就把tab13的value换为tab12中对应的value

update tab13 set value=(select value from tab12 where tab12.key=tab13.key)

15.原表:
courseid coursename score
-------------------------------------
   1       java      70
   2       oracle    90
   3       xml       40
   4       jsp       30
   5       servlet   80
-------------------------------------
为了便于阅读,查询此表后的结果显式如下(及格分数为60):
courseid coursename score mark
---------------------------------------------------
  1        java     70  pass
  2        oracle   90  pass
  3        xml      40  fail
  4        jsp      30  fail
  5        servlet  80  pass
---------------------------------------------------
select t.courseid,t.coursename,t.score,(case when score>60 then 'pass' else 'fail' end) mark from tab14 t

16.表15
 a1   a2
 1    a
 1    b
 2    x
 2    y
 2    z
用select能选成以下结果吗?
1 ab
2 xyz

17.题为
有两个表, t1, t2,
Table t1:

SELLER | NON_SELLER
-----    -----
  A       B
  A       C
  A       D
  B       A
  B       C
  B       D
  C       A
  C       B
  C       D
  D       A
  D       B
  D       C

Table t2:

SELLER |  BAL
------  --------
  A       100
  B       200
  C       300
  D       400
要求用SELECT 语句列出如下结果:------如A的SUM(BAL)为B,C,D的和,B的SUM(BAL)为A,C,D的和.......
且用的方法不要增加数据库负担,如用临时表等 

转载于:https://www.cnblogs.com/blogsport/archive/2010/12/09/1900995.html

涉及到了基础SQL的所有知识点 desc emp;描述一下emp表的结构 2 select from salgrade; 3 select ename sal 12 from emp; 4 select 2 3 from emp;会出现14行 5 select 2 3 from dual;只有一行 专门提供计算数学表达式的 6 select sysdate from dual;获取当前系统时间 7 select ename sal 12 annual salary from emp;给sal 12取了个别名叫做annual salary 8 select ename sal 12 "annual salary" from emp;用双引号将别名括起来 这样可以保留住空格 大小写 中文等等 9 select ename comm from emp;其中comm中包括空值 空值不等于0 10 select ename sal 12+comm "总年薪" from emp;任何数加上空值都等于空值 11 select ename||sal from emp;字符串连接符 12 select ename||" nihao" from emp;在SQL语句中 字符串是由单引号括起来的若干字符 13 select ename||" niahao""dajiahao" from emp;如果字符串中本身就有一个单引号 那么在SQL语句中进行表示时 用两个单引号表示一个单引号 14 select distinct deptno from emp;select distinct deptno job from emp; 15 select from emp where ename "CLARK";where中添加条件约束 并且字符串需要加上单引号来表示 16 select ename job sal from emp where ename > "CBA";字符串比较 先比较第一个 比C大 就TRUE 第一个字母相同)否则比较下一个 比B大 就TRUE 否则 17 select ename sal from emp where sal<>ALL 1500 2000 2500 3000 ;<>表示不等于 <>ALL表示不等于所有的( ) 18 select from emp where sal between 800 and 1300;和select from emp where sal> 800 and sal< 1300;的效果是一样的 19 select from emp where comm is null;is null表示是空值 is not null表示非空值 不可以用 20 select ename sal from emp where ename in "SMITH" "CLARK" "SCOTT" ;ename在()中的人选出来 ()中也可以是其他类型 21 select ename sal hiredate from emp where hiredate > "20 2月 81";日期的格式 22 select ename sal from emp where ename like " A%"; 表示一个字符 %表示0个或多个字符 23 select from emp where ename like "% %%" escape " ";其中 作为转义字符的符号 select from emp where ename like "% %%"; %表示百分号 24 select empno ename from emp order by empno asc;升序排列(默认);select deptno dname from dept order by deptno desc;降序排列 25 select empno ename from emp where deptno<>10 order by empno desc;可以先选择在排序 26 select ename sal deptno from emp order by deptno asc ename desc;先按照deptno进行升序排列 在deptno相同的地方 按照ename进行降序排列 27 select ename sal 12 annual sal from emp where ename not like " A%’ and sal > 800 order by desc; 28 select ename sal from emp where lower ename like " a%";小写 29 select substr ename 2 3 from emp;从第二个字符开始 截3个字符出来作为结果 30 select chr 65 from dual;把ASCII码转化成字符 31 select ascii "A" from dual;把字符转化成ASCII码 32 select round 23 643 2 from dual;四舍五入为23 64 默认第二个参数为0 即四舍五入到各位 也可以为 1 则为20 33 select to char sal "$99 999 9999" from emp;美元 9代表一个数字 “ ”代表一个千分位符号 select to char sal "L99 999 9999" from emp;人民币 select to char sal "L00000 0000" from emp; 34 select to char hiredate "YYYY MM DD HH:MI:SS" from emp;转换Date类型的时间表示方法 select to char sysdate "MM DD YY HH24:MI:SS" from dual;24小时制 35 select ename hiredate from emp where hiredate > to Date "1981 2 3 14:23:12" "YYYY MM DD HH24:MI:SS" ;to date "" "" 将日期转换成自己想要的格式 36 select ename sal 12 + nvl comm 0 from emp;nvl comm 0 是将comm中的空值设为0 非空值还是本身 37 to number "$1250 00" "$9 999 99" ;有点问题 ">涉及到了基础SQL的所有知识点 desc emp;描述一下emp表的结构 2 select from salgrade; 3 select ename sal 12 from emp; 4 select 2 3 from emp;会出现14行 5 select 2 3 from dual;只有一行 专门提供计算数学表达式的 6 select sysdate from dual;获 [更多]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值