oracle pl/sql 面试,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

创建过程如下:

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','负');

方法一:利用子查询

select a.rq,a.胜,b.负 from (select rq,count(shengfu) as 胜 from tmp where shengfu='胜' group by rq) a,(select rq,count(shengfu) as 负 from tmp where shengfu='负' group by rq) b   where a.rq=b.rq

方法二:高级查询---使用case表达式

case表达式可以在sql中实现if-then-else型的逻辑,而不用去使用PL/SQL。case工作方式与decode类似,但是我们应该使用case,因为它与ansi兼容。

有两种类型的case表达式

a 简单case表达式,使用case表达式确定返回值

b 搜索case表达式,使用条件确定返回值

select rq,sum(case when shengfu='胜' then 1 else 0 end) as 胜,

sum(case when shengfu='负'then 1 else 0 end) as 负from tmp group by rq

方法三:通过表的内连接与子查询联合实现,可以说是另一种方式,思想类似于方法一。

select a.rq,a.胜,b.负 from   (select rq,count(shengfu) 胜 from tmp where shengfu='胜'group by rq) a innerjoin   (select rq,count(shengfu) 负 from tmp where shengfu='负'group by rq) b

on a.rq=b.rq

2.表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。

create table tmp2(a int,b int ,c int);

insert into tmp values(10,20,30);

insert into tmp values(5,20,30);

insert into tmp values(10,7,30);

select (case when a>b then a else b end),(case when b>c then b else c end) from tmp2;

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

请取出tab5表中日期(SendTime字段)为当天的所有记录?(SendTime字段为datetime型,包含日期与时间)

select * from table t where to_char(t.SendTime,'yyyy-mm-dd')=to_char(sysdate,'yyyy-mm-dd')

此题关键在于转换sendtime字段的格式,转换成日期格式。

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 table

想到利用case表达式是关键。

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 table1 where mon='一月份' and dep=t.dep) 一月份,

(select yj from table1 where mon='二月份' and dep=t.dep) 二月份,

(select yj from table1 where mon='三月份' and dep=t.dep) 三月份

from table1 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 fromtab9 t2 group by t2.name)) t4,(select * from tab9) t5 where t4.name = t5.name and t4.kecheng = t5.kecheng and t5.fengshu

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

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)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441889.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【HDU - 5094】 Maze (状态压缩+bfs)

题干&#xff1a; This story happened on the background of Star Trek. Spock, the deputy captain of Starship Enterprise, fell into Klingon’s trick and was held as prisoner on their mother planet Qo’noS. The captain of Enterprise, James T. Kirk, had to f…

oracle idl_ub1$,system表空间急剧增大原因分析

system表空间增大是正常的&#xff0c;但急剧增大是不合理的。1有可能是用户对象错误的放在系统表空间中2也可能是system表空间的UNDO过大3还有可能和高级复制的空间使用有关可通过如下语句查看一下是不是有应用的段放到了SYSTEM中&#xff1a;select OWNER,SEGMENT_NAME,SEGME…

【HDU - 1087】Super Jumping! Jumping! Jumping! (最大上升子序列类问题,dp)

题干&#xff1a; Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more than two pl…

oracle启动监听读取哪个文件,监听服务启动及数据文件恢复oracle数据库

最近遭遇了 oralce 监听服务启动了 又自行关闭的 悲惨经历我把我的过程和大家分享一下&#xff01;1)排查原因程序员是懒惰的&#xff0c;我始终都希望能够成功启动监听服务&#xff0c;但是就是事与愿违有一下方式可能不能成功启动监听1.端口占用&#xff0c;oralce 要用到152…

linux串口写入命令失败,linux – 从串口读取失败

我有以下C程序&#xff1a;#include #include #include int main(){int fd open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NONBLOCK);if(fd < 0){perror("Could not open device");}printf("Device opened\n");struct termios options;tcgetattr…

【HDU - 1172】猜数字 (枚举暴力)

题干&#xff1a; 猜数字游戏是gameboy最喜欢的游戏之一。游戏的规则是这样的&#xff1a;计算机随机产生一个四位数&#xff0c;然后玩家猜这个四位数是什么。每猜一个数&#xff0c;计算机都会告诉玩家猜对几个数字&#xff0c;其中有几个数字在正确的位置上。 比如计算机随…

linux内核支持的加密算法,Linux Kernel(Android) 加密算法总结(三)-应用程序调用内核加密算法接口...

本文将主要介绍&#xff0c;如何在应用程序空间中(user space) 调用内核空间(kernel space)加密模块提供的加密算法API。方法一&#xff1a;通过调用crypto: af_alg - User-space interface for Crypto API&#xff0c; Herbert Xu 2010年&#xff0c;给内核2.6.X 接口实现下面…

【HDU - 2571】 命运(记忆化搜索)

题干&#xff1a; 穿过幽谷意味着离大魔王lemon已经无限接近了&#xff01; 可谁能想到&#xff0c;yifenfei在斩杀了一些虾兵蟹将后&#xff0c;却再次面临命运大迷宫的考验&#xff0c;这是魔王lemon设下的又一个机关。要知道&#xff0c;不论何人&#xff0c;若在迷宫中被…

