目录
1.单行查询........................................................................................................................................ 2 2.基本查询........................................................................................................................................ 3 3.限定返回的行 ................................................................................................................................ 4 4.逻辑运算........................................................................................................................................ 5 5.排序 ............................................................................................................................................... 6 6.函数 ............................................................................................................................................... 6 7.分组查询........................................................................................................................................ 9 8.多表查询...................................................................................................................................... 10 9.多表连接...................................................................................................................................... 13 10.子查询 ....................................................................................................................................... 14 11.创建和管理表............................................................................................................................ 20 12.约束 ........................................................................................................................................... 23 13.视图,序列,索引 .................................................................................................................... 25 14.其他数据库对象 ........................................................................................................................ 26 15.pl sql基础 .................................................................................................................................. 28
1.单行查询
--查询所有员工的姓 select e.last_name as 姓 from employees e
--消除重复的姓
select distinct e.last_name as 姓氏 from employees e
--计算员工的月收入(工资+佣金)
select salary,salary*(nvl(commission_pct,0)+1) as 工资佣金 from employees
--计算员工的年收入
select salary*(nvl(commission_pct,0)+1)*12 as 年收入 from employees
--查询员工的姓名
select e.first_name||' '||e.last_name rom employees e
--查询位置为1700的部门名称(不重复) select distinct d.department_name as 部门名称 from departments d
where d.location_id=1700
--查询工资高于10000的员工 select *
from employees where salary>10000
--查询工资低于3000的员工 select *
from employees where salary<3000
--查询在1998年入职的员工 select *
from employees e
where to_char(e.hire_date,'yyyy')='1998'
--查询没有佣金的员工
select *
from employees e
where commission_pct is null
--查询姓以B开头的员工 select *
from employees e
where e.last_name like 'B%'
--查询部门号为10或者20或者30的员工 select *
from employees e
where e.department_id in(10,20,30)
2.基本查询
--查询所有员工的姓 select last_name from employees;
--消除重复的姓
select distinct last_name from employees;
--创建一个查询,以显示employees表中的唯一职务代码 select distinct t.job_id from employees t;
--创建一个查询,使其显示每位员工的姓氏、职务代码、聘用日期和员工编号,并且首先显示员工编号。为hire_date列提供一个别名:startdate
select t.employee_id,t.last_name,t.job_id,t.hire_date as startdate from employees t;
--计算员工的月收入(工资+佣金)
select t.salary+t.salary*nvl(t.commission_pct,0) from employees t; --计算员工的年收入
select 12*(t.salary+t.salary*nvl(t.commission_pct,0)) from employees t; --查询员工的姓名
select t.first_name||' '||t.last_name from employees t;
--显示与职务标识连接的姓氏,它们之间由逗号和空格分隔,这列数据命名为 Employee and Title
select t.job_id||', '||t.last_name as rom employees t;
--创建一个查询,使其显示employees表的所有数据,用逗号分隔各列,命名列为THE_OUTPUT select t.employee_id||','||t.first_name||','||t.last_name||','|| t.email||','||t.phone_number||','||t.hire_date||','||t.job_id||','||
t.salary||','||t.commission_pct||','||t.manager_id||','||t.department_id as rom employees t;
============================================================================
3.限定返回的行
--查询位置为1700的部门名称(不重复) select distinct t.department_name from departments t
where t.location_id=1700;
--创建一个查询,显示员工编号为176的员工的姓氏和部门编号 select t.last_name,t.department_id from employees t
where t.employee_id=176;
--查询工资高于10000的员工的姓氏和薪资 select t.last_name,t.salary from employees t where t.salary>10000;
--查询工资低于3000的员工 select t.*
from employees t where t.salary<3000;
--查询在1998年2月20日和1998年5月1日之间入职的员工的姓氏、职务标识和起始日期
select t.last_name,t.job_id,t.hire_date from employees t where t.hire_date between to_date('19980220','yyyyMMdd') and to_date('19980501','yyyyMMdd');
--显示在1994年聘用的每位员工的姓氏和聘用日期 select t.last_name,t.hire_date from employees t where t.hire_date between to_date('19940101','yyyyMMdd') to_date('19950101','yyyyMMdd'); --*查询没有佣金的员工 select t.*
from employees t
and
where t.commission_pct is null; --查询姓以B开头的员工 select t.*
from employees t
where t.last_name like 'B%';
--查询部门号为10或者20或者30的员工 select t.*
from employees t
where t.department_id in (10,20,30); --查询没有经理的所有员工的姓氏和职称 select t.last_name,t.job_id from employees t
where t.manager_id is null;
--显示员工名字中的第三个字母为“a”的所有员工的姓氏 select t.last_name from employees t
where t.last_name like '__a%';
4.逻辑运算
--找出部门10中所有的经理(MANAGER)和部门20中所有办事员(**_CLERK) (需用子查询,暂不做)
--找出有佣金的员工的都做什么工作(无重复) select distinct t.job_id from employees t
where t.commission_pct is not null;
--找出不收取佣金或收取的佣金高于100的员工 select *
from employees t
where t.commission_pct is null or t.salary*t.commission_pct>100;
--找出部门10中所有的经理(MANAGER)和部门20中所有办事员(CLERK)和既不是经理又不是办事员但其薪金大于或等于2000的所有员工的详细资料(需用子查询,暂不做) --显示员工姓氏中有“a”和“e”的所有员工的姓氏 select *
from employees t
where t.last_name like '%a%' or t.last_name like '%e%' --显示职务为销售代表(SA_REP)或仓库管理员(ST_CLERK)并且薪金不等于2500,3500,7000的所有员工的姓氏、职务和薪金 select t.last_name,t.job_id,t.salary from employees t
where (t.job_id='SA_REP' or t.job_id='ST_CLERK') and t.salary not in (2500,3500,7000);