`
edwards0307
  • 浏览: 27339 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

SQL

 
阅读更多

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(>,>=,<,<=,=,<>,between...and(>=...<=),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)>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)>(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 ==== <> 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>all(select salary from employees where job_id='IT_PROG')

      and job_id<>'IT_PROG';

 

      select employee_id,last_name,job_id,salary

      from employees

      where salary<all(select salary from employees where job_id='IT_PROG')

      and job_id<>'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<>'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<>'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<>'IT_PROG';

 

     

 

    

     

分享到:
评论

相关推荐

    通过SqlCmd执行超大SQL文件

    ##通过sqlcmd执行sql文件 由于sql文件过大,超过了100M,再数据库的窗口执行,结果超出内存了,对于特别大的sql文件可以使用sqlcmd进行执行 ###1.打开cmd窗口 运行–cmd–进入到sql文件所在的文件夹。 如果是win7可...

    java sql操作工具类 java sql操作工具类

    java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作工具类 java sql操作工具类java sql操作...

    SQLServer_2000-2008_R2查询智能分析器RedGate_SQL_Prompt_V5.3.4.1_Crack_Keygen破解教程注册机免费

    在我个人编写SQL脚本时,至少会把SQL的格式排列成易于阅读的,因为其他人会阅读到你的SQL,无论是在程序中或是脚本文件中,良好的排版不仅让人看起来赏心悦目,在和他人之间做交流时也省时省力,不会因为揉成一团的...

    SQL优化 SQL优化软件 SQL优化工具

    SQL优化是数据库管理中的关键环节,它涉及到提升查询性能、减少资源消耗以及改善系统整体效率。SQL优化软件和工具能够帮助数据库管理员(DBA)和开发人员找出性能瓶颈,优化查询逻辑,从而提高数据库系统的响应速度...

    SQLPrompt5.3破解

    本人在Windows7 64位+SQL Server 2012环境下测试通过(系统是全新安装) 使用方法: 1,安装SQLPrompt v5.3,这个不多说。 2,安装完毕后,断开网络连接。 3,打开Visual Studio或者SQL Server Management Studio(版本...

    SQLPrompt for SQLServer2016 智能提示插件 SQL2016 提示

    SQLPrompt for SQLServer2016 智能提示插件 SQL2016 提示 SQLPrompt最新版本 绿色版 SQL Prompt 是一款拥有SQL智能提示功能的SQL Server和VS插件。SQL Prompt能根据数据库的对象名称,语法和用户编写的代码片段自动...

    sqlserver自动生成sql语句工具sqlserver转oracle

    在IT行业中,数据库管理系统是核心组成部分,SQL Server和Oracle分别是微软和甲骨文公司推出的两款广泛应用的关系型数据库系统。在企业级应用中,有时需要在不同的数据库系统间进行数据迁移或兼容性处理,这就涉及到...

    sql server 导入超大SQL脚本文件

    SQL Server 导入超大 SQL 脚本文件 SQL Server 是一种关系型数据库管理系统,广泛应用于各种行业。然而,在实际应用中,我们经常会遇到导入超大 SQL 脚本文件的问题。本文将介绍如何使用 osql 工具来导入超大 SQL ...

    Android通过webservice连接Sqlserver实例

    在Android开发中,有时我们需要与远程数据库进行交互,例如SQLServer。这个场景通常是通过Web服务,如WebService来实现。本文将详细介绍如何在Android应用中利用WebService接口连接到SQLServer数据库,实现数据的增...

    SQL SQLPrompt 9 SQL 2016/2017可用

    SQL Prompt是Redgate Software开发的一款高效SQL代码编辑工具,它为SQL Server的开发人员提供了智能提示、格式化、重构和代码分析等功能,极大地提升了编写和维护SQL代码的效率。SQL Prompt 9是该系列的最新版本,...

    SQL 语法 SQL 总结 SQL教程

    SQL 基础 SQL 首页 SQL 简介 SQL 语法 SQL select SQL distinct SQL where SQL AND & OR SQL Order By SQL insert SQL update SQL delete SQL 高级 SQL Top SQL Like SQL 通配符 SQL In SQL Between ...

    SQLMonitor oracle跟踪SQL工具

    《SQLMonitor:Oracle数据库SQL跟踪与分析利器》 在IT行业中,数据库的高效管理与优化是至关重要的。针对Oracle数据库,有一款名为SQLMonitor的工具,它专为跟踪和监控SQL语句而设计,帮助开发者和DBA们找出程序...

    Sql Server 2014 安装包

    Sql Server 2014 安装包 SQL Server 2014是微软推出的一款关系型数据库管理系统,它在企业级数据管理和分析领域扮演着重要的角色。此安装包包含两个主要组件:SQL Management Studio和SQL Server Express。 1. **...

    AI自动生成SQL语句的开源代码 sqlcoder-main.zip

    开源的AI自动生成SQL语句源代码,这款SQLCoder-70B-Alpha在文本到SQL的转换能力上超越了包括GPT-4在内的所有通用模型,它能更准确地理解你的需求,并生成相应的SQL查询。SQLCoder2和SQLCoder-7B模型已经向公众开放,...

    sqlserver驱动包 jdbc驱动 sqljdbc.jar和sqljdbc4.jar

    SQL Server驱动包是用于Java应用程序通过JDBC(Java Database Connectivity)接口与Microsoft SQL Server数据库进行交互的必备组件。本文将详细介绍这两个重要的驱动文件——sqljdbc.jar和sqljdbc4.jar,以及如何...

    sqlserver驱动包:sqljdbc4.jar

    SQL Server驱动包`sqljdbc4.jar`是微软官方提供的Java数据库连接器(JDBC),用于在Java应用程序中与Microsoft SQL Server进行通信。JDBC是Java编程语言中的一个标准API,它使得开发人员能够以标准化的方式访问各种...

    sqlservr32和sqlservr64.zip

    标题中的"sqlservr32和sqlservr64.zip"指的是SQL Server 2005服务中的两个关键组件,`sqlservr32.exe`和`sqlservr64.exe`。这两个文件是SQL Server服务的核心执行文件,分别对应于32位和64位操作系统。在Windows 8和...

    SQLTracker,抓取sql语句的工具

    SQLTracker是一款专为数据库操作监控设计的工具,它在IT领域中主要用于跟踪和记录SQL语句的执行情况。SQL(Structured Query Language)是用于管理关系数据库的编程语言,包括查询、更新、插入和删除数据等操作。SQL...

    oracle sqldeveloper连接mysql、SQLServer第三方dll

    解决oracle sqldeveloper无法连接mysql、SQLServer问题,sqlDeveloper是ORACLE数据库开发工具,自带的是无法连接MS SQL Server以及mysql的,想连接的话需要第三方工具。 使用方法: 解压出来后将2个jar放入jlib...

    Oracle Sql语句转换成Mysql Sql语句

    在数据库管理领域,Oracle SQL和MySQL SQL是两种广泛使用的SQL方言,它们在语法和功能上存在一定的差异。当需要将一个基于Oracle SQL的应用程序迁移到MySQL环境时,就需要进行SQL语句的转换工作。本项目提供了一个...

Global site tag (gtag.js) - Google Analytics