00-基础SQL-SELECT语句

 

DML(数据操作语言,增删查改)
DDL(数据定义语言,比如创建、修改或删除数据库对象)
DCL(数据控制语言,控制数据库的访问)

desc 表名:显示表结构
dual : 伪表

对于日期型数据,做 * 、/ 运算不合法;
包含空值的数学表达式的值都为空值;
别名使用双引号;

连接字符串使用:“ ||
日期与字符串只能在“单引号”中出现;
重复行:去重:distinct;


sql是一种语言;
sql*plus是一种环境,关键字可以缩写,如(desc、ed);

where紧跟from语句;

过滤与排序:
  过滤 :where
  字符串区分大小写;
  日期格式敏感 DD-MM-YYYY(抛弃),用to_char()转换;


赋值 :=
比较运算:字段名 : between ..... and ..... 两个之间,包含边界
in :等值列中的多个;
like:模糊查询;

 

转义字符:escape 如 '%#_%' escape '#';
is null / is not null; 为空/不为空
排序:order by 字段 asc(升序),desc(降序);


单行函数:只对一行进行变换,每行返回一个结果;
字符函数:
  Lower:转小写
  Upper:转大写
  Initcap:首字母大写,其它小写;

 

字符控制函数:
  concat:连接,如concat('Hello','World');
  substr('helloworld',1,4):1开始,输出4个;
  Length:长度;
  Instr('helloworld','l'):返回首次出现的位置,无则返回0;
  LRAD/RPAD : 左对齐/右对齐,不足位,作补;如 LRAD(salary,10,'*')
  trim('h' from 'helloword'):去除首尾h;
  replace('abcd','b','m'):将所有b都转为m;

 

数字函数:
  round 四舍五入;
  trunc 直接截断;
  MOD:取余;

 

日期:加一个数字或减一个数字,仍为日期;
日期相减为天数;
可以除以24来向日期加上或减去天数;

 

日期函数:
  months_between(sysdate,hire_date):两个日期相差月数;
  add_months :向指定日期加上若干月;add_months(sysdate,2);
  next_day :指定的日期下一个星期某对应的日期;如next_day(sysdate,'星期一');
  Last_day:本月后的一天;last_day(sysdate)-1:倒数第二天
  round:日期四舍五入;
  trunc:日期戒断;

 

转换函数:
  to_char(hire_date,'yyyy-mm-dd');转换为字符串;
  to_date('1990-01-01','yyyy-mm-dd')=hire_date;转换为日期
  to_number('$12345678.123','$000,000,000.00');前后对应;

 

通用函数:使用于任何数据类型,也适用于空值;
  NVL(EXP1,EXP2):如果exp1为空,值则为exp2;
  NVL2(EXP1.EXP2,EXP3):exp1不为空,返回EXP2 , 为空,返回exp3;
  NULLIF(EXP1,EXP2) : 相等返回null,不等返回exp1;
  COALESCE(EXP1,EXP2,EXP3.....):exp1为空,值则为EXP2,如果exp2为空,则一直往下...

条件表达式:
sql语言中使用if-then-else;
使用两种方式:
  ① case:
    case 某列 when ..... then .....else .....end 无标点符号;
    译:当某列的值为 某,则是 某值,否则 为某,结束;

eg:

  查询部门号为 10, 20, 30 的员工信息, 若部门号为 10, 则打印其工资的 1.1 倍, 20 号部门, 则打印其工资的 1.2 倍, 30 号部门打印其工资的 1.3 倍数;

SELECT last_name, job_id, salary,
       CASE job_id WHEN 'IT_PROG'  THEN  1.10*salary
                   WHEN 'ST_CLERK' THEN  1.15*salary
                   WHEN 'SA_REP'   THEN  1.20*salary
       ELSE      salary END     "REVISED_SALARY"
FROM   employees;

  

  ②decode:

    decode(变量,1,2,1,2);
    变量,为1时,值为2,依次往下,括号结束;

eg:  

