Skip to content
Codewilds

Lesson 2 of 30 · Querying Basics

SQL SELECT Columns: Choose, Rename with AS and Calculate

easy10 XP2 min readFree lesson

SELECT * is handy for a first look at a table, but real queries ask for specific columns. The result is easier to read, the database sends less data, and your query keeps working when someone adds new columns to the table later.

Choosing columns

Instead of *, list the columns you want, separated by commas:

SELECT name, level FROM heroes;
name level
Aria Swiftwind 14
Borin Ironfist 18
Cyra Moonweaver 21
… …

The result has exactly the columns you asked for, in the order you listed them. The order inside the table doesn't matter — SELECT level, name FROM heroes; puts the level first.

Renaming columns with AS

An alias gives a column a new name in the result. Write AS and the new name after the column:

SELECT name AS hero, gold AS coins FROM heroes;

The result's headers are now hero and coins. The table itself is not changed — the alias only exists in this one result. Aliases are usually short, lowercase words joined with underscores, such as total_price.

Calculating new columns

A column in SELECT can also be an expression. SQL works it out separately for every row:

SELECT name, gold, gold * 2 AS doubled FROM heroes;
name gold doubled
Aria Swiftwind 320 640
Borin Ironfist 540 1080
… … …

You can use +, -, * and /, combine columns (level * 100 + gold) and use parentheses. Without an alias, the column would be named after the expression itself — gold * 2 — so calculated columns almost always get an AS.

Common mistakes

  • A missing comma. SELECT name level FROM heroes; runs without an error, but it returns one column: the names, under the header level. Without the comma, SQL reads the second word as an alias.
  • A comma before FROM. SELECT name, level, FROM heroes; is a syntax error. Commas go between columns only.
  • Dividing whole numbers. In SQLite, 7 / 2 is 3: dividing two integers drops the decimals. Write 7 / 2.0 when you want 3.5.

Assignment

The market keeper wants a price list for customers who buy in bulk. The items table looks like this:

id name type price rarity
1 Oak Shortbow weapon 120 common
8 Healing Potion potion 25 common
13 Crown of Stars armor 2500 legendary
… … … … …

Write a query that returns every item with exactly these three columns, in this order:

  1. the item's name, renamed to item
  2. its price
  3. the price of three of that item (price * 3), named three_pack

The row order doesn't matter. For example, the Healing Potion row should read Healing Potion | 25 | 75.

Keep learning

Need a quick reminder? The SQL cheat sheet has every topic of Learn SQL on one page.

Lesson last updated on .

Write code ↓
SQL+10 XP

Press Run to execute your code, or Submit to check it against the tests.