## Oracle 常用函数练习题含答案

SELECT *

FROM employees

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

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

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;

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

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

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

FROM employees

（提示：使用initcaplengthsubstr

SELECT  first_name

FROM   employees

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

select first_name

from employees

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

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位：编号,不足部分用*填充,左对齐

FROM employees

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

FROM  employees

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

(提示：使用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';

SELECT employees.first_name,employees.hire_date

FROM employees

WHERE employees.first_name !='MANAGER';

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

from dual;

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

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

FROM employees

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

FROM employees

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.在员工表中查询出员工的工资，并计算应交税款：

SELECT employees.first_name,

employees.salary,

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;

0条评论