6 Oracle 表的管理-表查询(重点)
-- 查询表中所有记录
SELECT * FROM employees;
-- 查询特定列(姓名和薪水)
SELECT first_name, last_name, salary FROM employees;
-- 查询并为特定列指定别名(姓名和薪水)
SELECT first_name AS fname, last_name AS lname, salary AS sal FROM employees;
-- 查询并去除重复行(不同部门)
SELECT DISTINCT department_id FROM employees;
-- 查询并按薪水降序排序
SELECT * FROM employees ORDER BY salary DESC;
-- 查询并按部门和薪水升序/降序排序
SELECT * FROM employees ORDER BY department_id, salary DESC;
-- 查询特定条件的记录(薪水大于5000的员工)
SELECT * FROM employees WHERE salary > 5000;
-- 查询特定条件的记录(部门为10和薪水大于5000的员工)
SELECT * FROM employees WHERE department_id = 10 AND salary > 5000;
-- 查询特定条件的记录(部门为10或薪水大于5000的员工)
SELECT * FROM employees WHERE department_id = 10 OR salary > 5000;
-- 查询特定条件的记录(薪水在5000到10000之间的员工)
SELECT * FROM employees WHERE salary BETWEEN 5000 AND 10000;
-- 查询特定条件的记录(姓名以'A'开头的员工)
SELECT * FROM employees WHERE first_name LIKE 'A%';
-- 查询特定条件的记录(薪水为NULL的员工)
SELECT * FROM employees WHERE salary IS NULL;
-- 使用IN操作符查询特定条件的记录(部门为10、20或30的员工)
SELECT * FROM employees WHERE department_id IN (10, 20, 30);
-- 查询并通过子查询(找出部门20的管理者)
SELECT * FROM employees WHERE employee_id IN (SELECT manager_id FROM employees WHERE department_id = 20);
-- 查询并通过分组(不同部门的平均薪水)
SELECT department_id, AVG(salary) FROM employees GROUP BY department_id;
-- 查询并通过分组和筛选(部门平均薪水大于5000的部门)
SELECT department_id, AVG(salary) FROM employees GROUP BY department_id HAVING AVG(salary) > 5000;
-- 查询并通过分组、筛选和连接(部门平均薪水大于5000的部门及其名称)
SELECT d.department_id, AVG(e.salary), d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id
GROUP BY d.department_id, d.department_name
HAVING AVG(e.salary) > 5000;
-- 查询并通过分页(获取前5条记录)
SELECT * FROM employees WHERE ROWNUM <= 5;
-- 查询并通过分页和排序(获取薪水第二高的员工)
SELECT * FROM (SELECT * FROM employees ORDER BY salary DESC) WHERE ROWNUM = 1;
这些示例展示了如何在Oracle数据库中使用不同的SQL查询操作来管理和查询表。注意,这些操作可能需要根据您的特定数据库环境和表结构进行调整。
评论已关闭