SELECT last_name, job_id, salary,
       DECODE(job_id, 'IT_PROG',  1.10*salary,
                      'ST_CLERK', 1.15*salary,
                      'SA_REP',   1.20*salary,
                              salary)
       "REVISED_SALARY"
FROM   employees;

 

eg: 

 1 --查询工资大于12000的员工姓名和工资
 2 select last_name,salary
 3 from employees
 4 where salary > 12000;
 5 
 6 --查询员工号为176的员工的姓名和部门号
 7 select last_name,department_id
 8 from employees
 9 where employee_id = 176;
10 
11 --选择工资不在5000到12000的员工的姓名和工资
12 select last_name,salary
13 from employees
14 --where salary < 5000 or salary > 12000
15 where salary not between 5000 and 12000;
16 
17 --选择雇用时间在1998-02-01到1998-05-01之间的员工姓名,job_id和雇用时间
18 select last_name,job_id,hire_date
19 from employees
20 --where hire_date between '1-2月-1998' and '1-5月-1998'
21 where to_char(hire_date,'yyyy-mm-dd') between '1998-02-01' and '1998-05-01';
22 
23 --选择在20或50号部门工作的员工姓名和部门号
24 select last_name,department_id
25 from employees
26 where department_id = 20 or department_id = 50;
27 --where department_id in (20,50)
28 
29 --选择在1994年雇用的员工的姓名和雇用时间
30 select last_name,hire_date
31 from employees
32 --where hire_date like '%94'
33 where to_char(hire_date,'yyyy') = '1994';
34 
35 --选择公司中没有管理者的员工姓名及job_id
36 select last_name,job_id
37 from employees
38 where manager_id is null;
39 
40 选择公司中有奖金的员工姓名,工资和奖金级别
41 select last_name,salary,commission_pct
42 from employees
43 where commission_pct is not null;
44 
45 --选择员工姓名的第三个字母是a的员工姓名
46 select last_name
47 from employees
48 where last_name like '__a%';
49 
50 --选择姓名中有字母a和e的员工姓名
51 select last_name
52 from employees
53 where last_name like '%a%e%' or last_name like '%e%a%';
54 
55 
56 --显示系统时间(注:日期+时间)
57 select to_char(sysdate,'yyyy-mm-dd hh:mi:ss')
58 from dual;
59 
60 --查询员工号,姓名,工资,以及工资提高百分之20%后的结果(new salary)
61 select employee_id,last_name,salary,salary*1.2 "new salary"
62 from employees;
63 
64 --将员工的姓名按首字母排序,并写出姓名的长度(length)
65 select last_name,length(last_name)
66 from employees
67 order by last_name asc;
68 
69 --查询各员工的姓名,并显示出各员工在公司工作的月份数(worked_month)。
70 select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
71 from employees;
72 
73 --查询员工的姓名,以及在公司工作的月份数(worked_month),并按月份数降序排列
74 Select last_name,hire_date,round(months_between(sysdate,hire_date),1) workded_month
75 from employees
76 order by workded_month desc;
77 
78 --Dream Salary
79 --King earns $24000 monthly but wants $72000
80 select last_name || ' earns '|| to_char(salary,'$999999')||' monthly,but wants '||to_char(3*salary,'$999999') "Dream Salary"
81 from employees;
82 
83 --查询员工的姓名,职务ID和等级
84 --第一种方式
85 select last_name "Last_name",job_id "Job_id",decode(job_id,'AD_PRES','A','ST_MAN','B', 'IT_PROG','C', 'SA_REP','D', 'ST_CLERK','E') "Grade"
86 from employees;
87 ----第二种方式
88 select last_name "Last_name",job_id "Job_id",case job_id when 'AD_PRES'then 'A'
89 when 'ST_MAN' then 'B'
90 when 'IT_PROG' then 'C'
91 when 'SA_REP' then 'D'
92 when 'ST_CLERK' then'E' end  "Grade"
93 from employees;

 

posted @ 2020-07-28 10:32  路修索  阅读(209)  评论(0编辑  收藏  举报