oracle学习笔记(十) 查询练习(一)

news/2024/10/6 19:30:36 标签: 数据库, java

查询练习一

表创建
create table employee as select * from soctt.emp ;
--记得授权
sysdba用户登录
grant select on scott.emp to $username$

--表结构
create table empployee_demo(
   empno     number(4) not null primary key,  --员工编号,主键
   ename     varchar2(10) not null unique,    --员工名,唯一键
   job       varchar2(9),                     --职位、工作
   mgr       number(4),                       --经理编号
   hiredate  date default sysdate,            --入职日期,默认约束
   sal       number(7,2) check(sal>=500 and sal<=10000),   --工资
   comm      number(7,2),                     --资金
   deptno    number(2)                        --部门编号
)

--16. 找出各月倒数第三天受雇的所有员工
select * from employee where last_day(hiredate)-2=hiredate;

--17. 找出早于25年前受雇佣的员工
select * from employee where months_between(hiredate,sysdate)/12>25;
select * from employee where year(sysdate)-year(hiredate)>25;

--18. 以首字母大写其它字母小写的方式显示所有的员工的姓名
select upper(substr(ename,0,1))||lower(substr(ename,1)) from employee;
--下面这两种写法都是可以获得同样的结果,不过不太清楚加nls的区别
select nls_initcap(ename) from employee;
select initcap(ename) from employee;

--以首字母小写其它字母大写的方式显示所有的员工的姓名。
select lower(substr(ename,1,1))||upper(substr(ename,2)) from employee;

--19. 显示正好为5字符的员工的姓名
select ename from employee where length(ename)=5;

--20. 显示不带有‘R’的与员工的姓名
select ename from employee where ename not like '%R%';

--21. 显示所有员工姓名的前三个字符。
select substr(ename,1,3) from employee --注意,与java中截取字符串的有所区别

--22. 显示所有员工的姓名,用 A 替换 a 
select replace(ename,'a','A') from employee; --把a替换成A

--23. 显示 满25年 服务年限的员工的姓名 和受雇日期
select ename,hiretate from employee where months_between(hiredate,sysdate)/12 >=25

--24. 显示员工的详细资料,按姓名排序,姓名相同按工资降序排序。
select * from employee order by ename,sal desc; --asc升序 desc降序 默认升序

--25. 显示与员工的姓名和受雇日期,根据其服务年限,将最老的员工排在最前面。
select ename,hiredate,months_between(hiredate,sysdate) 
from employee 
order by months_between(hiredate,sysdate)

select ename,hiredate,months_between(sysdate,hiredate) 
from employee 
order by months_between(sysdate,hiredate) desc

--26. 显示所有员工的姓名,工作和薪金,按工作的降序排序,若工作相同按薪金排序
select ename,job,sal from employee 
order by job,sal;

/*
27. 显示所有员工的姓名,加入公司的年份和月份,按受雇日期所在的月排序,
    若月份相同,则将最早的年份的员工排在最前面
*/
select ename,JOB,TO_CHAR(hiredate,'yyyy') year,
    TO_CHAR(hiredate,'mm') month
  from employee 
    order by month,year;

转载于:https://www.cnblogs.com/kexing/p/10864698.html


http://www.niftyadmin.cn/n/712528.html

相关文章

Python札记3:可变对象和不可变对象

Python中有可变对象和不可变对象之分。可变对象创建后可改变但地址不会改变&#xff0c;即变量指向的还是原来的变量&#xff1b;不可变对象创建之后便不能改变&#xff0c;如果改变则会指向一个新的对象。 Python中dict、list是可变对象&#xff0c;str、int、tuple、float是…

ios-AppDelegate常用的方法

//UIApplicationDelegate以下的方法都是实现了前面这个协议的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.//完成应用初始化后需要执行的…

python创建提示用户输入_python 3.6基础:创建一个捕获用户输入的程序,并使用变量存储要打印的地址...

请帮忙 &#xff1a;)编写程序以从命令行提示中收集用户输入的两个完整地址(姓名&#xff0c;街道号&#xff0c;街道名称&#xff0c;城市&#xff0c;州和邮政编码) . 首先需要创建变量以将地址存储在变量中&#xff0c;然后创建相应的内置函数以从用户的地址中捕获输入 . 街…

洛谷P1926-小书童——刷题大军(01背包)

题目背景&#xff1a; 数学是火&#xff0c;点亮物理的灯&#xff1b;物理是灯&#xff0c;照亮化学的路&#xff1b;化学是路&#xff0c;通向生物的坑&#xff1b;生物是坑&#xff0c;埋葬学理的人。 文言是火&#xff0c;点亮历史宫灯&#xff1b;历史是灯&#xff0c;照亮…

云服务器 ECS 学习笔记

云服务器 ECS云服务器 Elastic Compute Service&#xff08;ECS&#xff09;是阿里云提供的一种基础云计算服务阿里云专有网络&#xff1a;在阿里云专有网络之上&#xff0c;可以产生更多的业务可能性。阿里云专有网络更加稳定和安全。 纵向的弹性&#xff0c;即单个服务器的配…

洛谷P2639-[USACO09OCT]Bessie‘s Weight Problem G(01背包)

题目描述&#xff1a; Bessie像她的诸多姊妹一样&#xff0c;因为从Farmer John的草地吃了太多美味的草而长出了太多的赘肉。所以FJ将她置于一个及其严格的节食计划之中。她每天不能吃多过H (5 < H < 45,000)公斤的干草。 Bessie只能吃一整捆干草&#xff1b;当她开始吃一…

Python札记4:连接字符串使用join还是+号

你应该知道&#xff0c;连接字符串既可以用join函数&#xff0c;也可以用号&#xff1a; a "hello " "world" b " ".join(["hello", "world"]) print(a) # hello world print(b) # hello world两种方法的输出是相同的&a…

图像的几何变换中的函数简述

本文是学习opencv-python官方教程的函数总结 cv2.resize(src, dst, dsize, fx, fy, interpolation)参数&#xff1a; src&#xff1a;输入图像 dst&#xff1a;输出图像&#xff0c;尺寸等于dsize或者通过缩放因子计算的尺寸 dsize&#xff1a;输出图像尺寸。如果为空…