Oracle——基本SQL「建议收藏」

Oracle——基本SQL「建议收藏」 第 1 章 基本SQL SELECT语句 1. 对于日期型数据, 做 , / 运算不合法 2. 包含空值的数学表达式的值都为空值 3. 别名使用双引号! 4. oracle 中连接字符串使用

Oracle——基本SQL[数据库教程]


第 1 章 基本SQL-SELECT语句

  1. 对于日期型数据, 做 *, / 运算不合法

  2. 包含空值的数学表达式的值都为空值

  3. 别名使用双引号!

  4. oracle 中连接字符串使用 “||”, 而不是 java 中的 “+”

  5. 日期和字符只能在单引号中出现. 输出 last_name`s email is email

    select last_name || ‘ `s email is ‘ || email EMAIL
    from employees
    

    代码100分

  6. distinct 关键字, 以下语法错误

    代码100分select last_name, distinct department_id
    from employees
    

第 2 章 过滤和排序数据

  1. WHERE 子句紧随 FROM 子句

  2. 查询 last_name 为 ‘King‘ 的员工信息

    错误1: King 没有加上 单引号

    select first_name, last_name
    from employees
    where last_name = King
    

    错误2: 在单引号中的值区分大小写

    代码100分select first_name, last_name
    from employees
    where last_name = ‘king‘
    

    正确

    select first_name, last_name
    from employees
    where last_name = ‘King‘
    
  3. 查询 1998-4-24 来公司的员工有哪些?

    注意: 日期必须要放在单引号中, 且必须是指定的格式

    select last_name, hire_date
    from employees
    where hire_date = ‘24-4月-1998‘
    
  4. 查询工资在 5000 — 10000 之间的员工信息.

    1). 使用 AND

    select *
    from employees
    where salary >= 5000 and salary <= 10000
    

    2). 使用 BETWEEN .. AND .., 注意: 包含边界!!

    select *
    from employees
    where salary between 5000 and 10000
    
  5. 查询工资等于 6000, 7000, 8000, 9000, 10000 的员工信息

    1). 使用 OR

    select *
    from employees
    where salary = 6000 or salary = 7000 or salary = 8000 or salary = 9000 or salary = 10000
    

    2). 使用 IN

    select *
    from employees
    where salary in (6000, 7000, 8000, 9000, 10000)
    
  6. 查询 LAST_NAME 中有 ‘o‘ 字符的所有员工信息.

    select *
    from employees
    where last_name like ‘%o%‘
    
  7. 查询 LAST_NAME 中第二个字符是 ‘o‘ 的所有员工信息.

    select *
    from employees
    where last_name like ‘_o%‘
    
  8. 查询 LAST_NAME 中含有 ‘_‘ 字符的所有员工信息

    1). 准备工作:

    update employees
    set last_name = ‘Jones_Tom‘
    where employee_id = 195
    

    2). 使用 escape 说明转义字符.

    select *
    from employees
    where last_name like ‘%\_%‘ escape ‘‘
    
  9. 查询 COMMISSION_PCT 字段为空的所有员工信息

    select last_name, commission_pct
    from employees
    where commission_pct is null
    
  10. 查询 COMMISSION_PCT 字段不为空的所有员工信息

    select last_name, commission_pct
    

from employees
where commission_pct is not null
“`

  1. ORDER BY:
    1). 若查询中有表达式运算, 一般使用别名排序
    2). 按多个列排序: 先按第一列排序, 若第一列中有相同的, 再按第二列排序.
    格式: ORDER BY 一级排序列 ASC/DESC,二级排序列 ASC/DESC;

第 3 章 单行函数

  1. 打印出 “2009年10月14日 9:25:40” 格式的当前系统的日期和时间.

    select to_char(sysdate, ‘YYYY"年"MM"月"DD"日" HH:MI:SS‘)
    from dual	
    

    注意: 使用双引号向日期中添加字符

  2. 格式化数字: 1234567.89 为 1,234,567.89

    select to_char(1234567.89, ‘999,999,999.99‘)
    from dual
    
  3. 字符串转为数字时
    1). 若字符串中没有特殊字符, 可以进行隐式转换:

        select ‘1234567.89‘ + 100
    from dual
        ```
    
    
    2). 若字符串中有特殊字符, 例如 ‘1,234,567.89‘, 则无法进行隐式转换, 需要使用 to_number() 来完成
    
    

    select to_number(‘1,234,567.89‘, ‘999,999,999.99‘) + 100
    from dual

    
    
    
    
  4. 对于把日期作为查询条件的查询, 一般都使用 to_date() 把一个字符串转为日期, 这样可以不必关注日期格式

    select last_name, hire_date
    from employees
    where hire_date = to_date(‘1998-5-23‘, ‘yyyy-mm-dd‘)
    --	where to_char(hire_date,‘yyyy-mm-dd‘) = ‘1998-5-23‘
    
  5. ??转换函数: to_char(), to_number(), to_date()

  6. 查询每个月倒数第 2 天入职的员工的信息.

    select last_name, hire_date
    from employees
    where hire_date = last_day(hire_date) - 1
    
  7. 计算公司员工的年薪

    --错误写法: 因为空值计算的结果还是空值
    select last_name, salary * 12 * (1 + commission_pct) year_sal
    from employees
    
    --正确写法
    select last_name, salary * 12 * (1 + nvl(commission_pct, 0)) year_sal
    from employees
    
  8. ??查询部门号为 10, 20, 30 的员工信息, 若部门号为 10, 则打印其工资的 1.1 倍, 20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数

    --使用 case-when-then-else-end
    select last_name, department_id, salary, case department_id when 10  then salary * 1.1
                                                                    when 20  then salary * 1.2
                                                                    when 30  then salary * 1.3
                                                 end new_sal
    from employees
    where department_id in (10, 20, 30)
    
    --使用 decode
    select last_name, department_id, salary, decode(department_id, 10, salary * 1.1,
                                               		               20, salary * 1.2,
                                                                       30, salary * 1.3
                                                 ) new_sal
        from employees
        where department_id in (10, 20, 30)
    

Oracle——基本SQL

原文:https://www.cnblogs.com/sunyanblog/p/12763603.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/8857.html

(0)
上一篇 2023-02-20
下一篇 2023-02-20

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注