select * from 表名;
select 字段1, 字段2 from 表名;
-- 去重
select distinct 字段名 from 表名;
-- 起别名
select 字段1 as 别名1, 字段2 as 别名2 from 表名;
-- 连接符
select 字段1 || 字段2 || 字段3 from 表名;
select employee_id id, last_name || ' ' ||first_name Name from employees;
-- 字符串
select 字段1 || is a || 字段2 as 别名1 from 表名;
-- 显示表结构
desc 表名;
07——条件查询
-- where 子句
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90
08——字符串和日期
字符串和日期要包含在单引号中。
字符串区分大小写,日期格式敏感。
默认的日期格式是 DD-MON-RR。
SELECT last_name, job_id, department_id
FROM employees
WHERE last_name = 'Whalen'
09——基本语句2
-- 比较运算符
-- 操作符: = > < >= <= <>
select 字段1,字段2 from where 条件1 < 限定值
-- 操作符:
-- between...and...:在两个值之间(包含边界)
-- in(set) :等于值列表中的一个
-- like :模糊查询
-- is null :是否为空
select 字段1,字段2 from where 条件1 between 值1 and 值2
select 字段1,字段2 from where 条件1 in (值1, 值2)
-- like 模糊查询
-- % 任意个字符
-- _ 单个字符
select * from employees where last_name like 'A%';
select * from employees where last_name like '_a%';
select * from employees where last_name not like 'A%';
-- 使用 ESCAPE 标识符 选择‘%’和 ‘_’ 符号。
select LAST_NAME from employees where LAST_NAME LIKE '_/_%' ESCAPE '/';
-- 逻辑运算符
-- and 逻辑与
-- or 逻辑或
-- not 逻辑否
10——优先级
算术运算符
连接符
比较符
is (not) null, like, (not) in
(not) between
not
and
or
SELECT last_name, job_id, salary
FROM employees
WHERE job_id = 'SA_REP'
OR job_id = 'AD_PRES'
AND salary > 15000
-- ⚫ 使用括号控制执行顺序。
SELECT last_name, job_id, salary
FROM employees
WHERE (job_id = 'SA_REP'
OR job_id = 'AD_PRES')
AND salary > 15000
11——排序
-- order by
-- 升序 降序
SELECT last_name, department_id, salary
FROM employees
ORDER BY department_id, salary DESC;