What is the FROM Clause?
TheFROM
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
- Table Specification: The
FROM
clause specifies the table(s) from which data is retrieved. - Single or Multiple Tables: You can query data from a single table or join multiple tables using the
FROM
clause. - Alias Support: You can assign aliases to tables for easier reference in the query.
- 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 namedEmployees
that stores employee details.
Table: Employees
EmployeeID | Name | City | State |
---|---|---|---|
1 | Anand | Chennai | Tamil Nadu |
2 | Bala | Coimbatore | Tamil Nadu |
3 | Kavitha | Karaikal | Puducherry |
4 | Raj | Madurai | Tamil Nadu |
5 | Kumar | Trichy | Tamil Nadu |
- To retrieve the names and cities of all employees:
Name | City |
---|---|
Anand | Chennai |
Bala | Coimbatore |
Kavitha | Karaikal |
Raj | Madurai |
Kumar | Trichy |
Students
that stores student details, and you want to retrieve the names of students from Tamil Nadu.
Table: Students
StudentID | Name | City | State |
---|---|---|---|
1 | Ram | Chennai | Tamil Nadu |
2 | Karthik | Coimbatore | Tamil Nadu |
3 | David | Bangalore | Karnataka |
4 | Kannan | Karaikal | Puducherry |
5 | Siva | Madurai | Tamil Nadu |
Name |
---|
Ram |
Karthik |
Siva |
Using the FROM Clause with Multiple Tables
TheFROM
clause can also be used to retrieve data from multiple tables using joins. For example, suppose you have another table named Departments
.
Table: Departments
DepartmentID | EmployeeID | DepartmentName |
---|---|---|
101 | 1 | HR |
102 | 2 | Finance |
103 | 3 | IT |
104 | 4 | Marketing |
105 | 5 | Sales |
Name | DepartmentName |
---|---|
Anand | HR |
Bala | Finance |
Kavitha | IT |
Raj | Marketing |
Kumar | Sales |
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.Name | DepartmentName |
---|---|
Anand | HR |
Bala | Finance |
Kavitha | IT |
Raj | Marketing |
Kumar | Sales |
Using Subqueries in the FROM Clause
TheFROM
clause can also include subqueries. For example, suppose you want to retrieve employees who work in the IT department.
Name |
---|
Kavitha |
Key Takeaways
- The
FROM
clause specifies the table(s) from which data is retrieved. - It is mandatory in a
SELECT
statement (unless querying without a table). - You can query data from a single table, multiple tables (using joins), or subqueries.
- Table aliases can simplify queries, especially with long table names or multiple tables.
- The
FROM
clause is essential for retrieving and combining data from one or more sources.