目标:
掌握SQL语句的基本语法;
可以使用SQL语句完成简单查询功能;
1.【范例】:查询emp表中的数据;
select * from emp;
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
14 rows selected
2.简单查询语句的语法:
select [distinct] * |列名称[as][别名], 列名称[as][别名],……
from 表名称[别名];
执行顺序:第一步执行from子句,确定数据来源,
第二步执行select子句,确定要显示的数据列;
3.【范例】:现在要求查询出公司的雇员雇佣情况,所以希望通过数据库可以查找到每个雇员的编号、姓名、基本工资三个信息进行浏览;
select empno,ename,job
from emp;
EMPNO ENAME JOB
------ -------------------- -----------
7369 SMITH CLERK
7499 ALLEN SALESMAN
7521 WARD SALESMAN
7566 JONES MANAGER
7654 MARTIN SALESMAN
7698 BLAKE MANAGER
7782 CLARK MANAGER
7788 SCOTT ANALYST
7839 KING PRESIDENT
7844 TURNER SALESMAN
7876 ADAMS CLERK
7900 JAMES CLERK
7902 FORD ANALYST
7934 MILLER CLERK
4.【范例】:现在要求查询出公司中所有雇员的职位信息
select job
from emp;
JOB
---------
CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER
MANAGER
ANALYST
PRESIDENT
SALESMAN
CLERK
CLERK
ANALYST
CLERK
发现有重复的内容;
select distinct job
from emp;
JOB
----------
CLERK
SALESMAN
PRESIDENT
MANAGER
ANALYST
使用distinct可以消除全部的重复内容,但是只局限于所有列的内容全部相同时,才可以。
5.【范例】:查询雇员编号,工作
select distinct empno,job
from emp;
EMPNO JOB
------ ------------
7369 CLERK
7499 SALESMAN
7521 SALESMAN
7566 MANAGER
7654 SALESMAN
7698 MANAGER
7782 MANAGER
7788 ANALYST
7839 PRESIDENT
7844 SALESMAN
7876 CLERK
7900 CLERK
7902 ANALYST
7934 CLERK
6.【范例】:要求通过数据库查询出所有雇员的编号、雇员姓名和年基本工资、日基本工资,以作为年终奖金的发送标准。
select empno,ename,sal*12,sal/30
from emp;
EMPNO ENAME SAL*12 SAL/30
---------- -------------------- ---------- ----------
7369 SMITH 9600 26.6666667
7499 ALLEN 19200 53.3333333
7521 WARD 15000 41.6666667
7566 JONES 35700 99.1666667
7654 MARTIN 15000 41.6666667
7698 BLAKE 34200 95
7782 CLARK 29400 81.6666667
7788 SCOTT 36000 100
7839 KING 60000 166.666667
7844 TURNER 18000 50
7876 ADAMS