首页 > 其他分享 >查询题目,日期函数

查询题目,日期函数

时间:2024-04-09 22:46:36浏览次数:20  
标签:11 题目 函数 sal 0.5 查询 emp dual select

老王是荆职百获食堂的采购员,每天买白菜50斤,土豆30斤,茄子30斤,豆角50斤,采购完都会记录到下面的Vegetable 表中,请你用学到的Oracle知识帮帮老王算算帐。

编号

白菜单价

土豆单价

茄子单价

豆角单价

日期

001

0.35

0.5

0.5

1.2

2017/11/10

002

0.4

0.6

0.5

1.3

2017/11/11

003

0.3

0.5

0.6

1.3

2017/11/12

004

0.5

0.5

0.5

1.35

2017/11/13

005

0.45

0.6

0.6

1.4

2017/11/14

006

0.45

0.5

0.55

1.2

2017/11/15

007

0.5

0.6

0.5

1.25

2017/11/16

 

 

1 请你计算一下老王每天的采购支出以及这周的采购支出

2  计算那天花钱最多

3 求出豆角一周的平均价格

system 123456
create user wang identified by wang123456;
grant create table to wang;
grant create session to wang;
grant resource to wang;
grant connect to wang;
grant create sequence to wang;
conn wang/wang123456;
create sequence myseq;
--使用序列
select myseq.nextval 序号  from dual;
select myseq.nextval 序号  from dual;
select myseq.nextval 序号  from dual;
select myseq.nextval 序号  from dual;
select myseq.currval 当前序号 from dual;
select myseq.currval 当前序号 from dual;
select myseq.nextval 序号  from dual;
select myseq.currval 当前序号 from dual;

select 
sequence_name,max_value,min_value,
increment_by,last_number from SEQ
where sequence_name=upper('myseq');

select to_char (myseq.nextval,'000') 序列号 from dual;

--循环序列3 5 7 9 11
create sequence seq2
increment by 2
maxvalue 11
minvalue 2
cycle
nocache;


select seq2.nextval 序号  from dual;
select seq2.nextval 序号  from dual;
select seq2.nextval 序号  from dual;
select seq2.nextval 序号  from dual;
select seq2.currval 当前序号 from dual;
select seq2.currval 当前序号 from dual;
select seq2.nextval 序号  from dual;
select seq2.currval 当前序号 from dual;


create sequence seq3
increment by 1
start with 190325114710914
nocycle;
select 'wxA'||seq3.nextval 快递单号 from dual;
select 'wxA'||seq3.nextval 快递单号 from dual;
select 'wxA'||seq3.nextval 快递单号 from dual;
select 'wxA'||seq3.nextval 快递单号 from dual;


create table vegetable        
(
编号 number(3)primary key,        
白菜 number(3,2) not null,        
土豆 number(3,2) not null,        
茄子 number(3,2) not null,        
豆角 number(3,2) not null,        
日期 date        
);

insert into vegetable
select 001,0.35,0.5,0.5,1.2,'10-11月-17'from dual union
select 002,0.4,0.6,0.5,1.3,'11-11月-17'from dual union
select 003,0.3,0.5,0.6,1.3,'12-11月-17'from dual union
select 004,0.5,0.5,0.5,1.35,'13-11月-17'from dual union
select 005,0.45,0.6,0.6,1.4,'14-11月-17'from dual union
select 006,0.45,0.5,0.55,1.2,'15-11月-17'from dual union
select 007,0.5,0.6,0.5,1.25,'16-11月-17'from dual;


