Oracle 常用函数练习题含答案

查询员工ENAME的第三个字母是A的员工的信息(使用2个函数)

SELECT *

FROM employees

where upper(employees.first_name || employees.last_name) like '__A%';

 

将员工的参加工作日期按如下格式显示:月份/年份

SELECT to_char(employees.hire_date,'mm/YYYY') FROM employees;

 

计算200011日到现在有多少月,多少周(四舍五入)

SELECT round(months_between(sysdate,'1-Jan-2000'),2) as "",

round((sysdate-to_date('2000-01-01','yyyy-mm-dd'))/7,2) as ""

FROM dual;

 

选择所有有奖金的员工的last_name , department_name , location_id , city

SELECT employees.last_name,departments.department_name,locations.location_id,locations.city

FROM employees,departments,locations

where employees.department_id = departments.department_id and

departments.location_id = locations.location_id

 

选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号

SELECT e.last_name,e.employee_id,m.last_name,m.employee_id

FROM employees e,employees m

where e.manager_id = m.manager_id

 

显示姓名、hiredate?和雇员开始工作日是星期几(提示:使用to_char函数)

SELECT employees.first_name||employees.last_name as "name",

employees.hire_date,to_char(hire_date,'day')

FROM employees

 

写一个查询,用首字母大写,其它字母小写显示雇员的 全名,显示名字的长度,并给每列一个适当的标签,条件是满足所有雇员名字的开始字母是JA M 的雇员,并对查询结果按雇员的全名升序排序。

