In SQL Server, the FROM
clause is used to specify the source table or tables from which the data is retrieved in a query.
The FROM
clause is one of the mandatory clauses in the SELECT statement, which is used to retrieve data from one or more tables.
Syntax
The basic syntax of the FROM clause is as follows:
SELECT column1, column2, ...
FROM table_name;
In this syntax, the FROM
keyword is followed by the name of the table or tables from which the data is retrieved. The table name can be followed by an optional alias name, which is used to provide a short name or alias for the table.
Example
For example, the following SQL query retrieves all the records from the customers table:
SELECT *
FROM customers;
In addition to specifying a single table in the FROM clause, you can also join multiple tables using various types of join operators, such as INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. When joining multiple tables, you need to specify the join condition in the ON clause, which is used to specify the column or columns that are used to join the tables.
For example, the following SQL query retrieves the order details for each customer from the customers and orders tables, which are joined on the customer_id column:
SELECT customers.customer_name, orders.order_id, orders.order_date
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;
In this query, the FROM
clause specifies two tables, customers
and orders
, which are joined using the INNER JOIN
operator on the customer_id column. The SELECT clause retrieves the customer_name, order_id, and order_date columns from the joined tables.
In conclusion, the FROM
clause is a fundamental part of SQL Server queries, and it is used to specify the source table or tables from which the data is retrieved. It can also be used to join multiple tables to retrieve data from related tables in a database.