SQL语句分为以下三种类型:
DML: Data Manipulation Language 数据操纵语言,用于查询与修改数据记录,包括如下SQL语句:
INSERT:添加数据到数据库中
UPDATE:修改数据库中的数据
DELETE:删除数据库中的数据
SELECT:选择(查询)数据
DDL: Data Definition Language 数据定义语言,用于定义数据库的结构,比如创建、修改或删除数据库对象,包括如下SQL语句:
CREATE TABLE:创建数据库表
ALTER TABLE:更改表结构、添加、删除、修改列长度
DROP TABLE:删除表
CREATE INDEX:在表上建立索引
DROP INDEX:删除索引
DCL: Data Control Language 数据控制语言,DCL用来控制数据库的访问,包括如下SQL语句:
GRANT:授予访问权限
REVOKE:撤销访问权限
COMMIT:提交事务处理
ROLLBACK:事务处理回退
SAVEPOINT:设置保存点
LOCK:对数据库的特定部分进行锁定
注意点:
1.列的别名使用双引号。
2.日期和字符只能在单引号中出现。
3.使用 desc table 显示表结构。
4.WHERE 子句紧随 FROM 子句。
5.使用 LIKE 运算选择类似的值选择条件可以包含字符或数字:% 代表零个或多个字符(任意个字符), _ 代表一个字符。
6.使用 IS (NOT) NULL 判断空值。
7.在多表中如有相同列时,在列名之前加上表名前缀。
8.当完成 n 个表之间的连接查询时,至少需要 n - 1 个连接条件。
9.左外连接:left join ...on 将多个表中满足条件的数据查询出来,还将左表中不满足条件的数据也查询出来
10.右外连接:right join ...on 将多个表中满足条件的数据查询出来,还将右表中不满足条件的数据也查询出来
11.count(),组函数不计算空值。
12.单行函数传递一行返回一个结果,多行函数是传递多行返回一个结果。
13.出现在select 子句后的非分组函数,就一定得出现在group by子句后。
14.where不能过滤组函数,若需要过滤组函数需要使用having。
练习题:
-- 分组函数
-- MAX() MIN() AVG() SUM() COUNT()
SELECT MAX(salary), MIN(salary), AVG(salary), SUM(salary), COUNT(salary)
FROM employees;
-- 查询各个部门的平均工资
SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id;
-- ***出现在 SELECT 子句后的非分组函数,一定得出现在 GROUP BY 子句后,多列分组
-- 求出各个部门中每个工种(job_id)的平均工资是多少
SELECT department_id, job_id, AVG(salary)
FROM employees
GROUP BY department_id, job_id;
-- where 不能过滤组函数,若需要过滤组函数需要使用 having
SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id
HAVING AVG(salary) >= 10000;
-- MySQL中组函数不能嵌套,但是 Oracle 可以
SELECT department_id, MAX(AVG(salary))
FROM employees
GROUP BY department_id;
SELECT MAX(avg_sal)
FROM (
SELECT department_id, AVG(salary) avg_sal
FROM employees
GROUP BY department_id
) e;
-- 子查询
-- 谁的工资比 Abel 高?
-- 主查询(外查询)
SELECT *
FROM employees
WHERE salary > (
-- 子查询(内查询)
SELECT salary
FROM employees
WHERE last_name = 'Abel'
);
-- 题目:返回job_id与141号员工相同,salary比143号员工多的员工姓名,job_id 和工资
SELECT last_name, job_id, salary
FROM employees
WHERE job_id = (
SELECT job_id
FROM employees
WHERE employee_id = 141
) AND salary > (
SELECT salary
FROM employees
WHERE employee_id = 143
);
-- 题目:返回公司工资最少的员工的last_name,job_id和salary
SELECT last_name, job_id, salary
FROM employees
WHERE salary = (
SELECT MIN(salary)
FROM employees
);
-- 题目:查询最低工资大于50号部门最低工资的部门id和其最低工资
SELECT department_id, MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary) > (
SELECT MIN(salary)
FROM employees
WHERE department_id = 50
);
--题目: 查询平均工资最低的部门信息
SELECT *
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MIN(avg_sal)
FROM (
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
) e
)
);
--题目: 查询平均工资最低的部门信息和 该 部门的平均工资
SELECT d.*, (SELECT AVG(salary) FROM employees WHERE department_id = d.`department_id`)
FROM departments d
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MIN(avg_sal)
FROM (
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
) e
)
);
-- 题目: 查询平均工资最高的 job 信息
SELECT *
FROM jobs
WHERE job_id = (
SELECT job_id
FROM employees
GROUP BY job_id
HAVING AVG(salary) = (
SELECT MAX(avg_sal)
FROM (
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY job_id
) e
)
);
--题目: 各个部门中 最高工资中最低的那个部门的 最低工资是多少
-- 1) 求出各个部门的最高工资
SELECT MAX(salary)
FROM employees
GROUP BY department_id
-- 2) 在1) 基础上查出最低的
SELECT MIN(max_sal)
FROM (
SELECT MAX(salary) max_sal
FROM employees
GROUP BY department_id
) e
-- 3) 求出哪个部门的最高工资是 2) 的结果
SELECT department_id
FROM employees
GROUP BY department_id
HAVING MAX(salary) = (
SELECT MIN(max_sal)
FROM (
SELECT MAX(salary) max_sal
FROM employees
GROUP BY department_id
) e
)
-- 4) 再在3) 的基础上求出最低工资
SELECT MIN(salary)
FROM employees
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING MAX(salary) = (
SELECT MIN(max_sal)
FROM (
SELECT MAX(salary) max_sal
FROM employees
GROUP BY department_id
) e
)
);
-- 题目:查询平均工资最高的部门的 manager 的详细信息: last_name, department_id, email, salary
SELECT last_name, department_id, email, salary
FROM employees
WHERE employee_id = (
SELECT manager_id
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MAX(avg_sal)
FROM (
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
) e
)
)
)
--题目:查询平均工资最低的部门信息
SELECT *
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MIN(avg_sal)
FROM (
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
) e
)
);