--用序列插入
create sequence seq4;
alter table vegetable modify 编号 char(10);
insert into vegetable values(to_char(seq4.nextval,'000'),0.35,0.5,0.5,1.2,'10-11月-17');
insert into vegetable values(to_char(seq4.nextval,'000'),0.4,0.6,0.5,1.3,'11-11月-17');
insert into vegetable values(to_char(seq4.nextval,'000'),0.3,0.5,0.6,1.3,'12-11月-17');
insert into vegetable values(to_char(seq4.nextval,'000'),0.5,0.5,0.5,1.35,'13-11月-17');
insert into vegetable values(to_char(seq4.nextval,'000'),0.45,0.6,0.6,1.4,'14-11月-17');
insert into vegetable values(to_char(seq4.nextval,'000'),0.45,0.5,0.55,1.2,'15-11月-17');
insert into vegetable values(to_char(seq4.nextval,'000'),0.5,0.6,0.5,1.25,'16-11月-17');
select * vegetable;


select 日期,白菜*50+土豆*30+茄子*30+豆角*50 总消费 from vegetable;


--视图的概念:把你的查询结果做一个封装叫做视图
conn system/123456
grant create view to wang;
conn wang/wang123456;
create view xiaofei_view
as select 日期,白菜*50+土豆*30+茄子*30+豆角*50 总消费 from vegetable;

select * from xiaofei_view;

select sum(总消费) from xiaofei_view;

select 日期 from xiaofei_view where 总消费=(select max(总消费)from xiaofei_view);

select avg(豆角) from vegetable;



作业
create table disney
(
id number primary key,
data varchar2(10)
);

create sequence id_seq5;

create or replace trigger bifer_disney_id_pk
beFORe insert
on disney
FOR each row
BEGIN
SELECT id_seq5.nextval into :new.id FROM dual;
END;
/

insert into disney(data) VALUES('TOM');
insert into disney(id,data) VALUES(6,'Jerry');

select * from Disney;

作业二
conn system/123456;
create user xiaoxue identified by xiaoxue123;
grant create table to xiaoxue;
grant create session to xiaoxue;
grant resource to xiaoxue;
grant connect to xiaoxue;
grant create sequence to xiaoxue;
conn xiaoxue/xiaoxue123;
create sequence myseq;
conn system/123456;
grant create view to xiaoxue;
conn xiaoxue/xiaoxue123;

create sequence seq6
increment by 1
start with 1000421
nocycle;
select 'wxA'||seq3.nextval 快递单号 from dual;
select 'wxA'||seq3.nextval 快递单号 from dual;
select 'wxA'||seq3.nextval 快递单号 from dual;
select 'wxA'||seq3.nextval 快递单号 from dual;

create table chanping
(
产品编号 number(10),
产品名称 varchar2(8),
质检员 number(5),
生产日期 date
);

 

 

 1 求出king 的下属(mgr)并封装为视图
 2 求出oracle公司的总人数(count)并封装为视图
 3  求出工资大于1000并封装为视图
 4  求出工号(empno),工资(sal)名字(ename)并封装为视图
 5   求出每个人的姓名(ename),部门编号(deptno) 并封装为视图。


 conn   system/123456;
 grant create view to scott;
 conn scott/tiger;


 
 create view emp_mgr_view as
select * from emp  where mgr=7839;
 
 create view emp_count_view as
select count(ename) 总人数 from emp  ;

 create view emp_sal_1000_view as
select * from emp  where sal>1000;

 create view emp_empno_sal_ename_view as
select empno,sal,ename from emp  ;

 create view emp_ename_deptno_view as
select ename,deptno from emp ;
用户名Scott
密码tiger
select * from emp;
--展示表结构
desc emp;

--字段查询
select ename,sal from emp;

select ename,empno from emp;

select ename,hiredate ,sal,comm from emp;

select ename,job,sal from emp;

select ename,mgr,deptno from emp;


--更改语句
--给所有人涨10%工资
select ename ,sal from emp;
update emp set sal=sal*1.1;
--领导说不能涨
rollback;
select ename,sal from emp;
--换种方案涨工资
--大于2000的涨20%,小于2000的涨10%
update emp set sal=sal*1.2 where sal>2000;
update emp set sal=sal*1.1 where sal<2000;

--永久生效,不能更改
commit;


--distinct 去除重复列
select ename ,job from emp;

select distinct job from emp;

select distinct deptno from emp;

