Oracle-数据库所有查询命令.pdf
《Oracle-数据库所有查询命令.pdf》由会员分享,可在线阅读,更多相关《Oracle-数据库所有查询命令.pdf(33页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、目录1.单行查询.22.基本查询.33.限定返回的行.44.逻辑运算.55.排序.66.函数.77.分组查询.98.多表查询.109.多表连接.1310.子查询.1411.创建和管理表.1912.约束.2213.视图、序列、索引.2414.其他数据库对象.2615.plsql 基础.281.单行查询-查询所有员工的姓select e.last_name as 姓 from employees e;-消除重复的姓select distinct e.last_name as 姓氏 from employees e;-计算员工的月收入(工资+佣金)select salary/salary*(nvl(
2、commission_pct/0)+l)as 工资佣金 from employees;-计算员工的年收入select salary*(nvl(commission_pct,0)+l)*12 as 年收入 from employees;-查询员工的姓名select e.first_name11|e.last_name name from employees e;-查询位置为1700的部门名称(不重复)select distinct d.department_name as 部门名称 from departments dwhere d.locationJd=1700;-查询工资高于10000的员工
3、select*from employees where salary10000;-查询工资低于3000的员工select*from employees where salary10000;-查询工资低于3000的员工select t.*from employees twhere t.salary100;-找出部门1 0 中所有的经理(manager)和部门2 0 中所有办事员(clerk)和既不是经理又不是办事员但其薪金大于或等于2000的所有员工的详细资料(需用子查询,暂不做)-显示员工姓氏中有a和e的所有员工的姓氏select*from employees twhere t.last_na
4、me like%a%ort.last_name like%e%-显示职务为销售代表(saep)或仓库管理员(st_derk)并且薪金不等于2500,3500,7000的所有员工的姓氏、职务和薪金select t.last_name,t.jobjd,t.salary from employees twhere(t.jobjd=sa_rep or t.jobjd=st_clerk,)and t.salary not in(2500,3500,7000);-显示薪金不在5000-1200这个范围之间的所有员工的姓氏和薪金select t.last_name,t.salary from employe
5、es twhere t.salary1200;5.排序-查询员工资料,按薪资升序排列select t.*from employees torder by t.salary;-查询员工资料,在部门号从大到小的情况下按按薪资升序排列select t.*from employees torder by t.departmentjd desc.salary;-按姓名的字母顺序显示部门20和部门50中的所有员工的姓氏和部门编号select t.last_name,t.departmentjd from employees twhere t.departmentjd in(20,50)order by t
6、.last_name;-显示可以赚取佣金的所有员工的姓氏、薪金和佣金,按薪金和佣金的降序对数据进行排序select t.last_name,t.salary,mission_pct from employees twhere mission_pct is not nullorder by t.salary mission_pct desc;6.函数-显示当前日期select sysdate from dual;-显示当前日期,格式为*年*月*日,别名为hdayselect to_char(sysdate;yyyymm dd 0)hday from dual;编写一个查询,显示姓名以j、a 或
7、m 开始的所有员工的姓氏(第一个字母大写,其余字母小写)和姓氏的长度,给每列一个合适的标签select initcap(t.last_name)lname,length(t.last_name)len from employees twhere substr(upper(last_name),O,l)in(j7a,m);-计算每位员工截止到当前时间入职的星期数,别名为weeks_worked按聘用的星期数对结果进行排序。该星期数舍入到最接近的整数。同时显示员工的名字;elect t.first_namezround(sysdate-t.hire_date)/7)as weeks_worked
8、from employees t;-计算每位员工截止到当前时间入职的月数,别名为months_worked。该星期数舍入到最接近的整数。同时显示员工的名字select t.first_name,round(months_between(sysdate,t.hire_date)as months_workedfrom employees t;-查询在1998-2-20和 1998-5-1之间入职的员工的姓氏、职务标识和起始日期select t.last_name,t.job_id/t.hire_date from employees twhere t.hire_date between to_d
9、ate(199802207yyyymmdd)and to_date(199805017yyyymmdd);创建一个查询。显示所有员工的姓氏和薪金。规定薪金为15个字符长,左边填充$select lpad(t.salary,15,$,)sal from employees t;-显示每位员工的姓氏、聘用日期和薪金复核日期,薪金复核日期是服务六个月之后的第一个星期一。将该列标记为review,此日期格式类似于:monday,the thirty-first of july,2000select t.last_name,t.hire_date,next_day(add_months(t.h汗 e_d
10、ate,6),2)from employees tselect t.last_name/t.hire_date,to_char(next_day(add_months(t.hire_datez6),2)/fmday/the ddspth of monthyyyy,nls_date_language=english)reviewfrom employees t;注:to_char的第三个参数用于设置查询使用的国家和地区,ddspth中 spth为后缀,表示spelled,ordinal number-显示员工的姓氏、聘用日期和该员工在星期几开始工作的select t.last_namezt.hi
11、re_date,to_char(t.hire_date/day)as 开始 from employees t;-计算员工的月收入(工资+佣金)select t.salary+t.salary*nvl(mission_pct/0)from employees t;-创建一个查询,使其显示员工的姓氏,并用星号指明他们的年薪。每个星号代表一千美元,按薪资降序排列数据。select t.last_name|lpad(,/trunc(t.salary/1000)+l,*)/t.salary from employees torder by t.salary desc;-创建一个查询。使其显示员工的姓氏和
12、佣金额。如果某位员工不赚取佣金则显示nocommission”,将该列标记为commselect t.last_name,nvl(to_char(t.salary*mission_pct/999/999.99)/no commission)commfrom employees t-使 用 decode函数编写一个查询,使其按照以下数据根据jo b jd 列的值显示所有员工的级别,同时显示员工的姓氏job grade ad_pres a st_man b it_prog c sa_rep d st_clerk e 其它 0select t.last_name,t.jobjd job,decode
13、.jobjd/ad.pres/a/sCman/b;it_prog;c/sa.rep/d/st.clerk/e;0)grade from employees t;select t.last_name,t.job_id job,case t.jobjdwhen ad_pres then awhen st_man then b*when it_prog then cwhen sa_rep thendwhen st_clerk then*eelse Oend as grade from employees t;-显示当前日期,本月最后一天的日期,以及本月还剩多少天select sysdate,last
14、_day(sysdate)last,last_day(sysdate)-sysdate days left from dual;-显示今年的第一天select trunc(sysdatejyear)from dual;-显示本月的第一天select trunc(sysdate/month)from dual;-最近一个星期四是哪天(不含今日)select next_day(sysdatez5)from dual;7.分组查询-求所有员工的平均工资、最高工资、最低工资和工资总和,给予适当的别名select avg(t.salary),max(t.salary),min(t.salary),sum
15、(t.salary)from employees t;-求每种工作的平均工资select avg(t.salary)from employees tgroup by t.jobjd;.求每个部门中同一种工作的平均工资,同时显示部门号,按部门号升序显示select t.department_id,avg(t.salary)from employees tgroup by t.departmentJd,t.job_idorder by t.departmentjd;-查询出各部门的部门编号以及各部门的总工资和平均工资,按部门编号升序排列。select t.departmentJdumft.sala
16、ryJvgft.salary)from employees tgroup by t.departmentjdorder by t.departmentjd;-显示每种工作的人数select t.job_id,count(*)from employees tgroup by t.jobjd;-显示员工最高工资超过10000的部门的id及其员工最高工资select t.department_id,max(t.salary)m from employees tgroup by t.departmentjdhaving max(t.salary)10000;-显示平均工资最高的部门id 及其平均工资s
17、elect*from(select t.departmentjd,avg(t.salary)from employees tgroup by t.departmentjdorder by avg(t.salary)desc)where rownum nonequijoins outerjoins self-joins crossjoins natural joins outerjoinsfull(or two-sided)-l.write a query for the hr department to produce the addresses of all the departments,
18、use thelocations and countries tables,show the location id,street address,city,state or province,andcountry in the output,use a natural join to produce the results.select lct.location_idjct.street_address,lct.state_province,cty.country_name from locations letnatural join countries cty;-2,the hr depa
19、rtment needs a report of all employees,write a query to display the last name,department number,and department name for all the employees.select emp.last_name,departmentJd,dpt.department_name from employees emp natural joindepartments dpt;-3.the hr department needs a report of employees in toronto.d
20、isplay the last name,job,department number,and the department name for all employees who work in toronto.select emp.last_name,emp.jobjd,dpt.department_id,dpt.department_name from employeesemp join departments dpt on emp.department_id=dpt.departmentJdjoin locations let on dpt.locationjd=lct.location_
21、idwhere lct.city=toronto;-4.create a report to display employees last name and employee number along with theirmanagers last name and manager number.label the columns employee,emp#,manager;and mgr#,respectively,save your sql statement aslab_06_04.sql.run the query.-自联结select emp.last_name employee,e
22、mp.employee_id emp#,mgr.last_name manager,mgr.employeejd mgr#from employees empjoin employees mgr on emp.manager_id=mgr.employee_id;-5.modify lab_06_04.sql to display all employees including king,who has no manager,order theresults by the employee number,save your sql statement as lab_06_05.sql.run
23、the query inlab_06_05.sql.select emp.last_name employee,emp.employeeJd emp#,mgr.last_name managecmgr.employeejd mgr#from employees empleft outer join employees mgr on emp.managerJd=mgr.employee_idorder by emp#;-6.create a report for the hr department that displays employee last names,departmentnumbe
24、rs,and all the employees who work in the same department as a given employee.giveeach column an appropriate label,save the script to a file named lab_06_06.sql.select emp.last_name employee,emp.departmentJd,colleague.Iast_name colleaguefrom employees empjoin employees colleague on emp.department_id=
25、colleague.departmentjdwhere emp.employeeJdocolleague.employeeJdorder by employee;-7.the hr department needs a report on job grades and salaries.to familiarize yourself with thejob_grades table,first show the structure of the job_grades table,then create a query thatdisplays the name job,department n
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Oracle 数据库 所有 查询 命令
限制150内