(提示:使用initcaplengthsubstr

SELECT  first_name

FROM   employees

WHERE  first_name LIKE 'J%'or first_name LIKE 'A%'or first_name LIKE 'M%'

 

查询员工姓名中中包含大写或小写字母A的员工姓名。

select first_name

from employees

where first_name like '%A%' or first_name like '%a%'

 

查询部门编号为1020,入职日期在8151日之后,并且姓名中包含大写字母A的员工姓名,

员工姓名长度(提示,要求使用INSTR函数,不能使用like进行判断)

select employees.first_name,departments.department_id,employees.hire_date,

        length(employees.first_name)

from employees,departments

WHERE (departments.department_id = 10 or departments.department_id = 20)

and to_char(employees.hire_date,'yyyy-mm-dd') > '1-5-81'

--and employees.hire_date >'1-5-81'

AND instr(employees.first_name,'A') > 0

 

查询每个职工的编号,姓名,工资要求将查询到的数据按照要求将查询到的数据按照定的格式合并成一个字符串.

–?10位:编号,不足部分用*填充,左对齐

SELECT LPAD(employees.employee_id,10,'*'),employees.first_name,employees.commission_pct

FROM employees

 

–?中间10位:姓名,不足部分用*填充,左对齐

SELECT employees.employee_id, LPAD(employees.first_name,10,'*'),employees.commission_pct

FROM  employees

 

10位:工资,不足部分用*填充,右对齐

SELECT employees.employee_id,employees.first_name,RPAD(employees.commission_pct,10,'*')

FROM employees

 

四舍五入

select ROUND(100.456,2),round(100.456,1),round(100.456,0)

from dual

 

没有四舍五入 截断

select trunc(100.456,2),trunc(100.456,1),trunc(100.456,0)

from dual

 

查询员工一共入职多少天,当前时间-入职

select round(sysdate-employees.hire_date,2)

from employees

 

查询服务器当前时间

SELECT sysdate

FROM dual

 

查询部门10 20 的员工截止到200011日,工作了多少个月,入职的月份。

(提示:使用months_between,extract)

SELECT employees.first_name,departments.department_id, employees.hire_date,

       extract(month from employees.hire_date)as ,

       --months_between(to_date(employees.hire_date,'yyyy-mm-dd'),'1-1-2000')

       months_between(employees.hire_date,'1-Jan-2000')

FROM employees,departments

WHERE departments.department_id in ('10','20');

 

select employees.hire_date

from employees      

WHERE employees.hire_date = '17-JUN-87';

 

如果员工试用期6个月,查询职位不是MANAGER的员工姓名,入职日期,入职日期后的第一个星期一,

入职当月的最后一天日期。(提示:使用add_months,next_day,last_day)

SELECT employees.first_name,employees.hire_date

FROM employees

WHERE employees.first_name !='MANAGER';

 

显示服务器系统当前时间,格式为2007-10-12 17:11:11(提示:使用 to_char函数)

select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss')

from dual;

 

--SELECT to_char(sysdate,'yyyy-mm-dd hh:mm:ss') FROM dual;

 

显示姓名、hiredate?和雇员开始工作日是星期几(提示:使用to_char函数) ***

SELECT employees.first_name,to_char(employees.hire_date,'day')

FROM employees

 

查询员工姓名,工资,格式化的工资(¥999,999.99(提示:使用to_char函数)

SELECT employees.first_name,to_char(employees.salary,'$999,999.99')

FROM employees

 

把字符串2015-3-18 13:13:13?转换成日期格式?转换成日期格式,并计算

和系统当前时间间隔多少天。?(提示:使用to_date函数)

select round(sysdate-(to_date('2015-03-18 15:33:21','yyyy-MM-dd HH24:mi:ss')))

from dual;

--select to_date('2015-MAR-18 13:13:13','yyyy-mm-dd')

--from dual;

 

1.计算200011日到现在有多少月,多少周(四舍五入)。

SELECT round((MONTHS_BETWEEN(sysdate,'1-JAN-00')),2) as , round((sysdate - to_date('2000-01-01','yyyy-mm-dd'))/7,2) as week

FROM dual;

 

2.查询员工ENAME的第三个字母是A的员工的信息(使用2个函数)

SELECT *

FROM employees

where employees.first_name like '%A%';

 

select *

from employees

where substr(employees.first_name,3,1)='A';

 

3.使用trim函数将字符串‘hello’‘?Hello?’、分别处理得到下列字符串elloHellollhello

SELECT trim('H' from 'Hello') as A,

       trim('Hello') as B,

       /*trim(both'l' from 'hello') as C,*/

       substr(substr('hello',3,5),1,2),

       trim('hello') as D

  FROM dual

 

4.将员工工资按如下格式显示:123,234.00 RMB?

select to_char('123234.00','000999.00') || 'RMB'

from dual

 

SELECT to_char(employees.salary,'999,999.00') || 'RMB'

FROM employees;

 

5.查询员工的姓名及其经理编号,要求对于没有经理的显示No Manager”字符串。

select employees.first_name,nvl(to_char(employees.manager_id),'No Manager')

from employees

 

6.将员工的参加工作日期按如下格式显示:月份/年份。

SELECT employees.first_name,employees.hire_date,to_char(employees.hire_date,'hh/rr')

FROM employees

 

7.在员工表中查询出员工的工资,并计算应交税款:

如果工资小于1000,税率为0

如果工资大于等于1000并小于2000,税率为10%,

如果工资大于等于2000并小于3000,税率为15%,

如果工资大于等于3000,税率为20%。

SELECT employees.first_name,

       employees.salary,

       lpad(employees.salary, 15, '$'),

       case

         when employees.salary > 0 and employees.salary < 1000 then

          employees.salary * 0

         when employees.salary >= 1000 and employees.salary < 2000 then

          employees.salary * 0.1

         when employees.salary >= 2000 and employees.salary < 3000 then

          employees.salary * 0.15

         when employees.salary >= 3000 then

          employees.salary * 0.2

         else

          0

       end tax

  FROM employees;

 

8.创建一个查询显示所有雇员的?姓名和?sal。格式化sal?15?个字符长度,用?$左填充,列标签?SALARY

SELECT employees.first_name,lpad(employees.salary,15,'$') FROM employees

 

1. 组函数处理多行返回一行吗?

 

2. 组函数不计算空值吗?是  (组函数忽略空值计算)

 

3. where子句可否使用组函数进行过滤? 不能再where子句中使用组函数。

 

4. 查询公司员工工资的最大值,最小值,平均值,总和

select max(employees.salary),min(employees.salary),avg(employees.salary),sum(employees.salary)

from employees

 

5. 查询各job_id的员工工资的最大值,最小值,平均值,总和

SELECT employees.job_id,max(employees.salary),min(employees.salary),avg(employees.salary),

sum(employees.salary) FROM employees group by employees.job_id

 

6. 选择具有各个job_id的员工人数

SELECT employees.job_id,count(*) FROM employees group by employees.job_id

 

7. 查询员工最高工资和最低工资的差距(DIFFERENCE

SELECT MAX(employees.salary)-MIN(employees.salary)

FROM employees

 

8. 查询各个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内

SELECT manager_id, min(salary)

  FROM employees

 group by manager_id

having min(salary) >= 6000 and manager_id is not null;

 

9. 查询所有部门的名字,location_id,员工数量和工资平均值

SELECT departments.department_name,

       departments.location_id,

       count(*),

       avg(employees.salary)

  FROM departments, employees

  where departments.department_id = employees.department_id

  group by departments.department_name,departments.location_id

 

SELECT department_name,location_id,count(job_id) as "部门总人数",avg(salary)as"平均工资"

FROM departments,employees

where departments.department_id = employees.department_id

group by departments.department_name,departments.location_id

 

10  查询公司在1995-1998年之间,每年雇用的人数    to_char   count 

SELECT to_char(employees.hire_date,'yyyy'),count(*)

FROM employees

group by to_char(employees.hire_date,'yyyy')

having to_char(employees.hire_date,'yyyy') in ('1995','1996','1997','1998');

 

 

11. 查询公司在1995-1998年之间,每年雇用的人数   使用case when

total  1995   1996   1997   1998

20      3       4        6      7

 

SELECT count(employees.employee_id) "total",

count(decode(to_char(employees.hire_date,'yyyy'),'1995','0',null))"1995",

count(decode(to_char(employees.hire_date,'yyyy'),'1996','0',null))"1996",

count(decode(to_char(employees.hire_date,'yyyy'),'1997',0,null))"1997",

count(decode(to_char(employees.hire_date,'yyyy'),'1998',0,null))"1998"

FROM employees

where to_char(hire_date,'yyyy')

in ('1995','1996','1997','1998');

 

SELECT count(*)"total",

count (case when to_char(employees.hire_date,'yyyy')=1995 then '1995' else null end )"1995",

count (case when to_char(employees.hire_date,'yyyy')=1996 then '1996' else null end )"1996",

count (case when to_char(employees.hire_date,'yyyy')=1997 then '1997' else null end )"1997",

count (case when to_char(employees.hire_date,'yyyy')=1998 then '1998' else null end )"1998"

FROM employees;

猜你喜欢

转载自blog.csdn.net/CQL_K21/article/details/88358729
0条评论
添加一条新回复