1. Call Notepad .
If you write the statement now, it is very difficult to do it directly in the command line mode. When there is a graphical interface, it is easy to write such a statement, but in many cases without a graphical interface, it is tiring to write directly, so you can use the " ed " command to start the local notepad program.
Type: " ed hello ".
Set the length of data displayed in each line: SET LINESIZE 300 ;
Set the number of lines displayed each time: SET PAGESIZE 30 ;
Query all data tables of a user : select * from tab;
Query table structure : desc table name ;
DML ( Data Manipulation Language): Data update and query operations ( SELECT , FROM , INSERT , GROUP BY , HAVING , WHERE , UPDATE , DELETE ) are mostly DML operations in development.
· DDL (Data Definition Language): The definition language of database objects, such as: data tables, constraints, indexes, synonyms, users; generally must be mastered when designing a database.
DCL (Database Control Language): Permission Control for Databases
2. Relational operations: > , = , < , >= , <= , != ( < > ) ;
· Range operation: BETWEEN...AND ;
· Null judgment: IS NULL , IS NOT NULL ;
· IN judgment: IN , NOT IN , exists() (complex query);
· Fuzzy query: LIKE , NOT LIKE .
The above qualifiers can only be judged once. If there are several qualifiers, then several qualifiers need to be connected, and logical operations can be used: AND (and), OR (or), NOT (not);
Returns true when all the judgment conditions expressed by the AND operation are satisfied ( true );
· The OR operation means that as long as one of several judgment conditions is satisfied, it returns true ( true ).
3. Fuzzy query : LIKE
LIKE can realize the fuzzy query operation of data. If you want to use LIKE , you must use the following two matching symbols:
" _ ": matches any one-bit symbol;
· " % ": matches any symbol (including matching 0 -bit, 1 -bit, and multiple digits).
The execution of the WHERE clause takes precedence over the SELECT clause, the WHERE clause determines the data row, and the SELECT clause determines the data column;
Link database : sqlplus scott/tiger
1. Query all content : emp table
select * from emp;
;
2. Query the number , name, position and basic salary of each employee
select empno,ename,job from emp;
3. Find out the number , name, and basic annual salary of each employee (monthly salary is sal , annual salary *12 )
select empno,ename,sal*12 income from emp;
4. Use " || " to connect
select ' Number :' || empno || ' Name :' || ename from emp;
5. Use distinct to eliminate duplicate content
select distinct job from emp;
select distinct ename,job from emp;
6. Query employees whose salary is less than 1200 (excluding 1200 )
select * from emp where sal < 1200;
7. Query the employees whose salary is 3000
select * from emp where sal = 3000;
8、查询职位不是办事员的雇员(职位是job字段,办事员的职位名称CLERK)
select * from emp where job<>'CLERK' ;
select * from emp where job!='CLERK' ;
9、要求查询出不是办事员,但是工资低于3000的雇员信息
· 第一个条件(不是办事员):job<>'CLERK';
· 第二个条件(工资低于3000):sal<3000。
select * from emp wherejob<>'CLERK' AND sal<3000 ;
10、查询出职位不是办事员也不是销售的雇员信息
· 第一个条件:job<>'CLERK';
· 第二个条件:job<>'SALESMAN'。
select * from emp where job<>'CLERK' and job<>'SALESMAN';
11、查询出职位是办事员,或者工资低于1200的所有雇员
· 第一个条件:job='CLERK';
· 第二个条件:sal<1200。
两个条件满足一个即可,使用OR连接。
select * from emp where job='CLERK' or sal<1200;
除了AND与OR之外,还可以使用NOT进行求反。即:true变为false、false变为true
此时表示的是工资小与等于2000
select * from emp where not sal<=2000;
12、查询出工资在1500 ~ 3000之间的所有雇员, 使用BETWEEN...AND查询;
select * from emp where sal between 1500 and 3000;
13、查询出所有在1981年雇佣的雇员信息
· 范围:1981-01-01('01-1月-81') ~ 1981-12-31('31-12月-1981')
select * from emp where hiredate between '01-1月-1981' and '31-12月-1981';
14、查询所有领取佣金不为空的雇员信息
select * from emp where comm is not null;
15、IN操作符(谓词IN,核心)查询出雇员编号是7369或7566或7788或9999的雇员信息
select * from emp where empno=7369 or empno=7566 or empno=9999;
select * from emp where empno in(7369,7566,9999);
用NOT IN,那么这个表示的是不在范围之中,NOT IN里面不能有null;
select * from emp where empno not in(7369,7566,9999);
16、查询所有雇员姓名中以字母A开头的雇员信息
select * from emp where ename like 'A%';
17、查询所有雇员姓名中第二个字母是A的所有雇员
select * from emp where ename like '_A%';
18、查询雇员姓名中任意位置上存在有字母A的雇员信息
select * from emp where ename like '%A%';
19、查询全部
select * from emp where ename like '%%';
20、LIKE可以在任意的数据类型上使用
select * from emp where ename like '%A%' or sal like '%1%' or hiredate like '%81%';
【③ 选出所需要的数据列】SELECT [DISTINCT] * | 列 [别名],列 [别名],列 [别名]...
【① 确定数据来源】FROM 表名称 [别名]
【② 筛选数据行】[WHERE 限定条件(s)]
【④ 数据排序】[ORDER BY 排序字段 [ASC | DESC] , 排序字段 [ASC | DESC] , ...];
既然ORDER BY是在SELECT子句之后执行,那么就意味着ORDER BY可以使用SELECT子句定义的别名。
但是对于字段排序有两种形式:
· 升序:ASC,默认不写排序也是升序;
· 降序:DESC,由高到低进行排序。
1、 SELECT子句确定数据列;
2、 WHERE子句控制数据行;
3、 ORDER BY子句永远最后执行。
1、 按照工资由高到低排序,此时应该使用的是一个降序排序
select * from emp order by sal desc;
2、 按照雇佣日期由早到晚排序
select * from emp order by hiredate asc;
select * from emp order by hiredate;
3、 按照工资由高到低排序(降序),如果工资相同,则按照雇佣日期由早到晚排序(升序)
select * from emp order by sal desc,hiredate;
4、 所有的排序操作都是在WHERE筛选之后进行的
查询出所有办事员的编号、职位、年薪,按照年薪由高到低排序
办事员:job=’CLERK’;
select empno,job,sal*12 年薪 from emp where job='CLERK' order by 年薪;
5、 选择部门30中的所有员工,限定查询使用where子句,部门编号deptno
select * from emp where deptno=30;
6、 列出所有办事员(CLERK)的姓名,编号和部门编号,限定查询使用where
select ename,empno,deptno from emp where job='CLERK';
7、 找出佣金高于薪金的60%的员工,佣金使用的是comm字段,而薪金是sal,而且comm本身包含有null,null进行任何数学计算结果都是null
select * from emp where comm>sal*0.6;
8、 找出部门10中所有经理(MANAGER)或部门20中所有办事员(CLERK)的详细资料
select * from emp where (deptno=10 and job='MANAGER') or (deptno=20 and job='CLERK');
9、 找出部门10中所有经理(MANAGER),部门20中所有办事员(CLERK),既不是经理又不是办事员但其薪金大于或等于2000的所有员工的详细资料
第一组条件(部门10中所有经理):deptno=10 AND job='MANAGER';
第二组条件(部门20中所有办事员):deptno=20 AND job='CLERK'。
第三组条件(不是经理又不是办事员但其薪金大于或等于2000):job NOT IN('MANAGER','CLERK') AND sal>=2000
select * from emp where (deptno=10 and job='MANAGER') or (deptno=20 and job='CLERK') or (job not in('MANAGER','CLERK') and sal>=2000);
10、 找出收取佣金的员工的不同工作, 销售具备佣金,但是现在要的是工作,工作就会有重复数据
select distinct job from emp where comm is not null;
11、 找出不收取佣金或收取的佣金低于100的员工
对于null的判断使用IS NULL或IS NOT NULL
select distinct job from emp where comm is null or comm<100;
12、 显示不带有“R”的员工的姓名
select * from emp where ename not like '%R%';
13、 显示姓名字段的任何位置包含“A”的所有员工的姓名,显示的结果按照基本工资由高到低排序,如果基本工资相同,则按照雇佣年限由早到晚排序,如果雇佣日期相同,则按照职位排序
select * from emp where ename like '%A%' order by sal desc,hiredate,job;