---
title: "SQL "
author: "edwards0307"
date: "2012-02-15T18:01:22Z"
tags: []
categories: ["互联网"]
slug: "sql"
draft: false
lastmod: "2012-02-20T11:30:49Z"
id: 1407503
url: "https://killdbs.iteye.com/blog/1407503"
---

1.select sal salary from emp;

   select sal as salary from emp;

   select sal as "salary" from emp;

   select comm "comm sal" from emp;

  column alias can not use where clause

 

2.DML select,insert,update,delete,merge

   DCL grant,revoke

   DDL create,drop,alter,rename,truncate

   DTL  commit,rollback,savepoint

 

3.select statements are not  case-sensitive

   select * from emp;

   select ename,id from emp;

   select sal+100 from emp; (+,-,\*,/,()),number or date data can use them

  

4.NULL value   unknown value,is not the same as zero or a blank space

                        the value of numeric expression containing NULL is NULL

                        the value of character expression containing NULL is no null, is original value 

5.||

   select ename||' '||job from emp;

 

6.string character or date  using single quotation

   select  ename||'is a'||job from emp;

   select  ename||q'\['s a ]'||job from emp;

 

7.select distinct deptid from emp;

   select distinct ename,empno from emp;

 

8 desc emp;

   select dbms\_metadata.getddl('TABLE','T') from dual;

 

9.character strings and dates

   strings and dates must use single quotations

   strings are case sensitive

   date are format-sensitive

   the defult date format is dd-mon-rr

 

10.comparison operators(&gt;,&gt;=,&lt;,&lt;=,=,&lt;&gt;,between...and(&gt;=...&lt;=),in(set),like,is null)

 

11.select sal from emp where sal between 100 and 3000;

     select ename from emp where ename between 'ALLEN' and 'CLARK';

 

12.in/not in

     select ename from emp where mgr in(7698,7782,7788); 

     select ename from emp where mgr=7698 or mgr=7788 or mgr=7782;

 

     select ename from emp where job in ('CLERK','MANAGER');

     select ename from emp where job='CLERK' or job='MANAGER';

 

13.like,\_,%

     select ename from emp where ename like 's%';

     select ename from employees where hire\_date like '%95';

     select job\_id from employees where job\_id like '%ST\\\_%' escape '\\'

 

14.is null

     is not null

 

15.and,or,not

     not in

     not like

 

16.order by

     1. asc(default)

         desc

     2. come last the select clause

     3. sorting by alias

     4. in asc, null last

             desc,nul first

    

     select employee\_id,salary,hire\_date from employees order by 3;

     select employee\_id,salary,hire\_date from employees order by hire\_date;

     select employee\_id,salary\*12 sal,hire\_date from employees order by sal;

     select employee\_id,department\_id,salary\*12 sal,hire\_date from employees order by department\_id,sal desc;

     select employee\_id,department\_id,salary\*12 sal,hire\_date from employees order by department\_id,sal desc nulls first;

     select employee\_id,department\_id,salary\*12 sal,hire\_date from employees order by department\_id,sal desc nulls last;

 

17.substitution variables

     1.&|&&

     2.using quotation when date or character string  '&hire\_date'

     3. can use in anyway in the sql statement

     4. is client function ,not oracle server

     5.&& can not promt the same column\_name

     select employee\_id,department\_id,salary\*12 sal,hire\_date from employees where employee\_id=&empid;

     select employee\_id,department\_id,salary\*12 sal,hire\_date from employees where hire\_date='&hiredate'; 

    select employee\_id,&hire\_date,salary\*12 sal,hire\_date from employees where hire\_date='&hiredate'; 

    select employee\_id,&&hire\_date,salary\*12 sal from employees order by &hire\_date

 

18.define

     define empno=20

     undefine empno

     select employee\_id from employees where employee\_id=&empno; 

 

19.verify: weather is or display the before and after of substitution variables

     set verify on

     set verify off

   

20.single row:return one result per row

     character functions

     lower()   concat('hello','world')

     upper()   substr('helloworld',1,5)

     initcap()  length()

                    instr('helloworld','w')

                    lpad(salary,10,'\*'),rpad(salary,10,'\*')

                    replace('jack and jue','j','bl')

                    trim('h' from 'helloworld')  elloworld

      

     number functions

     round()

     trunc()

     mod()

  

     date functions

     select sysdate from dual;

     months\_between('01-sep-95','11-jan-94')

     add\_months('31-jan-96',1)

     next\_day('01-sep-95','friday')  

     last\_day('01-feb-95')

     trunc()

     round()

 

 

 21.conversion function

      select '12'+1 from dual;

      select to\_char(sysdate,'yy/mm') from dual;

      select to\_char(sysdate,'fmyy-mm-dd') from dual;  

      select to\_char(sysdate,'yyyy-mm-dd HH24:MI:SS') from dual; 

      select to\_char(6000,'$999,999.00') from dual; 

      select to\_number('-$95.24','$99.99') from dual; 

      select to\_date('12,05 99','dd,mm yyyy') from dual;

 

22. genaral function

      nvl(1,2)

      nvl2(1,2,3)

      nullif(length(1),length(2)) from dual  if the length of 1 ,2 is equal, return null, not equal,return lenth(1)

      coalese(1,2,3,'') if 1is not null, return 1,else do remaing coalse

     

 