【HDU - 2089 】不要62 (dp)

题干&#xff1a; 杭州人称那些傻乎乎粘嗒嗒的人为62&#xff08;音&#xff1a;laoer&#xff09;。 杭州交通管理局经常会扩充一些的士车牌照&#xff0c;新近出来一个好消息&#xff0c;以后上牌照&#xff0c;不再含有不吉利的数字了&#xff0c;这样一来&#xff0c;就可…

nmon监控linux内存,使用Nmon监控Linux系统性能

Nmon (又称 Nigel’s Monitor) 是一款常用的系统性能监视工具&#xff0c;由 IBM 工程师 Nigel Griffiths 开发&#xff0c;适用于 AIX 和 Linux 操作系统。该工具可以直接在屏幕上显示当前操作系统的资源利用率&#xff0c;以帮助大家找出系统瓶颈和协助系统调优。由于其十分出…

【Codeforces - 632C】The Smallest String Concatenation (对string巧妙排序)

题干&#xff1a; Youre given a list of n strings a1, a2, ..., an. Youd like to concatenate them together in some order such that the resulting string would be lexicographically smallest. Given the list of strings, output the lexicographically smallest…

linux mysql授权远程登录,Linux中 MySQL 授权远程连接的方法步骤

说明&#xff1a;当别的机子(IP )通过客户端的方式在没有授权的情况下是无法连接 MySQL 数据库的&#xff0c;如果需要远程连接 Linux 系统上的 MySQL 时&#xff0c;必须为其 IP 和 具体用户 进行 授权 。一般 root 用户不会提供给开发者。如&#xff1a;使用 Windows 上的 SQ…

【HDU - 3466 】Proud Merchants(dp,背包问题,巧妙排序)

题干&#xff1a; Recently, iSea went to an ancient country. For such a long time, it was the most wealthy and powerful kingdom in the world. As a result, the people in this country are still very proud even if their nation hasn’t been so wealthy any more…

linux 双通道 磁盘,HP MSA2012SA 双通道 磁盘阵列配置说明 for linuxoracle

HP MSA2012SA磁盘阵列配置说明说明&#xff1a;可以先安装HP服务器的操作系统&#xff0c;等安装完了以后再配置磁阵&#xff1b;1、安装好服务器的操作系统&#xff1b;该安装的包都安装上&#xff1b;2、磁盘阵列上架&#xff0c;加电&#xff0c;SAS线不用接服务器&#xff…

linux mariadb 乱码,MariaDB插入中文数据乱码解决过程

基本情况&#xff1a;MariaDB安装方式&#xff1a;yum乱码解决过程&#xff1a;1.查看当前数据库编码(登录数据库后)# show variables like character%;(上图为已经配置成功)2.如果结果不为上图则需要设置数据库配置文件•编辑 /etc/my.cnf.d/client.cnf 文件&#xff0c;添加如…

【nyoj - 890】 分东西 (水题 二进制)

题干&#xff1a; 分东西 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB 难度&#xff1a;1 输入 第一行输出一个数i表示有i组情况&#xff08;0<i<10&#xff09; 接下来的i行&#xff0c;每一行输入两个个数M(0<M<1000000)和N(0<N<2…

摩托罗拉为什么要限制自家linux手机,摩托罗拉为何在安卓手机大放异彩的时候,突然开始衰败了呢?...

摩托罗拉从一开始就走在了安卓的道路上&#xff0c;并且魅力四射&#xff0c;可以说一时间也是风光无比。对比诺基亚坚定的走向WP之路&#xff0c;这一点摩托罗拉没有走错。安卓当时的热门机中&#xff0c;摩托罗拉的里程碑系列可以算作是经典之作。销量也可以进入当年的前三。…

【POJ - 3624 】Charm Bracelet (dp,0-1背包裸题)

题干&#xff1a; Bessie has gone to the malls jewelry store and spies a charm bracelet. Of course, shed like to fill it with the best charms possible from the N (1 ≤ N ≤ 3,402) available charms. Each charm i in the supplied list has a weight Wi (1 ≤ Wi…

pta输出三角形字符阵列c语言,C语言l|博客园作业11

这个作业属于哪个课程C语言程序设计II这个作业要求在哪里链接我在这个课程的目标是掌握C语言以及熟练运用这个作业在哪个具体方面帮助我实现目标询问同学&#xff0c;百度&#xff0c;vs2019上的报错参考文献链接1.1 题目名6-1 统计某类完全平方数本题要求实现一个函数&#xf…

【nyoj - 252】 01串(简单dp)

题干&#xff1a; 01串 时间限制&#xff1a;1000 ms | 内存限制&#xff1a;65535 KB 难度&#xff1a;2 输入 第一行有一个整数n&#xff08;0<n<100&#xff09;,表示有n组测试数据; 随后有n行&#xff0c;每行有一个整数m(2<m<40)&#xff0c;表示01串的…