select distinct mgr from emp;

--% 通配符
--查询首字母为a的员工信息
select * from emp where ename like 'A%';
--查询第二个字母为a的员工信息
select * from emp where ename like '_A%';
--查询第三个字母为a的员工信息
select * from emp where ename like '__A%';
--查询第四个字母为a 的员工信息
select * from emp where ename like '___A%';
--查询有a的员工
select * from emp where ename like '%A%';
--查询没有a的员工姓名
select ename from emp where ename not like '%A%';
--员工编号empno出现9的员工
select * from emp  where empno like '&9&';
--上司编号mgr 出现8的姓名
select * from emp  where mgr like '&8&';
--员工姓名ename出现s的员工的姓名
select * from emp  where ename like '&s&';


--处理空值null
select ename ,comm from emp where comm is not null;
select ename,comm from emp where comm is null;
select ename 姓名 ,nvl(comm,100)+sal*13 年薪 from emp;


--p144,6.46-6.54
select deptno,count(*),sum(sal) from emp GROUP BY deptno;


select job ,count(*),avg(sal)
from emp
where deptno=20
GROUP BY job
having avg(sal)>1000;

select * from emp,dept;

select * from emp e,dept d where e.deptno=d.deptno;

select empno ,sal ,s.* from emp e, salgrade s where sal between s.losal and s.hisal;

select e.empno,e.ename,e.mgr ,b.empno,b.ename from emp e,emp b
where e.mgr =b.empno;

select * from emp e left join dept d on e.deptno =d.deptno;

select * from emp e right join dept d on e.deptno =d.deptno;

select * from emp e full join dept d on e.deptno=d.deptno;




--1平均工资,最低工资,最高工资
--job分类 平均工资,最低工资,最高工资
select * deptno,count(*),sum(sal) from emp group by deptno;
select  job,count(*),avg(sal) from emp group by job;
select  job,count(*),min(sal) from emp group by job;
select  job,count(*),max(sal) from emp group by job;
--having的用法
select job ,count(*),avg(sal) from emp group by job
having avg(sal)>2000;

--多表连接
select distinct e2.ename 上司的名字 from emp e1,emp e2
where e1.mgr=e2.mgr;
1  查询 经理人的工资
2  查询 津贴比工资高的员工
3  查询 和当前月份和入职月份相同的员工的工资
4  查询 入职年份 是1982年的员工
5  查询 每个部门的平均工资同时按照部门编号进行排序
6  查询 每个工种(job)的最低工资同时按照工种进行排序、
7  查询员工表(emp) 按照入职日期进行desc排序
8  查询 大于ALLEN这名员工的工资   


1 select sal from emp where job='MANAGER';
  select sal from emp where job=upper('manager');
2 select ename from emp where comm>sal;

3 select ename,sal from emp where to_char(sysdate,'MM')=to_char(HIREDATE,'MM');
  select ename,sal from emp where to_char(sysdate,'dd')=to_char(HIREDATE,'dd');
4 select ename,empno from emp where to_char(HIREDATE,'yyyy')='1982';
5 select deptno,avg(sal) from emp group by deptno ;
6 select job ,avg(sal) from emp group by job;
7 select * from emp order by hiredate desc;
8 select sal from emp where ename=upper('allen');
  select sal from emp where ename='ALLEN';
  select ename,sal from emp where sal>(select sal from emp where ename='ALLEN');

 

小雪是友达乳业有限公司的DBA ,由于刚入职所以很多工作还不熟悉,现在公司需要一些数据 请你帮帮她。公司采用的是oracle数据库

  1. 创建小雪账号并且登录(小雪工号是531  密码123)
  2. 新建产品表
  3. 创建产品编号型序列
  4. 插入表数据
  5. 生成产品编号和生成日期视图
  6. 查询出过期的酸奶编号(保质期为7天)

数据表如下

 

产品编号

产品名称

质检员

生产日期

YD1000421

酸奶

510

2019年3月19日

YD1000422

酸奶

