sqlserver数据库查询语句入门

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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值