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.
FROM
clause specifies the table(s) from which data is retrieved.FROM
clause.FROM
clause can also include subqueries, allowing you to query data from the result of another query.column1, column2, ...
: The columns you want to retrieve.table_name
: The name of the table from which to retrieve data.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 |
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 |
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 |
Name | DepartmentName |
---|---|
Anand | HR |
Bala | Finance |
Kavitha | IT |
Raj | Marketing |
Kumar | Sales |
Name | DepartmentName |
---|---|
Anand | HR |
Bala | Finance |
Kavitha | IT |
Raj | Marketing |
Kumar | Sales |
FROM
clause can also include subqueries. For example, suppose you want to retrieve employees who work in the IT department.
Name |
---|
Kavitha |
FROM
clause specifies the table(s) from which data is retrieved.SELECT
statement (unless querying without a table).FROM
clause is essential for retrieving and combining data from one or more sources.