510

2019年3月19日

YD1000423

酸奶

510

2019年3月19日

YD1000424

酸奶

510

2019年3月19日

YD1000425

酸奶

510

2019年3月19日

YD1000426

酸奶

510

2019年3月19日

YD1000427

酸奶

499

2019年3月20日

YD1000428

酸奶

499

2019年3月20日

YD1000429

酸奶

499

2019年3月20日

YD1000430

酸奶

499

2019年3月20日

YD1000431

酸奶

499

2019年3月21日

YD1000432

酸奶

499

2019年3月21日

YD1000433

酸奶

499

2019年3月21日

YD1000434

酸奶

499

2019年3月21日

YD1000435

酸奶

499

2019年3月21日

YD1000436

酸奶

510

2019年3月22日

YD1000437

酸奶

510

2019年3月22日

YD1000438

酸奶

510

2019年3月22日

YD1000439

酸奶

510

2019年3月22日

YD1000440

酸奶

510

2019年3月22日

YD1000441

酸奶

510

2019年3月22日

YD1000442

酸奶

510

2019年3月23日

YD1000443

酸奶

510

2019年3月23日

YD1000444

酸奶

510

2019年3月23日

YD1000445

酸奶

510

2019年3月23日

YD1000446

酸奶

510

2019年3月23日

YD1000447

酸奶

510

2019年3月23日

YD1000448

酸奶

499

2019年3月24日

YD1000449

酸奶

499

2019年3月24日

YD1000450

酸奶

499

2019年3月24日

YD1000451

酸奶

499

2019年3月24日

 

 

用户名:system
密码123456
create user wangmin identified by wangmin123456;
grant create session to wangmin;
grant create table to wangmin;
grant resource to wangmin;
grant connect to wangmin;
conn wangmin/wangmin123456;




--创建student表--学号--姓名--电话号码--年龄--生日--邮箱
--length,求字段长度
create table student        
(
id number(8)primary key,        
name varchar2(20) not null,        
tel number(11) unique check(length(tel)=11),        
age number(3) check(age<150),            
both date,            
email varchar2(20) not null        
);

--删除表
drop table student;

--逐条插入
insert into student values        
(18424186,'旭辉',18888888888,18,'01-1月-01','[email protected]');
insert into student values 
(18424846,'家乐','13777777777',101,'01-1月-20','[email protected]');
insert into student values 
(1842424,'静静','13888888888',17,'02-2月-02','[email protected]');
insert into student values 
(1842425,'汪敏','18288888888',20,'01-2月-02','[email protected]');


--查询表
select * from student;             



--删除单行数据
delete from student where id=18424186;            
delete from student where id=18424846;
delete from student where id=1842424;
delete from student where id=1842425;



--更改表里面电话的类型
alter table student modify tel char(11);        

--测试非空
insert into student (id,tel)values
(188888,13222222222);


create table library        
(
bookID  number(4) primary key,        
bookName varchar2(40) not null,        
author varchar2(10) ,        
price number(9,2) check(price<100),            
publishDate date,            
ISBN varchar2(20) UNIQUE        
);

用户名:system
密码123456
create user wangmin identified by wangmin123456;
grant create session to wangmin;
grant create table to wangmin;
grant resource to wangmin;
grant connect to wangmin;
conn wangmin/wangmin123456;




--创建student表--学号--姓名--电话号码--年龄--生日--邮箱
--length,求字段长度
create table student        
(
id number(8)primary key,        
name varchar2(20) not null,        
tel number(11) unique check(length(tel)=11),        
age number(3) check(age<150),            
both date,            
email varchar2(20) not null        
);

--删除表
drop table student;

--逐条插入
insert into student values        
(18424186,'苏旭辉',18888888888,18,'01-1月-01','[email protected]');
insert into student values 
(18424846,'汪家乐','13777777777',101,'01-1月-20','[email protected]');
insert into student values 
(1842424,'静静','13888888888',17,'02-2月-02','[email protected]');
insert into student values 
(1842425,'汪敏','18288888888',20,'01-2月-02','[email protected]');


