# SELECT & Column Labels

> Choose exactly which columns a query returns, build new columns from expressions, and name them with AS.

Subject: SQL | Lesson 6 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/sql/select-and-column-labels
Previous: [Foreign Keys & Relationships](https://codewithanimation.in/sql/foreign-keys-and-relationships.md) | Next: [WHERE Filters](https://codewithanimation.in/sql/where-filters.md)

## The select list decides the columns

`SELECT` starts every query. The part after it is the select list: the columns you want back. You can write column names, `*` for every column, or any expression that produces a value.

An expression is worked out once for every row. It does not have to use a table column, and it can be as simple as `points + 10`.

`AS` gives an output column a name. It changes the name in the result only, never in the table.

## Ask only for what you need

A smaller result is faster to send and easier to read. It also keeps your program stable: if you name the columns you use, a new column in the table cannot change what your code receives.

Good labels make results self-explaining. A column called `score` says more than `points + 10`.

## Build a select list

This query returns two columns for every player, and renames one of them:

```sql
SELECT name, points AS score
FROM players;
```

The result has the columns `name` and `score`. The table still calls the second column `points`.

Without `AS`, PostgreSQL picks a name. A plain column keeps its own name, a function uses the function's name, and a more complex expression gets a generic name. `AS` is usually optional, but writing it always is the safe habit, because a name that happens to be a reserved word would otherwise break the query.

## Common mistake: SELECT * in real code

`SELECT *` is great for looking around. In a program it is risky: when someone adds a column, your query suddenly returns more data than your code expects.

A calculated column with no label gets a generic name. Your program cannot reliably find it, so always label calculated columns.

## Tip: Label every calculated column

Write `AS` for anything that is not a plain column. Pick a short name that says what the value means, such as `score` or `bonus`.

## Try it: Shape the select list

Tap columns to add or remove them, try `*`, rename `points`, and add a calculated column with and without a label.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/sql/select-and-column-labels

## Check yourself

### Question 1

What does `SELECT name, points FROM players` return?

- A. Two columns, name and points, for every player
- B. Only the first player
- C. Every column of every player

Correct answer: A

The select list picks the columns. With no WHERE or LIMIT, every row is returned.

- Why not B: Without a condition or a limit, every row comes back.
- Why not C: That is what SELECT * does. Here only the named columns come back.

### Question 2

You run `SELECT points + 1 FROM players` with no AS. What name does the result column get?

- A. A generic name chosen by PostgreSQL, so you should label it with AS
- B. points, the column used in the expression
- C. No name: the query fails

Correct answer: A

A complex expression gets a generic name. Add AS to give it a useful one.

- Why not B: Only a plain column reference keeps its column name.
- Why not C: The query works. The column just gets a generic name.

### Question 3

Which query returns a column called `score` that holds each player's points?

- A. SELECT points AS score FROM players;
- B. SELECT score AS points FROM players;
- C. RENAME points TO score FROM players;

Correct answer: A

The pattern is expression AS new_name. The old name comes first.

- Why not B: That renames a column called score, which does not exist, to points.
- Why not C: SQL has no RENAME in a SELECT. Use AS in the select list.

## Remember this

- The select list decides which columns a query returns: names, `*` or expressions.
- An expression in the select list is worked out once for every row.
- AS renames a column in the result only, and calculated columns should always have one.
- SELECT * is fine for exploring but risky in programs, because new columns change the result.
