1.概念
SQL,全称Structured Query Language,即结构化查询语言,是用于管理关系数据库管理系统(RDBMS)的标准编程语言。SQL语言的主要功能包括数据查询(Data Query)、数据操纵(Data Manipulation)、数据定义(Data Definition)和数据控制(Data Control)。常用操作就是增删改查
2.分类
- 数据定义语言(DDL,Data Definition Language):这类语句用于定义不同的数据库、表、视图、索引等数据库对象,以及用来创建、删除、修改数据库和数据表的结构。主要的语句关键字包括CREATE、DROP、ALTER等。
- 数据查询语言(DQL,Data Query Language):DQL用于检索数据库中的数据,其核心指令为SELECT。它允许用户查询存储在数据库中的信息,包括从单个或多个表中检索数据。
- 数据操纵语言(DML,Data Manipulation Language):DML用于添加、删除、更新和查询数据库记录,并检查数据完整性。主要的语句关键字包括INSERT、DELETE、UPDATE等。
- 数据控制语言(DCL,Data Control Language):DCL用于定义数据库、表、字段、用户的访问权限和安全级别。它控制特定用户或角色对数据库结构和数据的访问。主要的语句关键字包括GRANT、REVOKE等。
常用的 SQL 操作
以下 SQL 函数和操作涵盖了从数据提取、筛选到聚合、分析的常见需求。
基础查询
- SELECT
SELECT name, age FROM tablename;
- WHERE
SELECT * FROM tablename WHERE age > 11;
分组与排序
- GROUP BY 按某个字段分组:
SELECT department, COUNT(*) FROM employees GROUP BY department;
- ORDER BY 对结果集进行排序:
SELECT * FROM tablename ORDER BY age DESC;
- HAVING 对分组后的结果进行过滤:
SELECT department, AVG(salary) FROM employees GROUP BY department HAVING AVG(salary) > 5000;
表连接
- JOIN(包括 INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL JOIN):
SELECT employees.name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.id;
数据操作
- UNION 合并两个查询的结果:
SELECT name FROM employees UNION SELECT name FROM contractors;
- DISTINCT 去除重复值:
SELECT DISTINCT department FROM employees;
- COUNT() 计算数量:
SELECT COUNT(*) FROM employees WHERE department = 'Sales';
- SUM() 计算字段的总和:
SELECT SUM(salary) FROM employees WHERE department = 'Finance';
- AVG() 计算平均值:
SELECT AVG(age) FROM employees;
- MIN() 和 MAX() 查找最小值或最大值:
SELECT MIN(age) FROM employees; SELECT MAX(salary) FROM employees;
模糊匹配与范围查询
- LIKE 匹配查找以冯开头的姓名:
SELECT * FROM employees WHERE name LIKE '冯%';
- IN 检查是否在集合中:
SELECT * FROM employees WHERE department IN ('Sales', 'Marketing');
- BETWEEN 判断是否在范围内:
SELECT * FROM employees WHERE age BETWEEN 30 AND 40;
- IS NULL / IS NOT NULL 检查字段是否为空:
SELECT * FROM employees WHERE manager_id IS NULL;
数据类型和字符串操作
- CAST() / CONVERT() 数据类型转换:
SELECT CAST(salary AS DECIMAL(10,2)) FROM employees;
- SUBSTRING() / LEFT() / RIGHT() 字符串截取:
SELECT SUBSTRING(name, 1, 3) FROM employees;
- REPLACE() 替换字符串:
SELECT REPLACE(name, ' ', '_') FROM employees;
- TRIM() 去除空格:
SELECT TRIM(name) FROM employees;
数值处理
- ROUND() 四舍五入:
SELECT ROUND(salary, 2) FROM employees;
条件处理
- COALESCE() 返回第一个非空值:
SELECT COALESCE(manager_id, 'No_Manager') FROM employees;
- CASE 条件判断:
SELECT name, CASE WHEN salary > 5000 THEN 'High' ELSE 'Low' END AS salary_level FROM employees;
日期与时间处理
- DATEADD() / DATEDIFF() 日期处理:
SELECT DATEADD(day, 30, hire_date) FROM employees;
- NOW() / CURRENT_DATE() 返回当前日期:
SELECT NOW();
- EXTRACT() 提取日期部分:
SELECT EXTRACT(YEAR FROM hire_date) FROM employees;
窗口函数
- RANK() / ROW_NUMBER() 排序窗口函数:
SELECT name, salary, RANK() OVER (ORDER BY salary DESC) AS rank FROM employees;
- LAG() / LEAD() 获取前一个或下一个值:
SELECT name, salary, LAG(salary, 1) OVER (ORDER BY salary) AS previous_salary FROM employees;
- NTILE() 将数据划分为指定的数组: ```sql SELECT name, salary, NTILE(4) OVER (ORDER BY salary DESC) AS quartile FROM employees;
版权声明:如无特殊说明,文章均为本站原创,转载请注明出处
本文链接:http://kkxl95.cn/article/vz5YUEk3WV6TTExThcuK/