--查询表
select * from student;             



--删除单行数据
delete from student where id=18424186;            
delete from student where id=18424846;
delete from student where id=1842424;
delete from student where id=1842425;



--更改表里面电话的类型
alter table student modify tel char(11);        

--测试非空
insert into student (id,tel)values
(188888,13222222222);

--事物处理(函数)
max
min
avg
用Scott登录
select max (sal) 最大值 from emp;
select min (sal) 最小值 from emp;
select avg (sal) 平均值 from emp;
怎么求那些人的工资高于平均值?
select ename,sal from emp;

select ename from emp where(sal>(select min(sal)from emp));
正负一指标法如果他工资低于平均工资那么这个人的指标为-1如果他的工资低于平均工资
那么他的指标为1
select ename 姓名 ,sign(sal-(select avg(sal) from emp)) 指标 from emp;

sum 求和
select sum (sal) from emp;

desc xxx;表结构


create table vegetable        
(
id number(3)primary key,        
bc number(3,2) not null,        
td number(3,2) not null,        
qz number(3,2) not null,        
dj number(3,2) not null,        
da date        
);

insert into vegetable
select 001,0.35,0.5,0.5,1.2,'10-11月-17'from dual union
select 002,0.4,0.6,0.5,1.3,'11-11月-17'from dual union
select 003,0.3,0.5,0.6,1.3,'12-11月-17'from dual union
select 004,0.5,0.5,0.5,1.35,'13-11月-17'from dual union
select 005,0.45,0.6,0.6,1.4,'14-11月-17'from dual union
select 006,0.45,0.5,0.55,1.2,'15-11月-17'from dual union
select 007,0.5,0.6,0.5,1.25,'16-11月-17'from dual;


事务处理 (函数)

 max 最大值
 min 最小值
 avg 平均值
 重新打开SQLPLUS  
 用户名 : scott
 口令 :tiger
 
 select max(sal) 最大值 from emp ;
 select min(sal) 最小值 from emp;

 select avg(sal) 平均值 from emp;
 
 下面一个问题 怎么求哪些人的工资高于平均值?
 select ename,sal from emp;
 select ename from emp where (sal>(select min(sal) from emp));
 正负一指标法 如果他的工资低于平均工资,那么这个人的指标为-1 如果他的工资高于平均工资,那么这个人的指标为1
 select ename 姓名, sign(sal-(select avg(sal) from emp)) 指标  from emp;

sum 求和 

select sum(sal) from emp;



事务处理 (函数)

 max 最大值
 min 最小值
 avg 平均值
 重新打开SQLPLUS  
 用户名 : scott
 口令 :tiger
 
 select max(sal) 最大值 from emp ;
 select min(sal) 最小值 from emp;

 select avg(sal) 平均值 from emp;
 
 下面一个问题 怎么求哪些人的工资高于平均值?
 select ename,sal from emp;
 select ename from emp where (sal>(select min(sal) from emp));
 正负一指标法 如果他的工资低于平均工资,那么这个人的指标为-1 如果他的工资高于平均工资,那么这个人的指标为1
 select ename 姓名, sign(sal-(select avg(sal) from emp)) 指标  from emp;

sum 求和 

select sum(sal) from emp;

 

日期函数

select sysdate from dual;

select to_char(sysdate,'YYYY-MM-DD') from dual;

select to_char(sysdate,'YYYY-MM-DD HH:MI:SS') from dual;

select '今天是开心的日子,时间为'||to_char(sysdate,'YYYY-MM-DD HH:MI:SS') from dual;
-- 我活了多少月
select months_between(sysdate,'20-8月-90') from dual;
select last_day(sysdate) from dual;
select next_day(sysdate,'星期三') from dual;
--时间戳
--常用的处理数据的内容
select count(*) from emp where  sal>=3000;
select count(*) from emp where sal>2000;
select count(*) from emp where sal>=3000 and sal>2000;

