Documentation Index
Fetch the complete documentation index at: https://rajanand.org/llms.txt
Use this file to discover all available pages before exploring further.
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
- 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.
SELECT column1, column2, ...
FROM table_name;
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
| 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:
SELECT Name, City
FROM Employees;
| Name | City |
| Anand | Chennai |
| Bala | Coimbatore |
| Kavitha | Karaikal |
| Raj | Madurai |
| Kumar | Trichy |
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
| 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 |
SELECT Name
FROM Students
WHERE State = 'Tamil Nadu';
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
| DepartmentID | EmployeeID | DepartmentName |
| 101 | 1 | HR |
| 102 | 2 | Finance |
| 103 | 3 | IT |
| 104 | 4 | Marketing |
| 105 | 5 | Sales |
To retrieve the employee names and their corresponding department names:
SELECT Employees.Name, Departments.DepartmentName
FROM Employees
JOIN Departments ON Employees.EmployeeID = Departments.EmployeeID;
| 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.
SELECT e.Name, d.DepartmentName
FROM Employees AS e
JOIN Departments AS d ON e.EmployeeID = d.EmployeeID;
| Name | DepartmentName |
| Anand | HR |
| Bala | Finance |
| Kavitha | IT |
| Raj | Marketing |
| Kumar | Sales |
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.
SELECT Name
FROM (
SELECT Employees.Name
FROM Employees
JOIN Departments ON Employees.EmployeeID = Departments.EmployeeID
WHERE Departments.DepartmentName = 'IT'
) AS IT_Employees;
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.