现在的位置: 首页 > 综合 > 正文

sql练习

2018年05月17日 ⁄ 综合 ⁄ 共 7968字 ⁄ 字号 评论关闭
--1 查询dept表的结构 
  desc dept;
  
--2、检索dept表中的所有列信息
   select * from dept

--3、检索emp表中的员工姓名、月收入及部门编号
   select ename,sal,deptno from emp

--4、检索emp表中员工姓名、及雇佣时间
     日期数据的默认显示格式为"DD-MM-YY",如果希望使用其他显示格式	
        	(YYYY-MM-DD),那么必须使用TO_CHAR函数进行转换。
     select ename,to_char(hiredate,'yyyy-mm-dd') from emp
          

5、使用distinct去掉重复行。
     检索emp表中的部门编号及工种,并去掉重复行。
     select distinct deptno,job
     from emp

6、使用表达式来显示列
     检索emp表中的员工姓名及全年的月收入
     select ename,sal*12 from emp
     

7、使用列别名
   用姓名显示员工姓名,用年收入显示全年月收入。
   select ename "姓名",sal*12 "年收入"  from emp

8、连接字符串
  在oracle中连接字符串用"||"操作符来完成的,当连接字符串时,
    如果在字符串要加入数字值,那么在"||"后可以直接指定数字,如果在字符串加入字符和日期值,则必须要用单引号。
     检索emp表,用is a 这个字符串来连接员工姓名和工种两个字段
     select ename || '  is  a  ' || job from emp

9、使用WHERE子句
    检索月收入大于2000的员工姓名及月收入。
      select ename,sal  from emp where sal>2000 
      
    检索月收入在1000元到2000元的员工姓名、月收入及雇佣时间。
      select ename,sal,hiredate from emp where sal between 1000 and 2000
    

10、like的用法:
    检索以S开头的员工姓名及月收入。
      select ename,sal from emp where ename like 'S%'
      
	 检索员工姓名中的第三个字符是A的员工姓名及月收入。
       select ename,sal from emp where ename like '__A%' 

11、在WHERE条件中使用IN操作符
    检索emp表中月收入是800的或是1250的员工姓名及部门编号
    select ename,deptno from emp where sal in(800,1250)

12、在WHERE条件中使用逻辑操作符(AND、OR、NOT)
    显示在部门20中岗位CLERK的所有雇员信息
    select * from emp where deptno=20 and job='CLERK'
    
    显示工资高于2500或岗位为MANAGER的所有雇员信息
    select * from emp where sal>2500 or job='MANAGER'


13、查询表中是空值的数据
	  检索emp表中有提成的员工姓名、月收入及提成。
    select ename,sal,comm from emp where comm is not null
	

14、使用ORDER BY子句,进行排序。
    检索emp表中部门编号是30的员工姓名、月收入及提成,并要求其结果按月收入升序、然后按提成降序显示。
    select ename,sal,comm from emp 
    where deptno=30 
    order by sal asc,comm desc

1.	查询工资大于1200的员工姓名和工资
    select ename,sal from emp where sal>1200

2.	查询员工号为176的员工的姓名和部门号
   select ename,deptno from emp where empno=176

3.	选择工资不在5000到12000的员工的姓名和工资
    select ename,sal from emp where sal not  between 5000 and 12000

4.	选择雇用时间在1998-02-01到1998-05-01之间的员工姓名,职位(job)和雇用时间,按从早到晚排序.
   select ename,job,hiredate 
   from emp 
   where to_char(hiredate,'yyyy-mm-dd') between '1980-02-01' and '1998-05-01'
   order by hiredate asc

5.	选择在20或50号部门工作的员工姓名和部门号
    select ename,deptno from emp 
    where deptno=20 or deptno=50

6.	选择在1994年雇用的员工的姓名和雇用时间
    select ename,hiredate 
    from emp
    where to_char(hiredate,'yyyy')='1987'
    
7.	选择公司中没有管理者的员工姓名及job
    select ename,job 
    from emp
    where mgr is null

8.	选择公司中有奖金 (COMM不为空,且不为0) 的员工姓名,工资和奖金比例,按工资逆排序,奖金比例逆排序.
    select  ename,sal,sal/comm 
    from emp 
    where comm is not null and comm>0
    order by sal desc,sal/comm desc
    
9.	选择员工姓名的第三个字母是a的员工姓名

10.	选择姓名(FIRST_NAME,LAST_NAME)中有字母a和e的员工姓名
	假设有FIRST_NAME, LAST_NAME
  select  first_name||last_name 
  from employees
  where (FIRST_NAME like '%a%' and LAST_NAME like '%a%' )or (FIRST_NAME like '%e%' and LAST_NAME like '%e%')



测 试
1.	显示系统时间(取别名为"DATE")
    select sysdate "DARE" from dual

2.	查询员工号,姓名,工资(若为NULL则作为0处理),
           以及工资提高百分之20%后四舍五入到整数的结果(取别名为new salary)
     select  empno,ename,nvl(sal,0),round(nvl(sal,0)*1.2) "new salary"
     from emp
 