select count(*) from emp where sal<=3000 and sal>2000;

select count(*) from emp where sal in(2000,3000) ;

select count(*) from emp where sal not in(2000,3000);

 

标签:11,题目,函数,sal,0.5,查询,emp,dual,select
From: https://www.cnblogs.com/bky-wang/p/18125044

相关文章

  • 多表查询
    一语句查询1, 在SCOTT模式下,检索emp表的指定列(empno,ename,job)2, 检索emp表的sal列,把其值调整为原来的1.53, 在emp表中,使用like关键字匹配以字母s开头的员工名称4, 查询emp表中没有奖金的员工信息5, 在emp表中,查询既不是最高工资,也不是最低工资的员工信息6, ......
  • 查询
    用户名:scott密码:tiger三种方法可以打开sqlplus1win+R打开窗口输入sqlplus2快捷方式3在oracle主菜单下打开select*fromemp;select*fromdept;select1+1fromdual;select99*99fromdual;select99*99fromemp;selectename姓名......
  • 查询下属
    win+R键sqlplus1用户名:scott密码:tigerselect*fromemp;select*fromdept;selectename,sal,commfromemp;selectename,sal+nvl(comm,0)fromemp;selectename,12*(sal+nvl(comm,0))年薪fromemp;1selecte......
  • 查询表
    --1切换到master数据库中usemastergo--2判断cpms数据库是否存在,若存在则删除ifexists(select*fromsys.sysdatabaseswherename='cpms')dropdatabasecpmsgo--3创建cpms数据库createdatabasecpmson(name=cpms_data,filename='c:\cpms\cpms_data.mdf')log......
  • 关于查询优化的一些总结
    一、程序优化热点数据使用缓存数据库读写分离二、数据库方面的优化1、数据库设计优化如果单表数据量过大,可以根据业务来做分表数据库表可以做一些字段冗余,可以减少连表查询,提升查询效率2、Sql语句优化2.1.首先定位慢查询开启慢查询日志mysqlslow_query_log:是否开启慢查询sl......
  • MYSQL五个常见的聚合函数
    学生表DDLCREATETABLE`student`(`id`int(11)NOTNULLAUTO_INCREMENTCOMMENT'学号',`createDate`datetimeDEFAULTNULL,`userName`varchar(20)DEFAULTNULL,`pwd`varchar(36)DEFAULTNULL,`phone`varchar(11)DEFAULTNULL,`age`tinyi......
  • js代码的函数及应用
    昨天学习了单击事件及其应用,现在我们接触js的函数。什么是函数?函数是可以重复执行的代码,需要通过函数名称来执行代码。 自定义一个函数function功能名称(){代码}例题:   圆的面积   varr=5;varpi=3.14functionc......
  • 【数据结构 | 并查集】维护元素分组信息,支持高效合并集合、查询元素所在集合
    文章目录并查集概述引入并查集的实现存储方式Union-Find抽象基类两种实现思路基本实现基于QuickFind思路基于QuickUnion思路优化基于size的优化基于rank的优化find优化路径压缩路径分裂路径减半总结并查集概述并查集(DisjointSetUnion,简称并查集),也叫......
  • Django便捷函数shortcuts
    一、Django便捷函数1、介绍包django.shortcuts收集助手函数和“跨”多级mvc的类,换句话说,为了方便起见,这些函数/类引入受控耦合。fromdjango.shortcutsimportrender,HttpResponse,redirect,reverse,resolve_url2、官方链接https://docs.djangoproject.com/zh-hans/3......
  • C语言08-函数(递归、字符串、日期时间、数学计算函数),指针
    第11章函数11.7递归函数​ ——相当于俄罗斯套娃;一个程序未执行结束会挂起,相当于堆栈一个函数在函数体内又调用了本身,我们称为递归调用,这样的函数就是递归函数。递归函数成功执行需满足以下两个条件:(1)必须有一个明显的结束条件。(2)必须有一个趋近于结束条件的趋势......