DataLens

SELECT and FROM

Ask for exactly the columns you need, in the order you want, and read the result like a pro.

Level: Foundations · 9 min read

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

The idea

SELECT * FROM products; shows everything. That's handy for a first look, but at work you almost never want everything. A report about prices doesn't need stock levels, and a customer email list doesn't need signup dates. SELECT lets you pick exactly the columns you need, and that's the first step of every query you'll ever write.

Think of a table as a wide spreadsheet. FROM says which sheet to open. SELECT says which columns to keep visible, and in which order.

Picking columns

Say the marketing team is putting together a price list for the website. They need the name and price of every product, and nothing else. List the columns you want after SELECT, separated by commas, and the database leaves out all the others:

SELECT name, price
FROM products;
 name                 | price
----------------------+--------
 French Press 600ml   | 185000
 Pour-over Kettle     | 320000
 Arabica Gayo 250g    |  95000
 Robusta Lampung 250g |  65000
 Toraja Kalosi 250g   | 110000
 Ceramic Mug          |  55000
 Notebook A5 Dotted   |  45000
 Gel Pen Set          |  38000
 Desk Organizer       |  89000
 Wireless Mouse       | 175000
 USB-C Hub            | 289000
 Desk Lamp LED        | 245000
 Scented Candle       |  75000
 Throw Pillow         | 120000
 Bamboo Plant         |  60000
(15 rows)

You can put the columns in any order you like:

SELECT price, name, category
FROM products;
 price  | name                 | category
--------+----------------------+-------------
 185000 | French Press 600ml   | Kitchen
 320000 | Pour-over Kettle     | Kitchen
  95000 | Arabica Gayo 250g    | Coffee
...
(15 rows)

How SQL reads your query

You write SELECT first, but the database reads FROM first. It opens the table, then picks the columns. Keep that in mind, because it explains a lot of later rules:

You writeThe database does
SELECT name, price2. keep only these columns
FROM products1. open this table

This order will grow as you learn WHERE, GROUP BY and ORDER BY, and knowing it saves hours of confusion.

When something goes wrong

SQL errors look scary, but they're very specific. Ask for a column that doesn't exist:

SELECT name, prices
FROM products;
ERROR: column "prices" does not exist

The database tells you exactly which name it couldn't find. The fix is almost always a typo: here, the column is price, not prices. Check the Tables tab whenever you're not sure of a name.

Now the trailing comma:

SELECT name, price,
FROM products;
ERROR: syntax error at or near "FROM"

"Syntax error at or near FROM" means "something just before FROM is wrong". The database read the comma, expected another column, and found FROM instead.

Tip

When you get a syntax error, look at the word it names, then look just before it. That's where the mistake usually is.

Comments

Two dashes start a comment: everything after -- on that line is ignored by the database. Use comments to explain why a query exists:

-- Price list for the marketing team, July campaign
SELECT name, category, price
FROM products;
 name                 | category    | price
----------------------+-------------+--------
 French Press 600ml   | Kitchen     | 185000
 Pour-over Kettle     | Kitchen     | 320000
 Arabica Gayo 250g    | Coffee      |  95000
...
(15 rows)

Why not always use SELECT *?

* is perfect for exploring a table you don't know. In a query you'll keep, list the columns:

Key idea

Use SELECT * to look. Use named columns to work.

Several queries in one go

You can run more than one statement at once, ending each with a semicolon. The site shows each result separately:

SELECT name, city FROM customers;
SELECT name, department FROM employees;
 name            | department
-----------------+------------
 Rina Hartono    | Management
 Agus Salim      | Sales
 Maya Anggraini  | Operations
...
(12 rows)

The output above shows the last result. In the editor, you'll see both, one after the other.

Formatting style

There's no single official style, but this one is widely used and easy to read:

SELECT
    name,
    category,
    price
FROM products;

Why this matters for clean code

Choosing columns on purpose is the SQL version of naming variables well: it makes your intent obvious. A query that says SELECT name, email FROM customers is self-documenting. Your future self, your teammates and your Python scripts all benefit from queries that return exactly what they promise, nothing more.

Recap

Practise this module

The questions for this lesson run in your browser and check your answer straight away.

Next: WHERE and comparison operators →