SELECT * FROM emp_test;
SELECT * FROM dept_test;
SELECT salary*0.1234567 "原樣輸出",ROUND(salary*0.1234567) "默認零位小數", ROUND(salary*0.1234567,2) "保留兩位小數" FROM emp_test;
SELECT salary*0.1234567 "原樣輸出",TRUNC(salary*0.1234567,2) "直接截取留兩位小數" FROM emp_test;
SELECT name,hire_date,(SYSDATE-hire_date)DAYS FROM emp_test;
SELECT name, ROUND(MONTHS_BETWEEN(SYSDATE,hire_date)) FROM emp_test;
SELECT ADD_MONTHS(SYSDATE,-12) FROM DUAL;
SELECT LAST_DAY(SYSDATE) FROM DUAL;
SELECT to_char(SYSDATE , 'yyyy-mm-dd hh24:mi:ss') from DUAL;
INSERT INTO emp_test(emp_id,name,hire_date) VALUES (1012,'amy',SYSDATE);
SELECT * FROM emp_test;
INSERT INTO emp_test(emp_id,name,hire_date) VALUES (1012,'amy',TO_DATE('2011-10-10','yyyy-mm-dd'));
SELECT * FROM emp_test;
SELECT name,TO_CHAR(hire_date,'yyyy-mm-dd') FROM emp_test;
SELECT COALESCE(bonus,salary*0.5,100) FROM emp_test;
SELECT name,job,salary ,
CASE job WHEN 'Analyst' THEN salary*1.1
WHEN 'Programmer' THEN salary*1.05
WHEN 'clerk' THEN salary*1.02
ELSE salary
END new_salary
FROM emp_test;
SELECT name,job,salary ,
DECODE(job,'Analyst',salary*1.1,'Programmer',salary*1.05,'clerk',salary*1.02,salary) new_salary
FROM emp_test;
SELECT name,salary FROM emp_test ORDER BY salary ASC;
SELECT name,salary FROM emp_test ORDER BY salary DESC;
SELECT name,hire_date FROM emp_test ORDER BY hire_date ASC;
SELECT name,dept_test_id,salary FROM emp_test ORDER BY dept_test_id,salary;
SELECT COUNT(*) FROM emp_test;
select count(*) from user_tables
SELECT COUNT(hire_date) FROM emp_test WHERE hire_date IS NOT NULL;
SELECT SUM(salary) FROM emp_test;
SELECT COUNT(*), SUM(salary),AVG(salary) FROM emp_test;
SELECT COUNT(*), SUM(salary),AVG(NVL(salary,0)) FROM emp_test;
SELECT max(salary),min(salary) FROM emp_test;
SELECT dept_test_id,max(salary),min(salary) FROM emp_test GROUP BY dept_test_id;
SELECT dept_test_id,SUM(salary),AVG(NVL(salary,0)) FROM emp_test GROUP BY dept_test_id;
SELECT dept_test_id deptno,
max(salary) max_s,
min(salary) min_s,
SUM(salary) sum_s,
AVG(NVL(salary,0)) avg_s,
COUNT(*) emp_num
FROM emp_test GROUP BY dept_test_id;
SELECT MAX(salary),MIN(salary),COUNT(*) emp_num FROM emp_test GROUP BY job order by emp_num;
SELECT dept_test_id, AVG(NVL(salary,0)) avg_salary FROM emp_test WHERE dept_test_id IS NOT NULL GROUP BY dept_test_id HAVING AVG(NVL(salary,0))>5000;
SELECT dept_test_id, SUM(salary) FROM emp_test WHERE dept_test_id IS NOT NULL GROUP BY dept_test_id HAVING SUM(salary)> 20000;
SELECT job,COUNT(*) FROM emp_test GROUP BY job HAVING COUNT(*)>2;
SELECT * FROM emp_test WHERE salary=(SELECT MAX(salary) FROM emp_test);複製代碼