--1、查询数据
--查询所有的数据
SELECT * FROM EMP T;
--查询指定列的数据(最后一个指定列与FROM不需要用逗号隔开)
SELECT T.EMPNO,T.JOB,T.SAL FROM EMP T;
--查询去重复的数据(关键字DISTINC是去除相同列中重复的字段)
--去重只能在同一个列中进行
SELECT DISTINCT T.MGR FROM EMP T;
SELECT DISTINCT T.SAL FROM EMP T;
--2、查询数据中的使用+-*/表达式,包含数字和日期;
--数字
SELECT T.EMPNO, T.MGR , T.EMPNO+T.MGR ADDNUM FROM EMP T;
SELECT T.EMPNO, T.MGR , T.EMPNO-T.MGR ADDNUM FROM EMP T;
SELECT T.EMPNO, T.MGR , T.EMPNO*T.MGR ADDNUM FROM EMP T;
SELECT T.EMPNO, T.MGR , T.EMPNO/T.MGR ADDNUM FROM EMP T;
--日期(只能参与加减运算,都是在日上面进行叠加或叠减到月和年)
SELECT T.ENAME,T.HIREDATE,T.HIREDATE+6 FROM EMP T;
SELECT T.ENAME,T.HIREDATE,T.HIREDATE+6 FROM EMP T;
--指定日期的格式(使用TO_CHAR()方法)
SELECT T.HIREDATE,TO_CHAR(T.HIREDATE+6,'yyyy-mm-dd') NOWDATE FROM EMP T;
--3、查询数据中处理空值(使用NVL()函数)
SELECT NVL(T.MGR,0),NVL(T.SAL,0),NVL(T.MGR,0)+NVL(T.SAL,0) NOWADD FROM EMP T;
--4、使用排序查询,包含升序和降序(排序子句ORDER BY)
--排序规则使用列名,别名,表达式或序号
--升序(ACS关键字 此时为自动)和降序(DESC关键字)
--列名
SELECT T.EMPNO,T.MGR FROM EMP T ORDER BY T.EMPNO ASC;
SELECT T.EMPNO,T.MGR FROM EMP T ORDER BY T.EMPNO ;
SELECT T.EMPNO,T.MGR FROM EMP T ORDER BY T.EMPNO DESC;
--别名
SELECT T.SAL GONGZI FROM EMP T ORDER BY GONGZI ASC;
SELECT T.COMM JIANGJIN FROM EMP T ORDER BY JIANGJIN ASC;
SELECT T.SAL GONGZI FROM EMP T ORDER BY GONGZI DESC;
SELECT T.DEPTNO JIANG FROM EMP T ORDER BY JIANG DESC;
SELECT T.COMM JI FROM EMP T ORDER BY JI DESC;
--表达式
SELECT T.EMPNO,T.MGR,T.EMPNO+T.MGR HE FROM EMP T ORDER BY HE ASC;
SELECT T.EMPNO,T.MGR,T.EMPNO+T.MGR HE FROM EMP T ORDER BY HE DESC;
--序号
SELECT T.EMPNO,T.MGR,T.EMPNO+T.MGR HE FROM EMP T ORDER BY 1;
SELECT T.EMPNO,T.MGR,T.EMPNO+T.MGR HE FROM EMP T ORDER BY 1 DESC;
--5、where条件查询:
--使用 条件= > >= < <= != <> ^=
--字符串的精确查找以首字母为对象
SELECT * FROM EMP T WHERE T.EMPNO=7521;
SELECT * FROM EMP T WHERE T.ENAME='ALLEN';
SELECT * FROM EMP T WHERE T.SAL>1000;
SELECT * FROM EMP T WHERE T.JOB>'A';
SELECT * FROM EMP T WHERE T.SAL>=1000;
SELECT * FROM EMP T WHERE T.JOB>='A';
SELECT * FROM EMP T WHERE T.SAL<1000;
SELECT * FROM EMP T WHERE T.JOB<'A';
SELECT * FROM EMP T WHERE T.SAL<=1000;
SELECT * FROM EMP T WHERE T.JOB<='A';
--三个都是不等于
SELECT * FROM EMP T WHERE T.SAL!=1000;
SELECT * FROM EMP T WHERE T.JOB<>'A';
SELECT * FROM EMP T WHERE T.SAL^=1000;
--BETWEEN ... AND...
SELECT * FROM EMP T WHERE T.SAL BETWEEN 1000 AND 2000;
SELECT * FROM EMP T WHERE T.JOB BETWEEN 'A' AND 'J';
--IN(list)(使用的个数有限)
--此处是精确查询,类似于java中的switch(){case1:}
--字符串必须要用单引号括起来;
SELECT * FROM EMP T WHERE T.SAL IN(800,950,3000);
SELECT * FROM EMP T WHERE T.JOB IN('CLERK','SALESMAN');
--LIKE
SELECT * FROM EMP T WHERE T.ENAME LIKE 'S%'; --以S开头的
SELECT * FROM EMP T WHERE T.ENAME LIKE '%S';
SELECT * FROM EMP T WHERE T.JOB LIKE '__S%';
SELECT * FROM EMP T WHERE T.ENAME LIKE '%S%';
--IS NULL
SELECT * FROM EMP T WHERE T.SAL IS NULL;
--NOT BETWEEN ... AND...
SELECT * FROM EMP T WHERE T.SAL BETWEEN 1000 AND 2000;
SELECT * FROM EMP T WHERE T.JOB BETWEEN 'A' AND 'J';
--NOT IN(list)
SELECT * FROM EMP T WHERE T.SAL NOT IN(800,950,3000);
SELECT * FROM EMP T WHERE T.JOB NOT IN('CLERK','SALESMAN');
-- NOT LIKE
SELECT * FROM EMP T WHERE T.ENAME NOT LIKE 'S%';
SELECT * FROM EMP T WHERE T.ENAME NOT LIKE '%S';
SELECT * FROM EMP T WHERE T.JOB NOT LIKE '__S%';
SELECT * FROM EMP T WHERE T.ENAME NOT LIKE '%S%';
-- NOT IS NULL
SELECT * FROM EMP T WHERE T.SAL IS NOT NULL;