fastread homefastrread library fastread menu

SQL : FROM Clause

Tutorial by:Admin      Date: 2016-04-14 22:41:21

❰ Previous Next ❱

This SQL tutorial explains how to use the SQL FROM clause with syntax and examples.

Description

The SQL FROM clause is used to list the tables and any joins required for the SQL statement.

Syntax

The syntax for the FROM Clause in SQL is:

FROM table1
[ { INNER JOIN
  | LEFT [OUTER] JOIN
  | RIGHT [OUTER] JOIN
  | FULL [OUTER] JOIN } table2
ON table1.column1 = table2.column1 ]

Parameters or Arguments

table1 and table2
These are the tables used in the SQL statement. The two tables are joined based on table1.column1 = table2.column1.

Note

  • When using the FROM clause in a SQL statement, there must be at least one table listed in the FROM clause.
  • If there are two or more tables listed in the SQL FROM clause, these tables are generally joined using INNER or OUTER joins.

Example - With one table

It is difficult to explain the syntax for the SQL FROM clause, so let's look at some examples.

We'll start by looking at how to use the FROM clause with only a single table.

For example:

SELECT *
FROM suppliers
WHERE city = 'Newark'
ORDER BY city DESC;

In this SQL FROM clause example, we've used the FROM clause to list the table called suppliers. There are no joins performed since we are only using one table.

Example - Two tables with INNER JOIN

Let's look at how to use the FROM clause with two tables and an INNER JOIN.

For example:

SELECT products.product_name, inventory.quantity
FROM products 
INNER JOIN inventory
ON products.product_id = inventory.product_id
WHERE products.product_id < 1000;

This SQL FROM clause example uses the FROM clause to list two tables - products and inventory. And we are using the FROM clause to specify an INNER JOIN between the products and inventory tables based on the product_id column in both tables.

Example - Two Tables with OUTER JOIN

Let's look at how to use the FROM clause when we join two tables together using an OUTER JOIN. In this case, we will look at the LEFT OUTER JOIN.

For example:

SELECT contacts.last_name, contacts.first_name, employees.emp_number
FROM contacts
LEFT OUTER JOIN employees
ON contacts.contact_id = employees.employee_id
WHERE contacts.last_name = 'Anderson';

This SQL FROM clause example uses the FROM clause to list two tables - contacts and employees. And we are using the FROM clause to specify a LEFT OUTER JOIN between the contacts and employees tables based on the contact_id column from the contacts table and the employee_id column from the employees table.

❰ Previous Next ❱