Bytes

FROM Clause in SQL

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:

  • table_name: • Name of the table that is referenced in the query.
  • table_name2: (optional) The second table referenced in the query.
  • JOIN clause: (optional) Used to specify the JOIN type between two tables.
  • WHERE clause: (optional) Used to filter the data returned by the query.
  • GROUP BY clause: (optional) Used to group the data returned by the query.
  • HAVING clause: (optional) Used to filter the grouped data returned by the query.

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_namelast_name
JohnSmith
MarkWhite
JaneBrown
RitaJohnson
DavidMiller
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.

NameCity
JohnLondon
MaryParis
DaveBerlin
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

EmployeeIDNameDepartmentID
1John1
2Sarah2
3Thomas3
4Mark2

Departments table

DepartmentIDDepartmentName
1Accounting
2Human Resources
3IT
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

  1. The FROM clause indicates the source table(s) from which the information will be recovered.
  2. The FROM clause is the primary clause utilized in a SELECT statement.
  3. The FROM clause can incorporate different tables, subqueries, and/or views.
  4. The FROM clause is required in each SELECT statement.
  5. Aliases can be utilized to abbreviate the table names and make the query less demanding to read.
  6. Joining tables can be drained from the FROM clause with the Join keyword.
  7. The JOIN keyword is used to specify how two tables are related.

Quiz

  1. The FROM clause in SQL is used to:  
    1. Create tables 
    2. Retrieve data  
    3. Alter tables 
    4. Delete tables

Answer: b. Retrieve data

  1. A FROM clause in SQL can be used to join tables.  
    1. True 
    2. False

Answer: a. True

  1. The FROM clause in SQL can be used to: 
    1. Sort data  
    2. Update data 
    3. Filter data 
    4. Create tables

Answer: c. Filter data

  1. The FROM clause in SQL can be used to select data from:  
    1. One table 
    2. Two tables  
    3. Multiple tables 
    4. All tables

Answer: c. Multiple tables

Module 6: Clauses in SQLFROM Clause in SQL

Top Tutorials

Related Articles

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter