Skip to main content

FROM: Specifying the Source Table

FROM tells SQL where to get the data. It always pairs with SELECT and names the table, view, subquery, or CTE you want to query.

Prior lesson

This lesson builds on SELECT. The sample dataset (customers, products, orders) is defined in the Introduction to SQL lesson.

Syntax

SELECT column1, column2
FROM table_name;

-- With a table alias
SELECT column1, column2
FROM table_name AS alias;

Examples

Example 1: Basic FROM

SELECT
product_id,
name,
price
FROM products;

Result:

product_idnameprice
1Laptop999.00
2Mouse29.99
3Desk349.00
4Chair199.00
5Notebook4.99

Example 2: Table alias

A table alias shortens the table name within the query. This is especially useful when joining multiple tables (covered in the JOIN lesson). Aliasing is covered in full in the AS lesson.

SELECT
o.order_id,
o.order_date,
o.total_amount
FROM orders AS o;

The alias o is now a shorthand for orders throughout this query. Once you define an alias, use it consistently — most databases require this and will error if you mix orders.column and o.column in the same query.

Result:

order_idorder_datetotal_amount
12024-01-05999.00
22024-01-1059.98
32024-01-15349.00
.........

Example 3: FROM a subquery

FROM can also reference an inline query (a subquery) as if it were a table. This is a preview of a concept covered more fully in the WITH (CTE) lesson.

SELECT
sub.status,
sub.total_amount
FROM (
SELECT status, total_amount
FROM orders
WHERE total_amount > 100
) AS sub;

The subquery is given its own alias (sub) and treated like a regular table.

Common Mistake

Alias scope is query-local

A table alias only exists within the query that defines it. You cannot reference it in another query or a subsequent statement.

Practice

Write a query that selects customer_id, name, and country from the customers table using the alias c.

Show answer
SELECT
c.customer_id,
c.name,
c.country
FROM customers AS c;

Expected result:

customer_idnamecountry
1Alice JohnsonUSA
2Bob SmithUK
3Carol WhiteAustralia
4Dave BrownCanada
5Eve DavisUSA
6Frank LeeUSA
Donate to this project