Skip to main content

What is the FROM Clause?

The FROM clause in SQL is used to specify the table or tables from which to retrieve data. It is a mandatory clause in a SELECT statement (unless you’re using a query without a table, like SELECT 1+1). The FROM clause tells the database which table(s) to query for the data.

Key Points

  1. Table Specification: The FROM clause specifies the table(s) from which data is retrieved.
  2. Single or Multiple Tables: You can query data from a single table or join multiple tables using the FROM clause.
  3. Alias Support: You can assign aliases to tables for easier reference in the query.
  4. Subqueries: The FROM clause can also include subqueries, allowing you to query data from the result of another query.

Syntax

  • column1, column2, ...: The columns you want to retrieve.
  • table_name: The name of the table from which to retrieve data.

Example

Suppose you have a table named Employees that stores employee details. Table: Employees
  1. To retrieve the names and cities of all employees:
Suppose you have a table named Students that stores student details, and you want to retrieve the names of students from Tamil Nadu. Table: Students

Using the FROM Clause with Multiple Tables

The FROM clause can also be used to retrieve data from multiple tables using joins. For example, suppose you have another table named Departments. Table: Departments To retrieve the employee names and their corresponding department names:

Using Table Aliases in the FROM Clause

Table aliases can be used to simplify queries, especially when dealing with long table names or multiple tables.

Using Subqueries in the FROM Clause

The FROM clause can also include subqueries. For example, suppose you want to retrieve employees who work in the IT department.

Key Takeaways

  1. The FROM clause specifies the table(s) from which data is retrieved.
  2. It is mandatory in a SELECT statement (unless querying without a table).
  3. You can query data from a single table, multiple tables (using joins), or subqueries.
  4. Table aliases can simplify queries, especially with long table names or multiple tables.
  5. The FROM clause is essential for retrieving and combining data from one or more sources.