Oracle配套练习
练习一
3: SELECT last_name, job_id, salary AS Sal
FROM employees;
能够执行成功
4:SELECT * FROM job_grades; 不能够,From后面只能跟表或者视图
5:SELECT employee_id, last_name 缺少 ,
sal x 12 ANNUAL SALARY sal值不存在 X没有意义错误表达
FROM employees;
6:
7:将sql语句保存到 labl_7.sql文件中
8
9:创建唯一查询 select distinct job_id from employees;
10:将显示职务与姓氏,用用逗号和空格分隔开 然后命名为“employee and title”
select last_name||', '||job_id As "Employee and Title" from employees;
11:创建一个查询使其显示employees表所有的数据用户‘,’分割开 命名为“THe——Output”
练习二
1:
2: 创建一个查询,显示员工编号为176的员工的姓氏和部门编号
select Last_name,Department_id from employees
where employee_id =176;
3:
4: 显示在2005年2月20日和2008年5月1日之间聘用的元的姓氏、职务、和起始日期
select last_name , to_char(hire_date,'fmYYYY-Month-DD') from employees
where hire_date between '20-February-05' and '1-May-08'
5: 按姓名顺序显示部门20和部门50中所有员工的姓氏和编号
select department_id,last_name from employees
where department_id=20 or department_id=50
order by last_name asc;
6: 显示1994年聘用的员工的姓氏和聘用年限
select last_name,hire_date from employees
where hire_date like '%94%';
7:显示没有经理的的所有元的姓氏和职称
select last_name ,job_id from employees
where manager_id is null;
8: 显示可以赚取佣金的所有员工的姓氏、薪金和佣金。按薪金和佣金进行降序排序
select last_name,salary,commission_pct
from employees
where commission_pct is not null
order by salary desc ,commission_pct desc;
9: 显示员工姓名中的第三个字母为“a“的所有员工的姓氏
select last_name from employees
where last_name like '__a%';
10: 显示员工姓氏中有"a" 和 "e"的所有员工的姓氏
select last_name from employees
where last_name like '%a%' or last_name like '%e%';
11: 显示职务为销售代表或仓库管理员并且薪金不低于¥2500、¥3500或¥7000的所有员工的姓氏、薪金、和职务
select last_name ,salary, job_id from employees e,jobs j
where e.job_id=j.job_id and
j.job_title like '%Manager%'
and e.salary >= 2500
order by salary desc;
练习3
1: 显示当前日期,并标记为Date
select to_char(current_date,'fmYYYY-MM-DD')date1 from dual;
2: 显示每位员工的编号,姓氏,薪金和增加15%后的薪金总数并标记为New Salary
select employee_id,last_name,salary,salary*1.15 as "New Salary" from employees
4: 在第二题的基础上添加一个新薪金减去原来的薪金并标记为Increase
select employee_id,last_name,salary,salary*1.15,
nvl(salary*1.15-salary,0)Increase from employees;
5: 显示姓名一以J,S或M开头的所有员工的姓氏(第一字母大写,所有其它字母小写)和姓名长度,给每一列一个合适的标签,按员工的姓氏升序排序
select last_name as "Name",length(last_name)Length from employees
where last_name like 'A%' or last_name like 'J%' or last_name like 'M%';
6: 计算今天和该员工的聘用月数标记为Month_Worked ,月份取舍
select last_name ,round(months_between(current_date,hire_date),0)Month_Worded from employees;
7: <employee last name> earns <salary> monthly but wants <3 times salary>
select last_name||'e arns '||salary||' monthly '||' but wants '||salary*3 as "Dream Salaries"
from employees;
8: 薪金格式规定为15个字符长度,左边填充¥标记为salary
select last_name ,lpad(salary,15,'$'') from
employees;
9: 增加一个薪金复核日 为服务六个月后的第一个星期一标记为Review,使其显示格式为“Monday,the Thirty-Fist of July,2000”。
select LAST_NAME,TO_char(NEXT_DAY(ADD_MONTHS(HIRE_DATE,6), 'Monday'),'fmDay,"the of" Month,YYYY')New_date from EMPLOYEES;
10: 员工的工作日期(星期几)标记为Day 排序
select last_name ,hire_date,to_char(hire.date,'fmDay')DAY from employees
order by DAY asc;
将星期变为数字
11:显示员工姓氏与佣金金额,如果该员工不赚取佣金,则显示“NO Commission”并标记为COMM
select last_name,nvl(LPAD(to_char(COMMISSION_PCT), 5),'No Commission')COMM from employees ORDER BY COMM asc;
12: 用*号指明年薪,没一*代表一千美元按薪金降序排序并标记为Employees_And_Their_Salary.
select last_name||' '||TO_CHAR(lpad(0,to_number(salary/1000),'*'))Employees_And_Their_Salaries
from employees ORDER BY SALARY DESC;
13: 按职务给等级AD_PRES A
ST_MAN B
IT_PROG C
SA_REP D
ST_CLERK E
select job_id ,DECODE(job_id, 'AD_PRES','A','ST_MAN','B',
'IT_PROG','C','SA_REP','D','ST_CLERK','E',
0)Grade
from EMPLOYEES ORDER BY Grade asc;
select job_id , CASE job_id
WHEN 'AD_PRES' THEN 'A'ELSE'0'END "Grade"
from employees;
练习四
1: 显示员工姓氏、部门编号、部门名称
select e.last_name,e.department_id,d.department_name
from employees e,departments d
where e.department_id=d.department_id;
2: 创建部门80中所有职务唯一的
SELECT e.JOB_ID,d.LOCATION_ID
FROM employees e ,departments d
WHERE e.DEPARTMENT_ID=d.DEPARTMENT_ID
SELECT JOB_ID ,COUNT(*)Number1
FROM EMPLOYEES GROUP BY JOB_ID
3: 显示姓氏、部门名称、部门地点、以及城市
select e.first_name,e.department_name,d.location_id,l.city
from employees e,departments d,locations l
where e.department_id=d.department_id and
d.location_id=l.location_id;
4: 含小写a名称的员工的姓氏和部门名称
select e.last_name ,d.department_name
from employees e
join departments d
on (e.department_id=d.department_id)
where e.last_name like '%a%' ;
5: 在多伦多Toronto 的员工姓氏、职务、部门编号、部门名称
select e.last_name,j.job_title,e.department_id,d.department_name,l.city
from employees e
join departments d
on (e.department_id=d.department_id)
join jobs j
on (e.job_id =j.job_id)
join locations l
on (d.location_id=l.location_id)
where lower(l.city) ='toronto';
8: 部门id,部门员工,同意部门的所有员工
select e1.department_id,e1.last_name,e2.last_name
from employees e1
right outer join employees e2
on (e2.department_id=e1.department_id )
and e2.last_name !=e1.LAST_NAME
ORDER BY e1.DEPARTMENT_ID asc, e1.LAST_NAME
9: 查询工资等级
非等值联结 利用where between条件联结
SELECT e.last_name, e.salary, j.grade_level
FROM employees e, job_grades j
WHERE e.salary
BETWEEN j.lowest_sal AND j.highest_sal;
利用非等值联结进行查询
10: 显示Davies之后聘用的所有员工的姓名和聘用日期
select e1.last_name ,e1.hire_date
from employees e1
join employees e2
on (e2.last_name='Davies')
where e1.hire_date > e2.hire_date
ORDER BY e1.HIRE_DATE;
11: 在其经理前聘用的员工信息
select e.last_name,e.hire_date,
m.last_name,m.hire_date
from employees e
LEFT join employees m
On (m.employee_id=e.manager_id)
where e.HIRE_DATE < m.HIRE_DATE