select语句
select * from departments;
select department_id,department_name from departments;
复制代码
算数运算符(+ - * /)
select last_name,salary,salary+300 from employees;
select last_name,salary,salary*12+100 from employees;
select last_name,salary,(salary+100)*12 from employees;
复制代码
定义空值
select last_name,job_id,salary,commission_pct from employees;
复制代码
列的别名
select last_name as name,commission_pct comm from employees;
select last_name as "name",commission_pct "comm" from employees;
复制代码
连接符
select last_name||job_id as "Employees" from employees;
复制代码
字符串
select last_name || ' is a ' || job_id as "Employees Details" from employees;
复制代码
重复行
select department_id from employees;
select distinct department_id from employees;
复制代码
过滤
SELECT
employee_id,
last_name,
job_id,
department_id
FROM
employees
WHERE
department_id = 90;
复制代码
字符和日期
SELECT
last_name,
hire_date,
department_id
FROM
employees
WHERE
hire_date = '7-6月-1994';
复制代码
比较运算
SELECT
last_name,
salary
FROM
employees
WHERE
salary <= 3000;
SELECT
last_name,
salary
FROM
employees
WHERE
salary BETWEEN 2500
AND 3500;
SELECT
employee_id,
last_name,
manager_id
FROM
employees
WHERE
manager_id IN (100, 101, 201);
SELECT
first_name
FROM
employees
WHERE
first_name LIKE 'S%';
SELECT
last_name
FROM
employees
WHERE
last_name LIKE '_o%';
SELECT
last_name,
manager_id
FROM
employees
WHERE
manager_id IS NULL;
复制代码
escape回避特殊符号
SELECT
job_id
FROM
jobs
WHERE
job_id LIKE 'IT\_%' ESCAPE '\' ;
复制代码
逻辑运算
SELECT
employee_id,
last_name,
job_id,
salary
FROM
employees
WHERE
salary >= 10000
AND job_id LIKE '%MAN%';
SELECT
employee_id,
last_name,
job_id,
salary
FROM
employees
WHERE
salary >= 10000
OR job_id LIKE '%MAN%';
SELECT
last_name,
job_id
FROM
employees
WHERE
job_id NOT IN (
'IT_PROG',
'ST_CLERK',
'SA_REP'
);
复制代码
order by子句
SELECT
last_name,
job_id,
department_id,
hire_date
FROM
employees
ORDER BY
hire_date DESC;
SELECT
employee_id,
last_name,
salary * 12 annsal
FROM
employees
ORDER BY
annsal;
SELECT
last_name,
department_id,
salary
FROM
employees
ORDER BY
department_id,
salary DESC;
复制代码
单行函数
字符函数
SELECT
employee_id,
last_name,
department_id
FROM
employees
WHERE
LOWER (last_name) = 'higgins';
从左到右查找字符第一次出现的位置,下标从0开始
SELECT
employee_id,
CONCAT (first_name, last_name) NAME,
job_id,
LENGTH (last_name),
INSTR (last_name, 'a')
FROM
employees
WHERE
SUBSTR (job_id, 4) = 'REP';
复制代码
数值函数
SELECT
last_name,
salary,
MOD (salary, 5000)
FROM
employees
WHERE
job_id = 'SA_REP';
复制代码
日期函数
SELECT
last_name,
(SYSDATE - hire_date) / 7 AS weeks
FROM
employees
WHERE
department_id = 90;
SELECT
MONTHS_BETWEEN (
'6-7月-1995',
'11-11月-1994'
)
FROM
dual;
SELECT
ADD_MONTHS ('6-7月-1995', 6)
FROM
dual;
SELECT
NEXT_DAY ('26-10月-2018', 7)
FROM
dual;
SELECT
LAST_DAY ('26-10月-2018')
FROM
dual;
round(sysdate,'month')
round(sysdate,'year')
trunc(sysdate,'month')
trunc(sysdate,'year')
复制代码
转换函数
--隐式数据类型转换(oracle自动完成转换)
> varchar2--->number
> varchar2--->date
> number--->varchar2
> date--->varchar2
--显式数据类型转换
--to_char函数对日期的转换(to_char(date,format_model))
SELECT
TO_CHAR (
SYSDATE,
'yyyy-mm-dd hh24:mi:ss'
)
FROM
dual;
--to_date函数对字符的转换(to_date(char,format_model))
--格式必须匹配
SELECT
TO_DATE (
'2018年10月26日08:10:21',
'yyyy"年"mm"月"dd"日"hh24:mi:ss'
)
FROM
dual;
--to_char函数对数字的转换(to_char(number,format_model))
--很少会用到
--to_number函数对字符的转换(to_number(char,format_model))
--很少会用到
复制代码
通用函数
SELECT
last_name,
salary,
NVL (commission_pct, 0),
(salary * 12) + (
salary * 12 * NVL (commission_pct, 0)
) an_sal
FROM
employees;
SELECT
last_name,
salary,
commission_pct,
NVL2 (
commission_pct,
'sal+comm',
'sal'
) income
FROM
employees
WHERE
department_id IN (50, 80);
SELECT
first_name,
LENGTH (first_name) "expr1",
last_name,
LENGTH (last_name) "expr2",
NULLIF (
LENGTH (first_name),
LENGTH (last_name)
) result
FROM
employees;
SELECT
last_name,
COALESCE (commission_pct, salary, 10) comm
FROM
employees
ORDER BY
commission_pct;
复制代码
条件表达式
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;
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;
复制代码
嵌套函数
单行函数可以嵌套
嵌套函数的执行顺序是由内到外
SELECT
last_name,
NVL (
TO_CHAR (manager_id),
'No Manager'
)
FROM
employees
WHERE
manager_id IS NULL;
复制代码
多表查询
- 在where子句中写入连接条件
- 在表中有相同列时,在列名之前加上表名前缀
等值连接
SELECT
E .employee_id,
E .last_name,
E .department_id,
D .department_id,
D .location_id
FROM
employees E,
departments D
WHERE
E .department_id = D .department_id;
复制代码
非等值连接
SELECT
E .last_name,
E .salary,
j.grade_level
FROM
employees E,
job_grades j
WHERE
E .salary BETWEEN j.lowest_sal
AND j.highest_sal;
复制代码
外连接
SELECT
E .last_name,
E .department_id,
D .department_name
FROM
employees E,
departments D
WHERE
E .department_id = D .department_id (+);
SELECT
E .last_name,
E .department_id,
D .department_name
FROM
employees E,
departments D
WHERE
E .department_id (+) = D .department_id;
复制代码
自连接
SELECT
M .*
FROM
employees E,
employees M
WHERE
M .employee_id = E .manager_id
AND LOWER (E .last_name) = 'chen';
SELECT
*
FROM
EMPLOYEES
WHERE
EMPLOYEE_ID = (
SELECT
MANAGER_ID
FROM
EMPLOYEES
WHERE
LOWER (LAST_NAME) = 'chen'
)
复制代码
使用on子句创建连接(常用)
SELECT
E .employee_id,
E .last_name,
E .department_id,
D .department_id,
D .location_id
FROM
employees E
INNER JOIN departments D ON E .department_id = D .department_id;
SELECT
E .employee_id,
l.city,
D .department_name
FROM
employees E
INNER JOIN departments D ON E .department_id = D .department_id
INNER JOIN locations l ON D .location_id = l.location_id;
SELECT
E .last_name,
E .employee_id,
D .department_name
FROM
employees E
LEFT OUTER JOIN departments D ON E .department_id = D .department_id;
SELECT
E .last_name,
E .employee_id,
D .department_name
FROM
employees E
RIGHT OUTER JOIN departments D ON E .department_id = D .department_id;
SELECT
E .last_name,
D .department_id,
D .department_name
FROM
employees E
FULL OUTER JOIN departments D ON E .department_id = D .department_id;
复制代码
分组函数
SELECT
AVG (salary),
SUM (salary)
FROM
employees
WHERE
job_id LIKE '%REP%';
SELECT
MIN (hire_date),
MAX (hire_date)
FROM
employees;
SELECT
COUNT (*)
FROM
employees
WHERE
department_id = 50;
SELECT
COUNT (COMMISSION_PCT)
FROM
employees;
复制代码
组函数忽略空值
SELECT
AVG (COMMISSION_PCT),
COUNT (commission_pct),
SUM (commission_pct),
SUM (commission_pct) / COUNT (*),
SUM (commission_pct) / COUNT (commission_pct)
FROM
employees;
复制代码
在组函数中使用nvl函数
SELECT
AVG (NVL(commission_pct, 0))
FROM
employees;
复制代码
distinct关键字
SELECT
COUNT (DISTINCT department_id)
FROM
employees;
复制代码
group by子句语法
SELECT
department_id,
AVG (salary)
FROM
employees
GROUP BY
department_id;
SELECT
AVG (salary)
FROM
employees
GROUP BY
department_id;
SELECT
department_id dept_id,
job_id,
SUM (salary)
FROM
employees
GROUP BY
department_id,
job_id;
复制代码
过滤分组:having子句
SELECT
department_id,
MAX (salary)
FROM
employees
GROUP BY
department_id
HAVING
MAX (salary) > 10000;
复制代码
嵌套组函数
SELECT
MAX (AVG(salary))
FROM
employees
GROUP BY
department_id;
复制代码
子查询
SELECT
last_name
FROM
employees
WHERE
salary > (
SELECT
salary
FROM
employees
WHERE
last_name = 'Abel'
);
SELECT
last_name,
job_id,
salary
FROM
employees
WHERE
job_id = (
SELECT
job_id
FROM
employees
WHERE
employee_id = 141
)
AND salary > (
SELECT
salary
FROM
employees
WHERE
employee_id = 143
);
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
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 < ALL (
SELECT
salary
FROM
employees
WHERE
job_id = 'IT_PROG'
)
AND job_id <> 'IT_PROG';
复制代码
子查询中的空值问题
SELECT
last_name,
job_id
FROM
employees
WHERE
job_id = (
SELECT
job_id
FROM
employees
WHERE
last_name = 'Haas'
);
复制代码
SELECT
last_name,
hire_date
FROM
employees
WHERE
hire_date = LAST_DAY (hire_date) - 1
复制代码
- 2、查询出 last_name 为 'Chen' 的 manager 的信息.
SELECT
*
FROM
EMPLOYEES
WHERE
EMPLOYEE_ID = (
SELECT
MANAGER_ID
FROM
EMPLOYEES
WHERE
LOWER (LAST_NAME) = 'chen'
)
复制代码
- 3、查询平均工资高于 8000 的部门 id 和它的平均工资
SELECT
department_id,
AVG (salary)
FROM
EMPLOYEES
GROUP BY
DEPARTMENT_ID
HAVING
AVG (salary) > 8000
复制代码
- 4、查询工资最低的员工信息: last_name, salary
SELECT
last_name,
salary
FROM
employees
WHERE
SALARY = (
SELECT
MIN (salary)
FROM
EMPLOYEES
);
复制代码
SELECT
*
FROM
DEPARTMENTS
WHERE
DEPARTMENT_ID = (
SELECT
DEPARTMENT_ID
FROM
EMPLOYEES
GROUP BY
DEPARTMENT_ID
HAVING
AVG (salary) = (
SELECT
MIN (AVG(salary))
FROM
EMPLOYEES
GROUP BY
department_id
)
)
复制代码
SELECT
D .*, (
SELECT
AVG (salary)
FROM
EMPLOYEES E
WHERE
E .DEPARTMENT_ID = D .DEPARTMENT_ID
)
FROM
DEPARTMENTS D
WHERE
D .DEPARTMENT_ID = (
SELECT
DEPARTMENT_ID
FROM
EMPLOYEES
GROUP BY
DEPARTMENT_ID
HAVING
AVG (salary) = (
SELECT
MIN (AVG(salary))
FROM
EMPLOYEES
GROUP BY
DEPARTMENT_ID
)
)
复制代码
SELECT
*
FROM
JOBS
WHERE
job_id = (
SELECT
JOB_ID
FROM
EMPLOYEES
GROUP BY
JOB_ID
HAVING
AVG (SALARY) = (
SELECT
MAX (AVG(salary))
FROM
EMPLOYEES
GROUP BY
job_id
)
)
复制代码
SELECT
department_id
FROM
EMPLOYEES
GROUP BY
DEPARTMENT_ID
HAVING
AVG (salary) > (
SELECT
AVG (salary)
FROM
EMPLOYEES
)
复制代码
SELECT
*
FROM
EMPLOYEES
WHERE
EMPLOYEE_ID IN (
SELECT DISTINCT
manager_id
FROM
EMPLOYEES
);
复制代码
- 10、各个部门中 最高工资中最低的那个部门的 最低工资是多少
SELECT
MIN (salary)
FROM
EMPLOYEES
WHERE
DEPARTMENT_ID = (
SELECT
DEPARTMENT_ID
FROM
EMPLOYEES
GROUP BY
DEPARTMENT_ID
HAVING
MAX (SALARY) = (
SELECT
MIN (MAX(salary))
FROM
EMPLOYEES
GROUP BY
DEPARTMENT_ID
)
)
复制代码
- 11、查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
SELECT
last_name,
DEPARTMENT_id,
email,
salary
FROM
EMPLOYEES
WHERE
EMPLOYEE_ID IN (
SELECT
MANAGER_ID
FROM
EMPLOYEES
WHERE
DEPARTMENT_ID = (
SELECT
DEPARTMENT_ID
FROM
EMPLOYEES
GROUP BY
DEPARTMENT_ID
HAVING
AVG (salary) = (
SELECT
MAX (AVG(salary))
FROM
EMPLOYEES
GROUP BY
DEPARTMENT_ID
)
)
)
复制代码
- 12、查询 1999 年来公司的人所有员工的最高工资的那个员工的信息
SELECT
*
FROM
EMPLOYEES
WHERE
TO_CHAR (HIRE_DATE, 'yyyy') = '1999'
AND SALARY = (
SELECT
MAX (salary)
FROM
EMPLOYEES
WHERE
TO_CHAR (HIRE_DATE, 'yyyy') = '1999'
)
复制代码
- 13、返回其它部门中比 job_id 为‘IT_PROG’部门所有工资都低的员工的 员工号、姓名、job_id 以及 salary
SELECT
employee_id,
last_name,
job_id,
salary
FROM
employees
WHERE
salary < ALL (
SELECT
salary
FROM
employees
WHERE
LOWER (JOB_ID) = 'it_prog'
)
AND LOWER (JOB_ID) <> 'it_prog';
复制代码