/////////////////基礎知識//////////////////
應用場景178:最簡單的select語句
SELECT * FROM Employees;
應用場景179:指定要查詢的列
COL EMP_NAME FORMAT A20
COL SEX FORMAT A10
COL TITLE FORMAT A10
SELECT Emp_name, Sex, Title FROM HRMAN.Employees;
應用場景180:使用DISTINCT關鍵字
SELECT Title FROM HRMAN.Employees;
SELECT DISTINCT Title FROM HRMAN.Employees;
應用場景181:使用ROWNUM
COL EMP_NAME FORMAT A20
COL SEX FORMAT A10
COL TITLE FORMAT A10
SELECT Emp_name, Sex, Title FROM HRMAN.Employees
WHERE ROWNUM<=3;
應用場景182:改變顯示的列標題
COL 姓名 FORMAT A20
COL 性別 FORMAT A4
COL 職務 FORMAT A10
COL 身份證 FORMAT A20
SELECT EMP_NAME AS 姓名, SEX AS 性別, TITLE AS 職務, WAGE AS 工資, IDCARD AS 身份證 FROM HRMAN.Employees;
應用場景183:設置查詢條件
COL EMP_NAME FORMAT A20
SELECT Emp_Name, Wage FROM HRMAN.Employees WHERE Wage > 3000 AND Wage < 4000;
應用場景184:在查詢條件中使用BITWEEN 關鍵字
COL EMP_NAME FORMAT A20
SELECT Emp_Name, Wage FROM HRMAN.Employees WHERE Wage BETWEEN 3000 AND 4000;
應用場景185:在查詢條件中使用IN關鍵
COL EMP_NAME FORMAT A20
COL TITLE FORMAT A20
SELECT EMP_NAME, TITLE, WAGE FROM HRMAN.Employees WHERE Emp_name IN ('張三', '李四', '王五');
應用場景186:實現模糊查詢
COL EMP_NAME FORMAT A20
COL TITLE FORMAT A20
COL IDCard FORMAT A20
SELECT EMP_NAME, TITLE, IDCard FROM HRMAN.Employees
WHERE IDCard LIKE '%ddd%';
COL EMP_NAME FORMAT A20
COL TITLE FORMAT A20
COL IDCard FORMAT A20
SELECT EMP_NAME, TITLE, IDCard FROM HRMAN.Employees
WHERE IDCard LIKE '110123_adx_';
應用場景187:排序結果集
COL EMP_NAME FORMAT A20
COL TITLE FORMAT A20
COL IDCard FORMAT A20
SELECT EMP_NAME, TITLE, IDCard FROM HRMAN.Employees
ORDER BY Emp_name;
COL EMP_NAME FORMAT A20
COL TITLE FORMAT A20
SELECT EMP_NAME, TITLE, Wage FROM HRMAN.Employees
ORDER BY Wage DESC;
應用場景188:對多列進行排序
COL EMP_NAME FORMAT A20
COL SEX FORMAT A20
SELECT EMP_NAME, Sex, Wage FROM HRMAN.Employees
ORDER BY Sex, Wage;
應用場景189:使用分組統計
COL 職務 FORMAT A10
SELECT Title AS 職務, AVG(Wage) AS 平均公資 FROM HRMAN.Employees GROUP BY Title;
COL Sex FORMAT A10
COL Title FORMAT A10
SELECT Sex, Title, AVG(Wage) FROM HRMAN.Employees GROUP BY Title;
SELECT Dep_Id, AVG(Wage) FROM HRMAN.Employees
GROUP BY Dep_Id HAVING AVG(Wage) > 4000;
應用場景190:連接查詢
內連接
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT t1.Dep_name, t2.Emp_name FROM HRMAN.Departments t1, HRMAN.Employees t2
WHERE t1.Dep_id=t2.Dep_id;
外鏈接:
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT t1.Dep_name, t2.Emp_name FROM HRMAN.Departments t1 INNER JOIN HRMAN.Employees t2
ON t1.Dep_id=t2.Dep_id;
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT t1.Dep_name, t2.Emp_name FROM HRMAN.Departments t1 LEFT JOIN HRMAN.Employees t2
ON t1.Dep_id=t2.Dep_id;
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT t1.Dep_name, t2.Emp_name FROM HRMAN.Employees t2 RIGHT JOIN HRMAN.Departments t1
ON t1.Dep_id=t2.Dep_id;
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT t1.Dep_name, t2.Emp_name FROM HRMAN.Employees t2 FULL JOIN HRMAN.Departments t1
ON t1.Dep_id=t2.Dep_id;
交叉連接
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT t1.Dep_name, t2.Emp_name FROM HRMAN.Employees t2 CROSS JOIN HRMAN.Departments t1;
應用場景191:在連接查詢中對空值中對空值的判斷
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT t1.Dep_name, t2.Emp_name FROM HRMAN.Departments t1 LEFT JOIN HRMAN.Employees t2
ON t1.Dep_id=t2.Dep_id;
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT t1.Dep_name, t2.Emp_name FROM HRMAN.Departments t1 LEFT JOIN HRMAN.Employees t2
ON t1.Dep_id=t2.Dep_id
WHERE t2.Emp_id IS NULL;
應用場景192:一個簡單地子查詢
查詢辦公室的所有員工:
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_Name, Title FROM HRMAN.Employees WHERE Dep_Id =
(SELECT Dep_Id FROM HRMAN.Departments WHERE Dep_name = '辦公室');
返回兩個部門的值:
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_Name, Title FROM HRMAN.Employees WHERE Dep_Id =
(SELECT Dep_Id FROM HRMAN.Departments WHERE Dep_name = '辦公室' OR Dep_name = '人事部');
應用場景193:在查詢中使用具和函數返回值
統計表中所有工資低於品級工資的員工的信息
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees WHERE Wage <
(SELECT AVG(Wage) FROM HRMAN.Employees);
應用場景194:IN關鍵字與返回值的子查詢
查詢辦公室和人事部中的員工信息
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees WHERE Dep_Id IN
(SELECT Dep_Id FROM HRMAN.Departments WHERE Dep_name = '辦公室' OR Dep_name = '人事部');
應用場景195:EXISTS關鍵字與子查詢
查詢人事部中的員工信息
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees e
WHERE EXISTS
(SELECT Dep_id FROM HRMAN.Departments d
WHERE e.Dep_id = d.Dep_id AND d.Dep_name='人事部');
使用IN關鍵字:
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees e
WHERE e.Dep_id IN
(SELECT Dep_id FROM HRMAN.Departments d
WHERE e.Dep_id = d.Dep_id AND d.Dep_name='人事部');
應用場景196:使用UNION關鍵字的合並查詢
從表中Employee中查詢各個部門的部門經理
COL Dep_name FORMAT A20
COL Emp_name FORMAT A20
SELECT Dep_Id, Dep_Name FROM HRMAN.Departments
UNION
SELECT Dep_Id, Emp_Name FROM HRMAN.Employees WHERE Title = '部門經理';
工資大於3000的員工記錄:
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees
UNION
SELECT Emp_name, Title, Wage FROM HRMAN.Employees WHERE Wage > 3000;
高效率:
COL Emp_name FORMAT A20
COL Title FORMAT A20
SELECT Emp_name, Title, Wage FROM HRMAN.Employees
UNION ALL
SELECT Emp_name, Title, Wage FROM HRMAN.Employees WHERE Wage > 3000;
應用場景197:使用select語句中的DECODE函數
SELECT Emp_name, DECODE(Sex, '男', '先生', '女', '女士', '未知') AS Sex
FROM HRMAN.Employees;
應用場景198:使用select與劇中CASE函數
SELECT Emp_name, CASE Sex WHEN '男' THEN '先生' WHEN '女' THEN '女士' ELSE '未知' END AS Sex
FROM HRMAN.Employees;
查詢表Employees中的員工工資級別:
SELECT Emp_name, Wage, CASE WHEN Wage<=3000 THEN '低' WHEN Wage>3000 AND Wage<5000 THEN '中' WHEN Wage>=5000 THEN '高' END AS GRADE
FROM HRMAN.Employees;
應用場景199:保存查詢結果
將辦公室的所有員工的姓名和職務信息保存到表中OFFICE:
COL Emp_name FORMAT A20
COL Title FORMAT A20
CREATE TABLE HRMAN.Office AS
SELECT e.Emp_Name, e.Title
FROM HRMAN.Employees e, HRMAN.Departments d
WHERE e.Dep_id = d.Dep_Id AND d.Dep_Name = '辦公室';
SELECT * FROM HRMAN.Office;
應用場景200:插入數據語句
INSERT INTO HRMAN.Departments VALUES(100, '公關部');
SELECT * FROM HRMAN.Departments;
INSERT INTO HRMAN.Employees (Emp_Name, Sex,Title, IdCard, Dep_Id)
VALUES ('小明', '男', '職員', '110123456789', 2);
應用場景201:在插入數據時利用默認值
ALTER TABLE HRMAN.Employees ADD InputDate date DEFAULT(sysdate);
INSERT INTO HRMAN.Employees (Emp_Name, Sex,Title, IdCard, Wage, Dep_Id)
VALUES ('小李', '男', '職員', '210123456789', 2500, 2);
應用場景202:修改數據語句
將表中的所有記錄工資增加10%:
UPDATE HRMAN.Employees SET Wage=Wage*1.1;
將表中所有部門為"辦公室"的員工工資增加10%
UPDATE HRMAN.Employees SET Wage=Wage*1.1
WHERE Dep_id = (SELECT Dep_id FROM HRMAN.Departments WHERE Dep_name = '辦公室');
應用場景203:修改數據時不允許在唯一性約束列中使用相同的值
ALTER TABLE HRMAN.Employees
ADD CONSTRAINT UK_EMPNAME
UNIQUE(Emp_name);
UPDATE HRMAN.Employees SET Emp_name='張三' WHERE Emp_name='李四';
應用場景204:修改數據是不能違反檢查約束
ALTER TABLE HRMAN.Employees
ADD CONSTRAINT CK_EMPWAGE CHECK(WAGE>0);
UPDATE HRMAN.Employees SET Wage=-1 WHERE Emp_Name='張三';
應用場景205:修改數據時不能違反外鍵約束
為表HRMAN。Departments的DEP_id列和表HRMAN.Employees的DEP_ID列中創建外鍵約束
ALTER TABLE HRMAN.Employees
ADD CONSTRAINT FK_EMP_DEPID
FOREIGN KEY(Dep_id) REFERENCES HRMAN.Departments(Dep_id);
UPDATE HRMAN.Employees SET Dep_id=200 WHERE Emp_Name='張三';
應用場景206:刪除數據語句
DELETE FROM HRMAN.Employees WHERE Emp_Name = '小明';
TRUNCATE TABLE HRMAN.Employees;