SELECT & FROM
Introduction
Every query that reads a table has SELECT and FROM: FROM names the table, SELECT the columns to bring back.
Here you will:
- choose columns, or all of them with
* - name the table, and give it a short name
- rename columns with
AS - work out new columns from the ones a table has
- add the same value to every row
The SELECT Statement
The SELECT clause names the columns you want in the result.
Basic Syntax
SELECT column1, column2, column3
FROM table_name;
What SELECT Does
- Chooses columns: you name the ones to include
- Orders columns: they come back in the order you list them
- Allows expressions: a column can be worked out, not only read
- Renames columns: an alias gives a column of the result its own name
Example
SELECT customer_id, customer_city, customer_state
FROM olist_customers_dataset;It returns these three columns for every row of the customers table: 99,441 rows, of which the page shows the first 30.

Selecting All Columns
The asterisk * stands for every column of the table, in the table's own order.
Syntax
SELECT *
FROM table_name;
When to Use SELECT *
Good uses:
- looking at a table you do not know yet
- a quick check of what the rows hold
- when you need every column
Avoid it:
- in a program, or a saved query, that others rely on
- when you need only some of the columns
Why Naming the Columns Is Better
A table can gain a column, and * follows it. This block makes a small table, adds a column author to it, and reads it with *:
CREATE TABLE notes (id INTEGER, body TEXT);
INSERT INTO notes VALUES (1, 'first');
ALTER TABLE notes ADD COLUMN author TEXT;
SELECT * FROM notes;The page shows 3 columns now, the new author among them, holding NULL. Named columns do not change:
CREATE TABLE notes (id INTEGER, body TEXT);
INSERT INTO notes VALUES (1, 'first');
ALTER TABLE notes ADD COLUMN author TEXT;
SELECT id, body FROM notes;The page shows id and body, as before. Naming the columns:
- says in the query which columns it needs
- leaves less data to read and to send back
- keeps the result the same when a table gains a column
- is clearer to the next person who reads the query
The FROM Clause
The FROM clause names the table, or tables, the rows come from.
Basic Syntax
SELECT columns
FROM table_name;
What FROM Does
- Names the source: the table that holds the data
- Makes its columns available: a column in
SELECTmust belong to a table inFROM - Can name several tables: for joins and subqueries, later in the course
Example
SELECT order_id, order_status, order_purchase_timestamp
FROM olist_orders_dataset;SQLite finds these three columns in olist_orders_dataset; the first order was placed at 2017-10-02 10:56:33.
Column Aliases
An alias gives a column of the result its own name. Write it after the keyword AS.
Syntax
SELECT column_name AS alias_name
FROM table_name;
Why Use Aliases?
- Shorter names: long column names read more easily
- Clearer names: say what the column holds
- Names for expressions: without one, the page names a worked-out column after the expression itself
- No clashes: two tables' columns of the same name can be told apart
Example
SELECT
customer_city AS city,
customer_state AS state
FROM olist_customers_dataset;The result's columns are named city and state.
Alias Rules
AS can be left out:
SELECT customer_city city FROM olist_customers_dataset LIMIT 3;The heading is city all the same, but a reader can take the alias for a second column, so write AS. A name with a space goes in double quotes, which SQL keeps for names: "The SQL standard requires double-quotes around identifiers and single-quotes around string literals."
SELECT customer_city AS "Customer City" FROM olist_customers_dataset LIMIT 3;The heading reads Customer City. PostgreSQL 17.11 gives the same heading.
Table Aliases
A table can have a short name too, which helps with long table names and with joins.
Syntax
SELECT t.column_name
FROM long_table_name AS t;
Example
With the full table name before each column:
SELECT
olist_customers_dataset.customer_city,
olist_customers_dataset.customer_state
FROM olist_customers_dataset
LIMIT 3;The same with an alias:
SELECT
c.customer_city,
c.customer_state
FROM olist_customers_dataset AS c
LIMIT 3;Both give the same columns: customer_city and customer_state.
Common Table Alias Patterns
| Table | Common alias |
|---|---|
| customers | c |
| orders | o |
| products | p |
| items | i |
SELECT o.order_id, o.order_status
FROM olist_orders_dataset AS o
LIMIT 3;Expressions in SELECT
SELECT can work out new values, not only read columns.
Arithmetic Expressions
SELECT
price,
freight_value,
price + freight_value AS total_cost
FROM olist_order_items_dataset
LIMIT 3;The first row: 58.9 and 13.29 make total_cost 72.19.
Common Operators
| Operator | Operation | Example |
|---|---|---|
+ | addition | price + freight_value |
- | subtraction | price - freight_value |
* | multiplication | price * 2 |
/ | division | price / 2 |
% | remainder | 7 % 2 |
SELECT
7 + 2 AS plus,
7 - 2 AS minus,
7 * 2 AS times,
7 / 2 AS divided,
7 % 2 AS remainder;The page shows 9, 5, 14, 3 and 1. Seven divided by two gives 3: SQLite divides two whole numbers to a whole number, and PostgreSQL 17.11 does the same. Make one of them a real number to keep the fraction:
SELECT 7 / 2 AS whole_numbers, 7 / 2.0 AS one_real_number;The page shows 3 and 3.5.
String Concatenation
|| joins text:
SELECT
customer_city || ', ' || customer_state AS location
FROM olist_customers_dataset
LIMIT 3;The first rows read franca, SP, sao bernardo do campo, SP and sao paulo, SP. PostgreSQL joins text with || too.
Functions in SELECT
SELECT
customer_city,
UPPER(customer_city) AS city_uppercase,
LENGTH(customer_city) AS city_length
FROM olist_customers_dataset
LIMIT 3;For the first row, franca becomes FRANCA, 6 letters long.
Literal Values
A query can also return a fixed value, the same in every row.
Text Literals
SELECT
customer_id,
customer_city,
'Brazil' AS country
FROM olist_customers_dataset
LIMIT 3;Every row has Brazil in the country column.
Numeric Literals
SELECT
product_id,
price,
price * 1.1 AS price_plus_10_percent,
10 AS percent_added
FROM olist_order_items_dataset
LIMIT 3;Each price comes back beside the price with 10% added (64.79 for the first) and the number 10.
Use Cases for Literals
- adding context to a result
- filling a column with a default value
- marking which query each row came from, when two queries' rows are put together (UNION)
Practice
Each query below runs on the course's data. Run it, then change it and run it again.
Exercise 1: Basic SELECT
Select three columns from the orders table:
-- Select order_id, customer_id, and order_status
-- from the orders table
SELECT
order_id,
customer_id,
order_status
FROM olist_orders_dataset
LIMIT 10;The page shows 10 orders; the first is delivered.
Exercise 2: Using Column Aliases
Rename columns with AS:
-- Use aliases to rename columns
SELECT
customer_id AS id,
customer_city AS city,
customer_state AS state
FROM olist_customers_dataset
LIMIT 10;The headings read id, city and state.
Exercise 3: Calculated Columns
Add the freight to the price:
-- Calculate total cost (price + freight)
SELECT
order_id,
product_id,
price,
freight_value,
price + freight_value AS total_cost
FROM olist_order_items_dataset
LIMIT 10;For the first item, 58.9 and 13.29 make 72.19.
Exercise 4: Table Aliases
Use p as the products table's short name:
-- Use 'p' as an alias for the products table
SELECT
p.product_id,
p.product_category_name,
p.product_weight_g
FROM olist_products_dataset AS p
LIMIT 10;The first product is in perfumaria and weighs 225 grams.
Exercise 5: String Concatenation
Join city and state with a comma:
-- Concatenate city and state with a comma separator
SELECT
customer_id,
customer_city || ', ' || customer_state AS full_location
FROM olist_customers_dataset
LIMIT 10;The first location reads franca, SP.
Common Mistakes
Mistake 1: A Column Not in the FROM Table
SELECT customer_city
FROM olist_orders_dataset;The page shows no such column: customer_city: the orders table has no city. Check which table holds a column before you name it.
Mistake 2: Missing Commas Between Columns
With both commas missing, SQLite stops:
SELECT order_id customer_id order_status
FROM olist_orders_dataset;The page shows near "order_status": syntax error. With only one missing, there is no error at all:
SELECT order_id customer_id
FROM olist_orders_dataset
LIMIT 3;The page shows one column headed customer_id, holding the order ids: without the comma, customer_id became a new name for order_id. With the commas:
SELECT order_id, customer_id, order_status
FROM olist_orders_dataset
LIMIT 3;Mistake 3: Forgetting FROM
SELECT customer_city;The page shows no such column: customer_city: with no table there is no column to find. With FROM:
SELECT customer_city
FROM olist_customers_dataset
LIMIT 3;Mistake 4: Using an Alias in the Same SELECT
SELECT
price + freight_value AS total,
total * 0.1 AS tenth
FROM olist_order_items_dataset
LIMIT 3;The page shows no such column: total: an alias names a column of the result, and the other columns of the same SELECT cannot read it. PostgreSQL refuses it too. Repeat the expression instead:
SELECT
price + freight_value AS total,
(price + freight_value) * 0.1 AS tenth
FROM olist_order_items_dataset
LIMIT 3;The first row shows total 72.19 and tenth 7.219.
Best Practices
1. Name the Columns
In a query others rely on, name the columns you need rather than *, so that it keeps returning the same columns when a table gains one.
2. Use Meaningful Aliases
-- Hard to follow
SELECT customer_city AS c FROM olist_customers_dataset;
-- Clear
SELECT customer_city AS city FROM olist_customers_dataset;
3. Format for Readability
On one line:
SELECT customer_id,customer_city,customer_state FROM olist_customers_dataset LIMIT 3;One column a line:
SELECT
customer_id,
customer_city,
customer_state
FROM olist_customers_dataset
LIMIT 3;Both give the same 3 columns; the second is easier to read and to change.
4. Comment What Is Not Obvious
SELECT
order_id,
-- the price with 10% added
price * 1.1 AS price_plus_10_percent
FROM olist_order_items_dataset
LIMIT 3;Everything after -- on a line is a comment, and SQLite drops it before the query runs: "Whitespace and comment tokens are discarded." The result is the same with it or without it.
Summary
Key Takeaways
- SELECT names the columns to return, in the order you list them.
- FROM names the table they come from.
*returns every column, and follows the table when it gains one; name the columns in queries others rely on.- Aliases rename columns and tables with
AS; an alias is not seen by the rest of the sameSELECT. SELECTcan work out values: arithmetic (two whole numbers divide to a whole number),||to join text, and functions.
Quick Reference
-- Some columns
SELECT column1, column2 FROM table_name;
-- Every column
SELECT * FROM table_name;
-- Column aliases
SELECT column1 AS alias1, column2 AS alias2 FROM table_name;
-- A table alias
SELECT t.column1, t.column2 FROM table_name AS t;
-- An expression
SELECT col1, col2, col1 + col2 AS total FROM table_name;
What's Next?
WHERE keeps only the rows you ask for.