0. Query all data (the easiest, but take a long time)
select * from emp;
Result:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7369 SMITH CLERK 7902 1980/12/17 800.00 20
7499 ALLEN SALESMAN 7698 1981/2/20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981/2/22 1250.00 500.00 30
7566 JONES MANAGER 7839 1981/4/2 2975.00 20
7654 MARTIN SALESMAN 7698 1981/9/28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981/5/1 2850.00 30
7782 CLARK MANAGER 7839 1981/6/9 2450.00 10
7788 SCOTT ANALYST 7566 1987/4/19 3000.00 20
7839 KING PRESIDENT 1981/11/17 5000.00 10
7844 TURNER SALESMAN 7698 1981/9/8 1500.00 0.00 30
7876 ADAMS CLERK 7788 1987/5/23 1100.00 20
7900 JAMES CLERK 7698 1981/12/3 950.00 30
7902 FORD ANALYST 7566 1981/12/3 3000.00 20
7934 MILLER CLERK 7782 1982/1/23 1300.00 10
1. Query the system time (this table is not established in the user's database, so the virtual table dual is used)
select sysdate from dual;
Result:
SYSDATE
-----------
2018/5/9 20
2. Remove duplicate query data
select distinct(deptno) from emp;
Result:
DEPTNO
------
30
20
10
3. Query the database to display aliases
select ename "姓名" ,sal*12 "年薪" from emp;
Result
姓名 年薪
---------- ----------
SMITH 9600
ALLEN 19200
WARD 15000
JONES 35700
MARTIN 15000
BLAKE 34200
CLARK 29400
SCOTT 36000
KING 60000
TURNER 18000
ADAMS 13200
JAMES 11400
FORD 36000
MILLER 15600
4. Perform operations
select 3*4+1*2 from dual;
Result:
3*4+1*2
----------
14
5. NVL functions can handle fields with null values
6. Query employee information with department number 10 (need to add where condition to filter)
select * from emp where DEPTNO=10;
Result:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7782 CLARK MANAGER 7839 1981/6/9 2450.00 10
7839 KING PRESIDENT 1981/11/17 5000.00 10
7934 MILLER CLERK 7782 1982/1/23 1300.00 10
7. Query the employee information of the department whose number is not 10
select ename,sal,deptno from emp where deptno <>10;
Result
ENAME SAL DEPTNO
---------- --------- ------
SMITH 800.00 20
ALLEN 1600.00 30
WARD 1250.00 30
JONES 2975.00 20
MARTIN 1250.00 30
BLAKE 2850.00 30
SCOTT 3000.00 20
TURNER 1500.00 30
ADAMS 1100.00 20
JAMES 950.00 30
FORD 3000.00 20
8. Range query display results
select ename,sal from emp where sal between 800 and 1500;
Result:
ENAME SAL
---------- ---------
SMITH 800.00
WARD 1250.00
MARTIN 1250.00
TURNER 1500.00
ADAMS 1100.00
JAMES 950.00
MILLER 1300.00
或者 select ename,sal from emp where sal in(800,1500,2000);
Result
ENAME SAL
---------- ---------
SMITH 800.00
TURNER 1500.00
9. Fuzzy query
select * from emp where ename like '%ALL%';
Result:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7499 ALLEN SALESMAN 7698 1981/2/20 1600.00 300.00 30
10. Query the employee information whose second letter starts with A
select * from emp where ename like '%_A%';
Result
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7521 WARD SALESMAN 7698 1981/2/22 1250.00 500.00 30
7654 MARTIN SALESMAN 7698 1981/9/28 1250.00 1400.00 30
7698 BLAKE MANAGER 7839 1981/5/1 2850.00 30
7782 CLARK MANAGER 7839 1981/6/9 2450.00 10
7876 ADAMS CLERK 7788 1987/5/23 1100.00 20
7900 JAMES CLERK 7698 1981/12/3 950.00 30
11. Query the information of employees whose salary is between 800-2000 and sort the output according to the descending order of the department
select * from emp where sal between 800 and 2000 order by deptno desc;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
----- ---------- --------- ----- ----------- --------- --------- ------
7654 MARTIN SALESMAN 7698 1981/9/28 1250.00 1400.00 30
7900 JAMES CLERK 7698 1981/12/3 950.00 30
7844 TURNER SALESMAN 7698 1981/9/8 1500.00 0.00 30
7499 ALLEN SALESMAN 7698 1981/2/20 1600.00 300.00 30
7521 WARD SALESMAN 7698 1981/2/22 1250.00 500.00 30
7876 ADAMS CLERK 7788 1987/5/23 1100.00 20
7369 SMITH CLERK 7902 1980/12/17 800.00 20
7934 MILLER CLERK 7782 1982/1/23 1300.00 10