再复杂的SQL语句也不过是简单语句的堆砌而已,因此请反复练习下面的“砖块”并不断尝试组装复杂的SQL语句,多来几遍,SQL大神就是你!!!
SELECT * FROM dept;
SELECT * FROM dept d;
SELECT ROWID,deptno,dname FROM dept;
SELECT deptno*100 FROM emp;
SELECT e.empno 员工编号,e.ename 员工名字 FROM emp e;
SELECT DISTINCT job FROM emp e;
SELECT emp.ename 员工名字,emp.sal+NVL(emp.comm,0) 员工总工资 FROM emp;
SELECT e.empno||e.ename 编号和名字 FROM emp e;
SELECT CONCAT(e.empno,e.ename) FROM emp e;
SELECT * FROM emp e WHERE sal >2000;
SELECT * FROM emp e WHERE sal <2000;
SELECT * FROM emp e WHERE sal = ANY(800,1600);
SELECT * FROM emp e WHERE sal <> ALL(800,1600);
SELECT * FROM emp e WHERE ename LIKE 'B%';
SELECT * FROM emp e WHERE ename LIKE 'B\%' ESCAPE '\';
SELECT * FROM emp e WHERE sal IN (800,1600);
SELECT * FROM emp e WHERE sal NOT IN (800,1600);
SELECT * FROM emp e WHERE sal BETWEEN 800 AND 1600;
SELECT * FROM emp e WHERE sal NOT BETWEEN 800 AND 1600;
SELECT * FROM emp e WHERE comm IS NULL;
SELECT * FROM emp e WHERE comm IS NOT NULL;
SELECT * FROM emp e WHERE sal > 1000 AND sal <2000;
SELECT * FROM emp e WHERE sal < 1000 OR sal > 2000;
SELECT * FROM emp e WHERE NOT sal = 5000;
SELECT deptno FROM emp e GROUP BY deptno;
SELECT deptno,AVG(sal) FROM emp e GROUP BY deptno;
SELECT deptno,job FROM emp e GROUP BY deptno,job;
SELECT deptno,AVG(sal) FROM emp e GROUP BY deptno HAVING AVG(sal) > 2000;
SELECT deptno,job,AVG(sal) FROM emp e GROUP BY ROLLUP (deptno,job);
SELECT deptno,job,AVG(sal) FROM emp e GROUP BY CUBE(deptno,job);
SELECT deptno,job,AVG(sal),GROUPING(deptno),GROUPING(job) FROM emp e GROUP BY CUBE(deptno,job);
SELECT deptno,job,SUM(sal) FROM emp e GROUP BY ROLLUP((deptno,job));
SELECT deptno,job,AVG(sal) FROM emp e GROUP BY GROUPING SETS(deptno,job);
SELECT * FROM emp e ORDER BY e.sal;
SELECT * FROM emp e ORDER BY e.sal ASC;
SELECT * FROM emp e ORDER BY e.sal DESC;
SELECT * FROM emp e ORDER BY e.sal,e.empno;
SELECT * FROM emp e ORDER BY e.sal DESC,e.empno ASC;
SELECT * FROM dept d INNER JOIN emp e ON d.deptno = e.deptno;
SELECT * FROM dept d LEFT OUTER JOIN emp e ON d.deptno = e.deptno;
SELECT * FROM dept d RIGHT OUTER JOIN emp e ON d.deptno = e.deptno;
SELECT * FROM dept d FULL OUTER JOIN emp e ON d.deptno = e.deptno;
【【【【【【【【【【外连接操作符:(+),有兴趣可以了解】】】】】】】】】】
SELECT * FROM dept d NATURAL JOIN emp e;
SELECT * FROM dept d CROSS JOIN emp e;
SELECT ASCII('Z') FROM dual;
SELECT CHR(90) FROM dual;
SELECT CONCAT('bbb','aaa') FROM dual;
SELECT INITCAP('oh my god!') FROM dual;
SELECT INSTR('helloworld','l',1,2) FROM dual;
SELECT LENGTH('helloworld') FROM dual;
SELECT LOWER('Hello World') FROM dual;
SELECT UPPER('Hello World') FROM dual;
SELECT LTRIM(' hello ') FROM dual;
SELECT RTRIM('###hello##','#') FROM dual;
SELECT TRIM('#' FROM '#55##') FROM dual;
SELECT REPLACE('aassdd','aa','kkk') FROM dual;
SELECT SUBSTR('dbausbdasdiuiasdn',2,5) FROM dual;
SELECT ABS(-5) FROM dual;
SELECT CEIL(5.5) FROM dual;
SELECT COS(3) FROM dual;
SELECT EXP(3) FROM dual;
SELECT FLOOR(5.5) FROM dual;
SELECT LOG(2,4) FROM dual;
SELECT MOD(4,3) FROM dual;
SELECT POWER(2,3) FROM dual;
SELECT ROUND(5.5) FROM dual;
SELECT ROUND(5.555,2) FROM dual;
SELECT SIGN(-5) FROM dual;
SELECT SIN(2) FROM dual;
SELECT SQRT(4) from dual;
SELECT TRUNC(5.555,2) FROM dual;
SELECT SYSDATE FROM dual;
SELECT add_months(SYSDATE,2) FROM dual;
SELECT last_day(SYSDATE) FROM dual;
SELECT months_between(SYSDATE,SYSDATE) FROM dual;
SELECT new_time(SYSDATE,时区1,时区2) FROM dual;
SELECT CHARTOROWID(字符串) FROM dual;
SELECT CONVERT('s','字符集1','字符集2') FROM dual;
SELECT Rowidtochar(ROWID) FROM dual;
SELECT to_char(SYSDATE,'YYYY-MM-DD') FROM dual;
SELECT to_date('2001-05-05','YYYY-MM-DD') FROM dual;
SELECT to_number('18') FROM dual;
SELECT AVG(sal) FROM emp e;
SELECT COUNT(*) FROM emp e;
SELECT MAX(sal) FROM emp e;
SELECT MIN(sal) FROM emp e;
SELECT SUM(Sal) FROM emp e;
SELECT VARIANCE(sal) FROM emp e;
SELECT STDDEV(sal) FROM emp e;
SELECT * FROM emp e WHERE e.deptno = (SELECT deptno FROM dept WHERE dept.dname = 'RESEARCH');
SELECT * FROM emp e WHERE e.sal = (SELECT MAX(sal) FROM emp)
SELECT * FROM emp e WHERE e.empno IN (SELECT empno FROM dept WHERE dept.dname IS NOT NULL);
【子查询很强大,select、UPDATE、DELETE、insert等命令中允许使用表达式的地方都可以使用子查询】