23.condition expression

     1.case

     select last\_name,job\_id,  
          case job\_id when 'SH\_CLERK' THEN 1.10\*salary  
                       when 'SA\_REP'  then 1.15\*salary  
                       when 'SA\_MAN' then 2.0\*salary  
                      else salary end "revised salary"  
      from employees  
     2.decode

        select last\_name,job\_id,

           decode(job\_id,'SA\_REP',salary\*1.10,'SA\_MAN',salary\*1.15,'SA\_CLERK',salary\*2.0,salary)

        from employees

     

24.mutiple rows function : ignore null value

     avg()

     count()

     min()

     max()

     sum()

     stddev()

     variance()   

 

     select count(\*) from emp;

     select count(department\_id) from emp;  //return the number of rows with  not null vlues for expr

     select count(department\_id) from employees

     select count(distinct department\_id) from employees

     select avg(nvl(commission\_pct,0)) from employees;  

 

25.group by

     1.all columns in the select list that are not in group functions must be in the group by clause;

     2.can nou use group functions in the where clause;

     3. can not use where to restrict groups

     4. can use having to restrict groups

     select department\_id,avg(salary)

     from employees

     group by department\_id

 

     select department\_id,job\_id,sum(salary)

     from employees

     group by department\_id,job\_id

 

     select job\_id,max(salary) payroll

     from employees

     group by job\_id

     having max(salary)&gt;10000

 

26.execute order of the select statements

     from-where-group by-group functions-having-order by

    

27.select from muti tables

     1.tables alias, when we use table alias, we must use alias in places;

     2.column alias

     3.natural join(using) ,the same name,the same type

      

     一。sql std

      select department\_id,department\_name,location\_id,city                        
      from departments                                                             
      natural join locations;  (sql standard)

      where department\_id in(20,30);

 

      select employee\_id,last\_name,department\_id,department\_name  
      from employees join departments  
      using(department\_id)  
      where department\_id=30;

 

      select e.employee\_id,e.last\_name,e.department\_id,d.department\_name  
      from employees e join departments d  
      on (e.department\_id=d.department\_id) and (e.manager\_id=d.manager\_id)

 

     二。oracle std

   

      select department\_id,department\_name,d.location\_id,city                        
      from departments d,locations l                                                            
      where d.location\_id=l.location\_id(oracle sql)

   

     

    4.self join

      select w.last\_name,m.last\_name 

      from employees w join employees m

      on (w.manager\_id=m.employee\_id)

     

   5.not equaljoin

      select e.last\_name,e.salary,j.grade\_level

      from employees e join job\_grades  j

      on e.salary between j.lowerest\_sal and j.hightest\_sal;

 

      select e.last\_name,e.salary,j.grade\_level

      from employees e , job\_grades  j

      where e.salary between j.lowerest\_sal and j.hightest\_sal;

 

   6.left/right/full outer join

     select e.last\_name,e.department\_id,d.department\_name  
     from employees e left outer join departments d  
     on (e.department\_id=d.department\_id)

 

     select e.last\_name,e.department\_id,d.department\_name  
     from employees e right outer join departments d  
     on (e.department\_id=d.department\_id)

 

     select e.last\_name,e.department\_id,d.department\_name  
     from employees e full outer join departments d  
     on (e.department\_id=d.department\_id)

 

     oracle std

     select e.last\_name,e.department\_id,d.department\_name  
     from employees e ,departments d  
     where e.department\_id(+)=d.department\_id  (rigth join)

 

     select e.last\_name,e.department\_id,d.department\_name  
     from employees e ,departments d  
     where e.department\_id =d.department\_id(+)  (left join)

 

   7.cross join

      select last\_name,department\_name

      from employees cross join departments (sql std)

 

      select last\_name,department\_name

      from employees ,departments  (oracle std)

 

 28.subquery(inner query):

      1. the subquery(inner query) executes before the main query

      2. the subquery(inner query) exists from,where,having

      3. enclose subquery in parentheses

      4. place subquery on the right side of the comparison condition for readlibility(howerver,the subquery can appear on either side of the comparison operator)

      5. using single-row operators with single-row subquery

          multiple operators with mutilple operators

     

     一.single-row subquery

      select  last\_name,job\_id,salary

      from employees

      where salary =(select min(salary) from employees );

     

      select department\_id,min(salary)

      from employees

      group by department\_id

      having min(salary)&gt;(select min(salary) from employees where department\_id=50);

   

      select job\_id,avg(salary)

      from employees

      group by job\_id     

      having avg(salary)=(select min(avg(salary)) from employees group by job\_id);

   

    二.multi-row subquery(in ,any,all)

        not in ==== &lt;&gt; all

        in ====  =any   

      select employee\_id,last\_name

      from employees

      where salary in (select min(salary) from employees group by department\_id);

 

      select employee\_id,last\_name,job\_id,salary

      from employees

      where salary&gt;all(select salary from employees where job\_id='IT\_PROG')

      and job\_id&lt;&gt;'IT\_PROG';

 

      select employee\_id,last\_name,job\_id,salary

      from employees

      where salary&lt;all(select salary from employees where job\_id='IT\_PROG')

      and job\_id&lt;&gt;'IT\_PROG';

 

      select employee\_id,last\_name,job\_id,salary

      from employees

      where salary&gt;any(select salary from employees where job\_id='IT\_PROG')

      and job\_id&lt;&gt;'IT\_PROG';

 

      select employee\_id,last\_name,job\_id,salary

      from employees

      where salary&lt;any(select salary from employees where job\_id='IT\_PROG')

      and job\_id&lt;&gt;'IT\_PROG';

            

      select employee\_id,last\_name,job\_id,salary

      from employees

      where salary=any(select salary from employees where job\_id='IT\_PROG')

      and job\_id&lt;&gt;'IT\_PROG';