📜  SQL中From和Where子句的区别

📅  最后修改于: 2021-09-27 14:39:20             🧑  作者: Mango

1. 从条款:
它用于选择将要使用 Select、Update 或 Delete 命令操作的数据集。它与 SQL 语句结合使用来操作源表中的数据集。我们可以使用 FROM 子句中的子查询从表中检索数据集。

FROM 子句的语法:

SELECT * 
FROM TABLE_NAME; 

2. WHERE 条款:
它用于对选定的数据集或源数据应用任何条件。源数据可以是单个表,也可以是连接多个表的结果。它返回那些满足 WHERE 子句中提到的条件的数据集实例。可以应用条件使用各种比较或逻辑运算符,如 –
AND、OR、IN、NOT IN、BETWEEN、等于、不等于等。

WHERE 子句的语法:

SELECT * FROM TABLE_NAME
WHERE (CONDITIONS); 

例子:
考虑一个表名 STUDENT

S_NO. s_NAME S_AGE S_SECTION
1 Yash 20 A
2 Vishwash 21 A
3 Vishesh 19 B
4 Shivam 23 A
5 Vasu 21 B
6 Shrey 20 C

问题:
我们必须选择那些年龄小于 22 且部分为 A 的 STUDENT 表实例。

询问:

SELECT * 
FROM STUDENT 
WHERE S_AGE<22 AND S_SECTION='A'; 

输出:
这里 FROM 子句选择应该应用 WHERE 子句的表,WHERE 子句检查这两个条件以找到数据集的哪些实例满足它们。

S_NO. s_NAME S_AGE S_SECTION
1 Yash 20 A
2 Vishwash 21 A

FROM 子句和 WHERE 子句的区别:

S_NO. FROM Clause WHERE Clause
1. It is used to select the dataset on which manipulation has to be done. It is used for checking some conditions to filter result
2. We provide some dataset into the FROM clause as a input. In WHERE clause we give some condition as input.
3. FROM clause selects dataset to provide it to WHERE clause for applying conditions given in query. WHERE clause act as selector which filters required instances from dataset provide by FROM clause.
4. FROM clause is mandatory because if there is no dataset, no manipulation can be performed. WHERE is optional, we use it only in case of condition checking.