3.	将员工的姓名(取别名为"Name")按字母表先后顺序排序,并写出姓名的长度(取别名为"length")
    select ename "Name",length(ename) "length"
    from emp
    order by ename asc

4.	查询各员工的姓名,并显示出各员工在公司工作了多少个月份(起别名为"worked_month")四舍五入到整数.
   select ename, round(months_between(sysdate,hiredate))  "worked_month"
   from emp



5.	查询员工的姓名和工资,按下面的形式显示结果(工资字段必须为15位,空位用$填充)
姓名	工资
KING	$$$$$$$$$$24000
MIKE	$$$$$$$$$$$4800
 
  select upper(ename) "姓名",lpad(sal,15,'$') "工资"
  from emp

6.	查询员工的姓名,以及在公司工作满了多少个月(worked_month),并按月份数降序排列
    select ename,trunc(months_between(sysdate,hiredate)) "worked_month"
    from emp
    order by "worked_month" desc



7.	做一个查询,按下面的形式显示结果
<ename> earns <sal> monthly but wants <sal*3>
Dream Salary
KING earns $24000 monthly but wants $72000
JONE earns $18000 monthly but wants $54000
……………………………………………………

select upper(ename)|| ' earns  '|| to_char(sal,'$9999999')|| '  monthly but wants  '||to_char(sal*3,'$9999999')
from emp


8.	做一个查询,按下面的形式显示结果(注意两个日期之间的关系)需要设置语言环境:  
ALTER SESSION SET NLS_LANGUAGE=AMERICAN;
ENAME	HIREDATE	  6个月零4天后
KING	17-JUN-87	  MONDAY,the TWENTY-FIRST of DECEMBER , 1987

select upper(ename)"ENAME", to_char(hiredate,'dd-mon-yy') "HIREDATE",
         to_char(add_months(hiredate,6)+4,'DAY ",the "  DDSPTH  " of " MONTH    " ,"  YYYY ')  " 6个月零4天后"
from emp 
  
  9.	做一个查询,按下面的形式显示结果
  Employees_and_their_salarys
  King************************
  Zhang*******************
  Wang*****************(姓名的 长度+工资的数量(1000))
  ………………………….
  
  其中每一个*代表一千元(四舍五入)。按工资从多到少排序.
     
select rpad(initcap(ename),length(ename)+round(sal/1000),'*')
from emp


10.	使用decode函数,按照下面的条件:(根据job类别来确定级别GRADE)
job       grade
AD_PRES					A
ST_MAN					B
IT_PROG					C
SA_REP 					D
ST_CLERK     			E
…….						OTHER
产生下面的结果:
ENAME	Job_id	Grade
king	AD_PRES	A
kate	IT_PROG	C

select job, decode(job,
                   'CLERK','A',
                   'SALESMAN','B',
                   'MANAGER','C',
                   'ANALYST','D',
                   'PRESIDENT','E',
                   'other')   "grade"
from emp

11.	将上一题的查询用case函数再写一遍。
  select job,case job
              when 'CLERK' then 'A'
              when 'SALESMAN' then 'B'
              when 'MANAGER' then 'C'
              when 'ANALYST' then 'D'
              when 'PRESIDENT' then 'E'
              else 'other'
              end "grade"
  from emp            
   

1.	组函数处理多行返回一行(对)判断题
  
2.	组函数不计算空值(错)判断题

3.	where子句在分组之前对检索进行过滤(对)判断题

4.	查询公司员工工资的最大值,最小值,平均值,总和
    select max(sal),min(sal),avg(sal),sum(sal)
    from emp

5.	查询各job的员工工资的最大值,最小值,平均值,总和
    select job, max(sal),min(sal),avg(sal),sum(sal)
    from emp
    group by job


6.	选择具有各个job 的员工人数
    select job,count(*)
    from emp
    group by job

7.	查询员工最高工资和最低工资的差距(DIFFERENCE)
    select max(sal)-min(sal) "DIFFERENCE" from emp 

8.	查询公司的人数,以及在80,81,82,87年,每年雇用的人数,结果类似下面的格式
     total	1980	1981	1982	1987
     14	     1	   10	    1	    2
     
     
      select count(*) from emp where to_char(hiredate,'yyyy')='1980'
      
      
      select count(*) from emp where to_char(hiredate,'yyyy')='1981'
   
      select    count(*) "total",
                sum(decode(to_char(hiredate,'yyyy'),'1980',1,0)) "1980",
                sum(decode(to_char(hiredate,'yyyy'),'1981',1,0)) "1981",
                sum(decode(to_char(hiredate,'yyyy'),'1982',1,0)) "1982",
                 sum(decode(to_char(hiredate,'yyyy'),'1987',1,0)) "1987"
      from emp

测 试
1.	显示所有员工的姓名,部门号和部门名称(dname)。联合查询emp, dept.两个表中都有deptno字段
   --分析字段:员工的姓名  部门号   部门名称(dname)
      --emp   员工的姓名  部门号
      --dept  部门号  部门名称(dname)
      --条件  emp.部门号=dept.部门号

   select e.ename,e.deptno,d.dname
   from emp e,dept d
   where e.deptno=d.deptno
   
   
   select e.ename,e.deptno,d.dname
   from emp e join dept d
   on(e.deptno=d.deptno)
   
   
   

