CodeStride
Getting Started

Choosing columns in SQL

Topic 2 of 45 · 7 minFree lesson

SELECT * shows every column, which is handy for a first look. Most of the time, though, you only need a few. Asking for only those columns keeps the answer short and clear.

Naming the columns you want

List the columns after SELECT, with commas between them. You get only those columns, for every row.

Example 1
SELECT name, price FROM products;
name | price-----------------+------- Fern | 12.50 Cactus | 8.00 Monstera | 35.00 Snake Plant | 22.00 Lavender | 9.50 Rosemary | 6.00 Basil | 4.50 Olive Tree | 89.00 Watering Can | 15.00 Clay Pot | 7.50 Bonsai | 49.00 Mint | 4.00 Potting Soil | 11.00 Sunflower Seeds | 3.50 Plant Food | 9.00(15 rows)

The column names must match the table's exactly. You can find them in the list of tables under the editor.

Choosing the order

The columns come back in the order you list them, not the order the table keeps them in. Put the most important column first.

Example 2
SELECT city, name FROM customers;
city | name--------+-------------- Lisbon | Ana Silva Lagos | Ben Okafor Paris | Chloe Martin London | Dev Patel London | Emma Jones Berlin | Felix Wagner | Grace Kim Rome | Hugo Rossi Lisbon | Isla Brown | Jon Berg(10 rows)

One column

A single column is fine too. This is a quick way to look at one kind of fact, like the date each customer joined.

Example 3
SELECT joined FROM customers;
joined------------ 2024-02-11 2024-03-05 2024-05-20 2024-06-01 2024-08-14 2024-09-30 2024-11-02 2025-01-17 2025-02-08 2025-03-22(10 rows)

Spreading a query over lines

As queries grow, put each part on its own line. PostgreSQL ignores the extra spaces and line breaks, but people find it much clearer to read.

Example 4
SELECT name, category, stockFROM products;
name | category | stock-----------------+----------+------- Fern | indoor | 30 Cactus | indoor | 45 Monstera | indoor | 12 Snake Plant | indoor | 0 Lavender | outdoor | 60 Rosemary | herbs | 40 Basil | herbs | 80 Olive Tree | outdoor | 5 Watering Can | tools | 25 Clay Pot | tools | 100 Bonsai | indoor | 3 Mint | herbs | 70 Potting Soil | | 50 Sunflower Seeds | outdoor | 200 Plant Food | | 0(15 rows)
Common mistakeLeaving out a comma between two columns. PostgreSQL doesn't stop with an error. It reads name price as "the name column, shown with the heading price", so you get the names under the wrong heading.
Example 5
SELECT name price FROM products;
price----------------- Fern Cactus Monstera Snake Plant Lavender Rosemary Basil Olive Tree Watering Can Clay Pot Bonsai Mint Potting Soil Sunflower Seeds Plant Food(15 rows)

There's no price at all, only the names, under a heading that says price. When a result looks strange, check the commas first.

TipAsk only for the columns you need. On a real table with thousands of rows and dozens of columns, SELECT * sends a lot of data nobody reads.

Try it yourself

Run the code in the editor and answer 8 practice questions on choosing columns. It's free, you only need an account.