The FROM clause in SQL Server

The SQL Server FROM clause (T-SQL) is used to list the necessary tables in the SQL Server query.

The SQL Server FROM clause (T-SQL) is used to list the necessary tables in the SQL Server query.

FROM clause clause

 FROM bang1 
[ { INNER JOIN
| LEFT OUTER JOIN
| RIGHT OUTER JOIN
| FULL OUTER JOIN } bang2
ON bang1.cot1 = bang2.cot1 ]

Variable names and variable values

bang1 and bang2 - tables used in SQL statements. Two linked tables according to the principle of state1.cot1 = bang2.cot1.

Note

  1. Must have at least 1 table in the FROM clause.
  2. If there are 2 or more tables, these tables are usually connected by keywords INNER or OUTER. Although it is possible to connect using the old syntax in the WHERE clause, it is recommended to use the new standard with the connection rule in the FROM clause.

Example - 1 table

 SELECT * 
FROM nhanvien
WHERE ten = 'Jane;

This example uses the FROM clause to get the table of nhanvien , no other connection tables.

Example - 2 tables with INNER JOIN

 SELECT nhacungcap.nhacungcap_id, nhacungcap.nhacungcap_ten, donhang.donhang_ngay 
FROM nhacungcap
INNER JOIN donhang
ON nhacungcap.nhacungcap_id = donhang.nhacungcap_id;

In this example, the FROM clause gives 2 tables and donhang , connecting these two tables with INNER JOIN with nhacungcap_id column on both tables.

Example - 2 tables with OUTER JOIN

 SELECT nhanvien.nhanvien_id, danhba.ho 
FROM nhanvien
LEFT OUTER JOIN danhba
ON nhanvien.nhanvien_id = danhba.danhba_id
WHERE nhanvien.ten = 'Sarah';

This FROM clause gives two tables, nhanvien and danhba , using LEFT OUTER JOIN to connect with nhanvien_id column in both tables.

Previous article: SELECT command in SQL Server

Next lesson: Comparison operators in SQL Server

5 ★ | 1 Vote