2.	不重复地查询90号部门员工的job和90号部门的loc
  --分析字段:job  loc
      --emp   job
      --dept  loc
      --条件  emp.部门号=dept.部门号 and emp.部门号=90
 
   select distinct e.job,d.loc
   from emp e,dept d
   where e.deptno=d.deptno and e.deptno=10
   
    select distinct e.job,d.loc
   from emp e join dept d
   on(e.deptno=d.deptno) where e.deptno=10

3.	选择所有有奖金的(奖金非空并且大于0)员工的ename , dname , loc , 
     city(请参考dept表的结构,并根据需要创建表LOCATION,其中包含字段loc varchar2(30),city varchar2(20))
     create table LOCATION(
       loc  varchar2(30),
       city varchar2(20)
     )
   --分析字段:ename , dname , loc   city(
      --emp   ename 
      --dept  dname , loc
      --LOCATION loc city
      --条件  emp.部门号=dept.部门号 and  LOCATION.loc=dept.loc

     select e.ename,d.dname,d.loc,l.city
     from emp e,dept d,location l
     where e.deptno=d.deptno and d.loc=l.loc  
        
        
     select e.ename,d.dname,d.loc,l.city
            from emp e join dept d 
                 on(e.deptno=d.deptno) 
            join location l
                on(d.loc=l.loc)  
        


4.	选择在Toronto(一个city)工作的员工的ename , job , deptno , dname 
 --分析字段:ename, job, deptno, dname 
      --emp       ename , job
      --dept      deptno , dname
      --LOCATION  city
      --条件  emp.部门号=dept.部门号 and  LOCATION.loc=dept.loc

     select e.ename,e.job,e.deptno,d.dname
     from emp e,dept d,location l
     where e.deptno=d.deptno and d.loc=l.loc   and  l.city='纽约'
     
     
     select e.ename,e.job,e.deptno,d.dname
     from emp e join dept d on( e.deptno=d.deptno) join location l on(d.loc=l.loc)  where l.city='纽约'


5.	选择所有员工的姓名,员工号,以及他的管理者的姓名和员工号,结果类似于下面的格式
    employees	  Emp#	   manager   	Mgr#
    kochhar	    101      	king	     100
    kate	      202	      peter     	201
    
    select  e.ename "employees",e.empno "Emp#",m.ename "manager",e.mgr "Mgr#"
    from emp e,emp m
    where e.mgr=m.empno

    select  e.ename "employees",e.empno "Emp#",m.ename "manager",e.mgr "Mgr#"
    from emp e join emp m
         on(e.mgr=m.empno)
 
6.	查询各部门员工姓名和他们的同事(在同一个部门工作的其他人)姓名,结果类似于下面的格式
Department_id	   Last_name   	colleague
20	             Fay	          Brown
20            	Fay	            Peter

select  e.deptno,e.ename,c.ename
from emp e,emp c
where e.deptno=c.deptno and e.ename<> c.ename


select  e.deptno,e.ename,c.ename
from emp e join emp c
on(e.deptno=c.deptno) where  e.ename<> c.ename


7.	查询各个管理者的编号及其手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
    select e.mgr,min(sal)
    from emp e
    where e.mgr is not null
    group by e.mgr
    having min(sal)>2000

8.	查询所有部门的名称,loc,员工数量和工资平均值

    select d.dname,d.loc,count(*),avg(sal)
    from emp e join dept d on(e.deptno=d.deptno)
    group by d.dname,d.loc
    

1.	查询和SMITH相同部门的其他员工姓名和雇用日期
     
     select ename,hiredate from emp
     where deptno=(select deptno from emp where ename='SMITH') and ename<>'SMITH'
    
2.	查询工资比公司平均工资高的员工的员工号,姓名和工资。
     select empno,ename,sal from emp where sal>(select avg(sal) from emp)

3.	查询: 和姓名中包含字母A的员工 在相同部门的员工的empno和ename
select empno,ename from  emp where deptno in(
     select deptno from emp where ename like '%A%'
   )

4.	查询在loc为NEW YORK的部门工作的员工的员工号,ename,deptno,job(使用连接查询,子查询两种查询方式)
    --子查询
    select empno,ename,deptno,job from emp where  deptno =
                    (select deptno from dept where loc='NEW YORK')             
    --连接查询
    select e.empno,e.ename,d.deptno,e.job from emp  e  join dept  d on(e.deptno=d.deptno) and d.loc='NEW YORK'
    
 
5.	查询管理者是KING的员工姓名和工资(至少使用两种查询方式)

    --子查询
       select ename,sal from emp where mgr=(select empno from emp where ename='KING')
       
    --连接查询
       select e.ename,e.sal
       from emp e,emp m
       where e.mgr=m.empno and m.ename='KING'

【上篇】
【下篇】

抱歉!评论已关闭.