1 SELECT语句:
SELECT语句用于从表中检索数据。
sql
SELECT column1, column2, ...
FROM table_name;
示例:
SELECT * FROM employees;
2 WHERE子句:
WHERE子句用于筛选满足特定条件的行。
SELECT column1, column2, ...
FROM table_name
WHERE condition;
示例:
SELECT * FROM employees
WHERE salary > 50000;
3 ORDER BY子句:
ORDER BY子句用于对结果进行排序,默认是升序。
sql
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 ASC, column2 DESC;
示例:
SELECT * FROM employees
ORDER BY last_name ASC, first_name DESC;
4 LIMIT/OFFSET子句:
LIMIT子句用于限制返回的行数,OFFSET子句用于指定从哪一行开始返回。
SELECT column1, column2, ...
FROM table_name
LIMIT limit_number OFFSET offset_number;
示例:
SELECT * FROM employees
LIMIT 10 OFFSET 20;
5 JOIN操作:
JOIN操作用于将两个或多个表中的行联接在一起。
SELECT column1, column2, ...
FROM table1
JOIN table2 ON table1.column = table2.column;
示例:
SELECT e.employee_id, e.first_name, d.department_name
FROM employees e
JOIN departments d ON e.department_id = d.department_id;
6 GROUP BY子句:
GROUP BY子句用于根据一个或多个列对数据进行分组。
SELECT column1, column2, ...
FROM table_name
GROUP BY column1, column2, ...;
示例:
SELECT department_id, COUNT(employee_id) AS employee_count
FROM employees
GROUP BY department_id;
7 HAVING子句:
HAVING子句用于筛选分组后的数据。
sql
SELECT column1, column2, ...
FROM table_name
GROUP BY column1, column2, ...
HAVING condition;
示例:
SELECT department_id, COUNT(employee_id) AS employee_count
FROM employees
GROUP BY department_id
HAVING COUNT(employee_id) > 5;