A Closer Look at SELECT *: Fine for Learning, Not Forever
SELECT * is the first query most of us write, and it is a wonderful way to explore a table. You may also have read that you should "never use SELECT *". Both are true in their place. This post explains when the star is your friend and when to retire it.
What the star means
* means "all columns". These two queries return the same thing for a table with three columns:
SELECT * FROM pets;
SELECT id, name, age FROM pets;
When the star is perfect
- Exploring. You have just opened a table and want to see what is in it.
- Quick checks. After an INSERT or UPDATE,
SELECT * FROM pets WHERE id = 4;shows the whole row. - Learning. While you are still discovering columns, typing them all is a distraction.
Add LIMIT 10 when exploring large tables, so you do not print thousands of rows by accident.
Why app code should name columns
1. Surprises when the table changes. Suppose your code reads rows as tuples:
for id, name, age in cur.execute("SELECT * FROM pets"):
print(name)
Someone adds a colour column. Now each row has four values and this line fails. With SELECT id, name, age, the code keeps working.
2. Clarity. A reader can see which columns the code depends on without looking up the table.
3. Less wasted data. If a table has a large column, like a long description or an image, SELECT * fetches it even when you only show names. On a list page with many rows, that adds up.
4. Ambiguity in joins. When two joined tables both have an id column, SELECT * returns two columns called id, and some tools pick the wrong one. Naming columns with aliases avoids confusion:
SELECT p.id AS pet_id, p.name, o.name AS owner_name
FROM pets p
JOIN owners o ON o.id = p.owner_id;
A simple rule
| Where you are typing | Star is fine? |
|---|---|
| A database console, exploring | Yes |
| A quick check after a change | Yes |
| Code that runs in your app | Name the columns |
| A query shared with others | Name the columns |
What about ORMs?
Tools such as Django or SQLAlchemy often select all mapped columns by default. That is fine while learning. Later, they offer ways to load only some columns when it matters. You do not need those yet; just know they exist.
Try it
Take a query from your own practice code that uses SELECT *. Rewrite it with named columns and aliases. Then add a new column to the table and confirm your code still works. That small experiment teaches the lesson better than any rule. For more on SELECT itself, see SELECT queries in five short modules.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.