1.清除重复行
使用DISTINCT关键字可从查询结果中清除重复行
1 SELECT DISTINCT department_id FROM employees;
DISTINCT的作用范围是后面所有字段的组合
1 SELECT DISTINCT department_id , job_id FROM employees;
2.WHERE中的字符串和日期值
字符串和日期要用单引号扩起来
字符串是大小写敏感的,日期值是格式敏感的
1 SELECT last_name, job_id, department_id 2 FROM employees 3 WHERE last_name = ‘KING';
3.WHERE中的比较运算符
1 SELECT last_name, salary, commission_pct 2 FROM employees 3 WHERE salary<=1500;
4.使用BETWEEN运算符显示某一值域范围的记录
1 SELECT last_name, salary 2 FROM employees 3 WHERE salary BETWEEN 1000 AND 1500;
5.使用IN运算符获得匹配列表值的记录
1 SELEC Temployee_id, last_name, salary, manager_id 2 FROM employees 3 WHERE manager_id IN (7902, 7566, 7788);
6.使用LIKE运算符执行模糊查询
查询条件可包含文字字符或数字, (%) 可表示零或多个字符, (_)可表示一个字符。
1 SELECT last_name 2 FROM employees 3 WHERE last_name LIKE '_A%';
7.逻辑运算符
7.1使用AND运算符
AND需要所有条件都是满足T
1 SELECT employee_id, last_name, job_id, salary 2 FROM employees 3 WHERE salary>=1100–4 AND job_id='CLERK';
7.2使用OR运算符
OR只要两个条件满足一个就可以
1 SELECT employee_id, last_name, job_id, salary 2 FROM employees 3 WHERE salary>=1100 OR job_id='CLERK';
7.3使用NOT运算符
NOT是取反的意思
1 SELECT last_name, job_id 2 FROM employees 3 WHERE job_id NOT IN ('CLERK','MANAGER','ANALYST');
7.4优先级规则
1 SELECT dept_id, salary 2 FROM employees 3 WHERE dept_id=1 4 OR dept_id=2 5 AND salary>1500;
8.排序查询 order by
参数ASC,表示按照升序进行排序
参数DESC,表示按照降序进行排序
默认情况下,按照ASC方式,升序进行排序
select * from employees order by gender,employee_id ASC; /*升序查询*/ select * from employees order by employee_id DESC; /*降序查询*/
9.子查询
1 SELECT 2 t.NAME 3 FROM 4 depts t 5 WHERE 6 t.dept_id = ( 7 SELECT 8 t1.dept_id 9 FROM 10 (select * from employees) t1 11 WHERE 12 t1.employee_id = 6 13 );