Lesson 2 of 20

In progress

Course progress

SQL

SELECT: choosing columns

Name the columns you need, rename them with AS, and build expressions that make a result readable.

SELECT decides what comes back. It can return whole columns, a computed value, a constant, or a mix — and the difference between a query that is easy to consume and one that is not is usually here.

What you will learn

  • Select specific columns and alias the result.
  • Compute new values with expressions and functions.
  • Know when SELECT * is acceptable.

The shape of a query

SQL
Editable. Runs in your browser.





Press Run to see the output.

SELECT lists expressions, FROM names the table. Two clauses are enough to get data out. Everything else filters, groups or sorts what those two produced.

The database returns columns in the order you listed them, with the names you gave. A consumer reading JSON from an API sees exactly those keys, so the SELECT list is an interface, not a convenience.

Aliases

SQL
Editable. Runs in your browser.





Press Run to see the output.

AS renames a column in the result. It does not rename anything in the table. Aliases are worth using when:

  • An expression would otherwise produce an unhelpful header.
  • The same table appears twice in a join and needs a short name.
  • An API contract expects a specific key.

Quotes are needed for aliases with spaces, but prefer names that do not need them.

Expressions

SQL can do arithmetic, string concatenation and function calls in the SELECT list.

SQL
Editable. Runs in your browser.





Press Run to see the output.

Two details matter here. First, integer division: price_paise / 100 in SQLite with integers truncates, so divide by 100.0 to get a decimal. Second, rounding is applied to the computed value, so the tax is not accumulated rounding error.

Text functions vary more between databases than arithmetic does. UPPER, LOWER, LENGTH, TRIM and SUBSTR are widely available; date functions differ the most.

Constants and labels

SQL
Editable. Runs in your browser.





Press Run to see the output.

A constant column looks odd in a raw query and is useful when combining several queries with UNION, where each part must have the same number of columns. It is also handy when you want a fixed label in an export.

SELECT * and when it bites

SQL
Editable. Runs in your browser.





Press Run to see the output.

During exploration, * is convenient: you do not yet know which columns exist. In application code it is a liability.

The result shape changes whenever someone adds, renames or reorders a column. Code that destructures row[3] breaks silently. A SELECT * also transfers columns the client never uses, which matters on a wide table with large text fields.

The rule that works: * in a scratch query, named columns in anything that ships.

Column order and readability

List columns in the order a reader wants them, not the order they appear in the table. An id is rarely the most interesting field; put the name first when a human will read the result.

Formatting matters too. One column per line with aligned aliases is easier to review in a code diff than a single long line.

WHERE comes later

The queries here return every row. Real questions usually need a subset, which is WHERE — but it is worth seeing the unfiltered shape first, because that is the data the filter will act on.

Writing SELECT name, price_paise / 100 AS price and expecting decimals. Integer division truncates, so 4500 paise becomes 45, not 45.0. Divide by a decimal literal, or cast one side.

From the products table, return the product name in capitals, the price in rupees rounded to two decimals, and a column called stock_label that is 'in stock' when in_stock is 1 and 'out of stock' otherwise. Use a CASE expression.

Show solution
SQL
Editable. Runs in your browser.





Press Run to see the output.

UPPER changes only the output, not the stored value. CASE is SQL’s conditional expression and works in SELECT, WHERE and ORDER BY. Dividing by 100.0 forces decimal arithmetic so rounding is meaningful.

Recap

  • SELECT lists the expressions returned; name columns rather than relying on * in shipped code.
  • AS renames output columns and keeps API keys stable.
  • Integer division truncates; use a decimal literal when a fraction matters.

Next, you will filter rows with WHERE and meet the three-valued logic that trips up everyone.

Can I use SELECT without FROM?
In most databases yes, for a constant expression such as `SELECT 1 + 1`. SQLite also allows it, which makes quick calculations possible without a table.
Does the column order in SELECT affect performance?
Not meaningfully. It affects payload size if you select more than you need, and it affects readability, which is the bigger cost.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.