开发工具与关键技术:PLSQL Developer、Oracle、SQL*plus
一.在查询中过滤行,使用where 子句,将不满足条件的行过滤掉。
• Where 子句紧随 From 子句。
• 字符和日期要包含在单引号中。
• 字符大小写敏感,日期格式敏感。
• 默认的日期格式是 dd-mon月-yy。
举例如下:
- 在employees表中查询last_name为“King”的员工的名字,入厂日期,部门号:
正确:select last_name,hire_date,department_id from employees
where last_name='King’;
错误:select last_name,hire_date,department_id from employees
where last_name=‘king’;
正确代码执行结果:
- 在employees表中查询入厂日期为1987年6月17日的员工的名字,入厂日期,部门号。
正确:select last_name,hire_date,department_id from employees
where hire_date=‘17-6月-1987’;
错误:select last_name,hire_date,department_id from employees
where hire_date=‘1987/6/17’;
正确代码执行结果:
比较运算
使用” >”比较和过滤数据:
在score表中查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录:
select sno as 学号,cno as 课程号,degree as 成绩 from score
where cno='3-105’and degree > all (select degree from score where sno=109)
代码执行结果:
使用” <>”比较和过滤数据:
在score表中查询没有选修“3-105”课程的所有同学的记录:
select sno as 学号,cno as 课程号,degree as 成绩 from score
where cno <>'3-105’
代码执行结果:
其它比较运算
• 使用 LIKE 运算选择类似的值
• 选择条件可以包含字符或数字:
% 代表零个或多个字符(任意个字符)。
_ 代表一个字符。
举例如下:
1.在employees表中查询出工资在5000到8000,并且名字里有“a”字母的员工的名字和工资。
select last_name,salary from employees
where salary between 5000 and 8000 and last_name like ‘%a%’;
代码执行结果:
2.在employees表中查询部门号为20,30,100,并且工资大于4000的员工的名字,工资和部门号。
select last_name,salary,department_id from employees
where salary > 4000 and department_id in (20,30,100);
代码执行结果:
逻辑运算
二.Order by 子句
• 使用 ORDER BY 子句排序
– ASC(ascend): 升序
– DESC(descend): 降序
• ORDER BY 子句在SELECT语句的结尾。
举例如下:
1. 在employees表中查询根据部门号由高而低,工资由低而高列出每个员工的姓名,部门号,工资。
select first_name||’.’||last_name as Name,department_id,salary
from employees
where department_id is not null
order by department_id desc,salary asc;
代码执行结果: