SELECT LPAD(name,10,'*') FROM emp_test;
SELECT RPAD(name,'*') FROM emp_test;
SELECT salary,MOD(salary,5000) FROM emp_test;
SELECT name,hire_date FROM emp_test WHERE name='amy';
UPDATE emp_test SET hire_date=ADD_MONTHS(hire_date,-2) WHERE name='amy';
SELECT name,hire_date FROM emp_test WHERE name='amy';
SELECT LAST_DAY(SYSDATE) FROM DUAL;
SELECT TO_NUMBER('$7,345.67','$9,999,999.99') FROM DUAL;
SELECT name,salary FROM emp_test WHERE salary>(SELECT salary FROM emp_test WHERE name='张无忌');
SELECT DISTINCT job FROM emp_test WHERE dept_test_id=(SELECT dept_test_id FROM dept_test WHERE dept_name='developer');
insert into emp_test(emp_id,name,salary) values(1014,'张无忌',8000) ;
SELECT name,salary FROM emp_test WHERE salary>ALL(SELECT salary FROM emp_test WHERE name='张无忌');
SELECT name,salary FROM emp_test WHERE salary>ANY(SELECT salary FROM emp_test WHERE name='张无忌');
SELECT name FROM emp_test WHERE dept_test_id=(SELECT dept_test_id FROM emp_test WHERE name='刘苍松') AND name <>'刘苍松';
insert into emp_test(emp_id,dept_test_id) values(1015,'刘苍松',20) ;
SELECT name FROM emp_test WHERE dept_test_id IN(SELECT dept_test_id FROM emp_test WHERE name='刘苍松') AND name <>'刘苍松';
SELECT name FROM emp_test WHERE manager IN(SELECT emp_id FROM emp_test WHERE name='张无忌');
SELECT dept_test_id,salary FROM emp_test WHERE (dept_test_id,salary) IN (SELECT dept_test_id,MAX(salary) FROM emp_test GROUP BY dept_test_id);
SELECT dept_test_id,COUNT(*) FROM emp_test GROUP BY dept_test_id HAVING COUNT(*) > (SELECT COUNT(*) FROM emp_test WHERE dept_test_id=30);
SELECT dept_test_id,COUNT(*),AVG(salary) FROM emp_test GROUP BY dept_test_id HAVING AVG(salary) > (SELECT AVG(salary) FROM emp_test WHERE dept_test_id=30);
SELECT dept_test_id,AVG(salary) FROM emp_test GROUP BY dept_test_id HAVING AVG(salary)>5000;
SELECT name,salary FROM emp_test WHERE salary<(SELECT AVG(salary) FROM emp_test);
SELECT name,salary FROM emp_test WHERE salary<(SELECT AVG(salary) FROM emp_test WHERE dept_test_id=emp_test.dept_test_id);
SELECT name FROM emp_test a WHERE EXISTS(SELECT manager FROM emp_test WHERE manager=a.emp_id);
SELECT name FROM emp_test WHERE emp_id IN (SELECT DISTINCT manager FROM emp_test);
SELECT name FROM emp_test a WHERE NOT EXISTS(SELECT manager FROM emp_test WHERE manager=a.emp_id);
SELECT name FROM emp_test WHERE emp_id NOT IN (SELECT DISTINCT manager FROM emp_test WHERE manager IS NOT NULL );
SELECT * FROM dept_test a WHERE NOT EXISTS (SELECT 1 FROM emp_test WHERE dept_test_id=a.dept_id);
select name,salary from emp_test where dept_test_id= 10 union select name,salary from emp_test where salary > 6000 select name,salary from emp_test where dept_test_id= 10 union all select name,salary from emp_test where dept_test_id= 10 intersect select name,salary from emp_test where dept_test_id= 10 minus select name,salary from emp_test where salary > 6000 -- 主键( PK )和外键( FK ) -- 1) 主键( Primary key,简称 PK ) --主键要求丌重复,丌能是空值 -- dept_xxx 表的主键: 部门编码( deptno ) -- emp_xxx 的主键: 职员编码( empno ) -- 2) 外键( Foreign key,简称 FK ) --外键参照主键的数据 -- emp_xxx 的所在部门( deptno )是外键,参照 dept_xxx 的主键 -- emp_xxx 的经理( mgr )列是外键,参照 emp_xxx 的主键 -- 列出员工的姓名和所在部门的名字和城市 SELECT name,dept_name,dept_location FROM emp_test,dept_test WHERE emp_test.dept_test_id=dept_test.dept_id;
SELECT name,dept_location FROM emp_test JOIN dept_test ON emp_test.dept_test_id=dept_test.dept_id;
SELECT a.name,b.name manager FROM emp_test a,emp_test b WHERE a.manager=b.emp_id;
SELECT a.name,b.name manager FROM emp_test a JOIN emp_test b ON a.manager=b.emp_id;
SELECT a.name,b.dept_name FROM emp_test a LEFT JOIN dept_test b ON a.dept_test_id=b.dept_id;
SELECT a.name,b.dept_name FROM emp_test a RIGHT JOIN dept_test b ON a.dept_test_id=b.dept_id;
SELECT a.dept_test_id,b.dept_name FROM emp_test a RIGHT JOIN dept_test b ON a.dept_test_id=b.dept_id WHERE a.dept_test_id IS NULL;
SELECT a.name,b.dept_name FROM emp_test a FULL OUTER JOIN dept_test b ON a.dept_test_id=b.dept_id;