Overview
The FROM clause in SQL indicates the source tables for the information that will be utilized within the query. It is the primary clause in a SELECT statement and is used to characterize the tables included within the query. The FROM clause can moreover indicate a JOIN between numerous tables, permitting information from distinctive tables to be combined within the query result. Also, the FROM clause can indicate subqueries and incorporate clauses such as WHERE and Group BY.
Introduction
A prevalent e-commerce site was encountering diminished deals due to the competition. The site had a broad client data database but needed help to target the right clients successfully. The promoting group used SQL's FROM clause to create targeted customer lists based on client socioeconomics, buy history, and other variables. As they are ignorant of it, let's help them.
The FROM clause in SQL indicates the source tables for the information that will be utilized within the query.
Syntax
FROM table1
[ { INNER JOIN | LEFT [OUTER] JOIN| RIGHT [OUTER] JOIN } table2
ON table1.column1 = table2.column1 ]
Parameters
The FROM clause is composed of several parameters:
MySQL FROM Clause: Retrieve data from one table
The MySQL FROM clause is used to retrieve data from one table. The syntax is as follows:
SELECT column_name_1, column_name_2
FROM table_name
WHERE condition;
This statement will retrieve the values in the specified columns from the specified table. The WHERE clause is used to filter the results as desired.
For example, the following statement will retrieve the first name and last name of all employees in the company:
first_name | last_name |
---|---|
John | Smith |
Mark | White |
Jane | Brown |
Rita | Johnson |
David | Miller |
SELECT first_name, last_name
FROM employees
WHERE company_id = 1;
MySQL FROM Clause: Retrieve data from two tables with inner join
Syntax
SELECT t1.column1, t2.column2
FROM table1 t1
INNER JOIN table2 t2
ON t1.column1 = t2.column2;
This query is utilized to recover information from two tables, table1, and table2, by utilizing an inner join. The inner join combines the two tables on the premise of a common column, in this case, t1.column1 and t2.column2. The SELECT statement indicates the columns from each table that ought to be returned within the query result.
Name | City |
---|---|
John | London |
Mary | Paris |
Dave | Berlin |
SELECT [t1.name](<http://t1.name/>), t2.city
FROM employees t1
INNER JOIN cities t2
ON t1.city_id = t2.city_id;
Using an inner join, the above query retrieves data from two tables, employees and cities. The inner join combines the two tables based on a common column, in this case, t1.city_id and t2.city_id. The SELECT statement specifies the columns from each table that should be returned in the query result: the employee's name from the employee's table and the city name from the cities table.
MySQL FROM Clause: Retrieve data from two tables using an outer join
Syntax
SELECT t1.column1, t2.column2
FROM table1 t1
LEFT OUTER JOIN table2 t2
ON t1.column3 = t2.column3;
This query retrieves data from two tables, table1 (t1) and table2 (t2), using a Left Outer Join. The Left Outer Join will return all of the rows from the left table (table1) even if there are no matching rows in the right table (table2). The ON clause specifies which columns from each table should be compared when determining which rows should be returned in the result set.
For example, let's say we have two tables, Employees and Departments, with the following columns:
Employees: EmployeeID, Name, DepartmentID Departments: DepartmentID, DepartmentName
Left Outer Join is used to retrieve all of the employees and their respective departments like so:
Employees table
EmployeeID | Name | DepartmentID |
---|---|---|
1 | John | 1 |
2 | Sarah | 2 |
3 | Thomas | 3 |
4 | Mark | 2 |
Departments table
DepartmentID | DepartmentName |
---|---|
1 | Accounting |
2 | Human Resources |
3 | IT |
SELECT e.EmployeeID, e.Name, d.DepartmentName
FROM Employees e
LEFT OUTER JOIN Departments d
ON e.DepartmentID = d.DepartmentID;
This query will return all of the employees from the Employees table, along with the name of their department from the Departments table (if they have one). If an employee does not have a department, the DepartmentName column will be NULL in the result set.
Conclusion
Now they have mastered the skills. Using the FROM clause to segment its customer database, the website identified and targeted the right customers and increased sales. The website achieved the desired results and continues to use the FROM clause for marketing campaigns. The website identified and targeted the right customers and increased sales, resulting in a successful marketing campaign.
Key takeaways
Quiz
Answer: b. Retrieve data
Answer: a. True
Answer: c. Filter data
Answer: c. Multiple tables
Top Tutorials
Related Articles