Skip to content

Beginner: asking questions · SELECT: filtering and sorting · topic 6 of 62 · 30 min

SELECT, columns, aliases and expressions

Explanation

After SELECT you list the columns you want, in the order you want them. You can also compute new columns with expressions such as price * qty or price * 1.18, round them with ROUND, and name them with AS. In real apps, list the columns you need instead of SELECT *.

Choosing columns

A table may have twenty columns, but a screen or a report usually needs three. After SELECT you write the columns you want, separated by commas, and after FROM the table. The result has exactly those columns, in that order.

sql
SELECT name, price FROM products;
-- Basmati Rice 5kg | 650
-- Toor Dal 1kg     | 160
-- ... 8 rows in all
Two columns of every product. The order in the SELECT list is the order in the result.
Think of it like this: SELECT is like asking the school office for "the names and phone numbers of class 10 students". You do not want their full files, only two details. Asking for less is quicker for the office and easier for you to read.

Computed columns

An expression is a small calculation, like price * qty. You can put an expression in the SELECT list, and the database works it out for every row. The table itself does not change; the new column exists only in the result.

OperatorMeaningExampleResult
+ -Add, subtract650 + 160810
*Multiply2 * 6501300
/Divide7 / 2 and 7 / 2.03 and 3.5
%Remainder7 % 21
||Join text'Asha' || ' ' || 'Verma'Asha Verma
sql
SELECT order_id, product_id, qty * price AS line_total FROM order_items;
-- 101 | 1 | 1300
-- 101 | 2 | 480
-- ... 12 rows
qty * price is worked out for each line. AS line_total names the new column.
Careful: Integer division: in SQLite and PostgreSQL, 7 / 2 is 3, because both numbers are whole, so the fraction is dropped. Write 7 / 2.0 or 7 * 1.0 / 2 to get 3.5. MySQL is different: its / always gives a decimal answer, 3.5000.

GST and ROUND

Many goods carry 18% GST, so the price with tax is price * 1.18. Decimals pile up quickly, so round money to two places with ROUND(value, 2).

sql
SELECT name, price, ROUND(price * 1.18, 2) AS price_with_gst FROM products;
-- Toor Dal 1kg  | 160 | 188.8
-- Cotton Kurta  | 899 | 1060.82
-- Basmati Rice 5kg | 650 | 767
ROUND(x, 2) keeps two decimals. The result is a number, so 767.0 is shown as 767 and 188.80 as 188.8.

Aliases with AS

An alias is a temporary name for a column (or a table) in one query. Write it after AS. It makes results readable and gives computed columns a proper heading instead of "ROUND(price * 1.18, 2)". If the alias has a space, put it in double quotes: AS "price with GST". The word AS is optional, but writing it makes your intent clear.

SELECT without FROM

sql
SELECT 2499 * 0.9 AS sale_price;   -- 2249.1
SELECT 'Asha' || ' ' || 'Verma';    -- Asha Verma
Handy as a calculator or for testing an expression. Oracle needs FROM dual; SQLite, PostgreSQL, MySQL and SQL Server do not.

NULL spreads through calculations

NULL means "no value" or "unknown". Any arithmetic with NULL gives NULL. Sneha Patil has no marks, so marks + 5 for her is NULL, not 5. Module 3 shows how to replace NULL with a value using COALESCE.

Why apps avoid SELECT *

  • It reads columns nobody needs, which wastes time and network, especially with big text columns.
  • When someone adds a column, the app suddenly gets data it did not expect, and code that reads columns by position breaks.
  • It can leak private columns, like a password hash, into places they should never reach.
  • SELECT * is fine for exploring a table by hand, as you did in module 1.

Common mistakes

  • A missing comma: SELECT name price FROM products does not fail. It returns name under the heading "price", because price is read as an alias. Check your commas.
  • A comma before FROM: SELECT name, FROM products gives near "FROM": syntax error. Remove the last comma.
  • Integer division giving 3 instead of 3.5. Make one number a decimal: / 2.0.
  • Forgetting ROUND on money, so a report shows 1060.8200000001. Round at the end of the calculation.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

SELECT, columns, aliases and expressions · 1/7

SELECT: columns and expressions

Now that you can read a whole table, let us choose exactly the columns we want.

Prefer a video by a teacher? Find free videos on “sql select columns alias calculated columns round tutorial” on YouTube ↗

In real life: An invoice line on a shopping site

When an online store shows your cart, each line is computed from the stored quantity and unit price: the line total, the GST and the amount to pay. The database stores only qty and price; the totals are expressions worked out at the moment you look, so a price change or a quantity change is reflected at once.

sql
SELECT order_id,
       qty,
       price,
       qty * price                   AS line_total,
       ROUND(qty * price * 0.18, 2)  AS gst,
       ROUND(qty * price * 1.18, 2)  AS to_pay
FROM order_items;
For order 101, product 2 (3 x 160): line_total 480, gst 86.4, to_pay 566.4.

Practice (0/4 done)

Task 1 · Code · SQL

From products, return two columns: name and price, for every product.

Task 2 · Code · SQL

From order_items, return order_id, product_id and the line total (qty multiplied by price) named line_total, for every line.

Task 3 · Code · SQL

Show prices with 18% GST. From products, return name, price, and price * 1.18 rounded to 2 decimal places, named price_with_gst.

Task 4 · Code · SQL

The shop wants the value of its stock. From products, return name, price, stock and price * stock named stock_value.

Quick quiz

Question 1

1.In SQLite, what does SELECT 7 / 2; return?

Question 2

2.What does SELECT name price FROM products; return?

Question 3

3.What is marks + 5 when marks is NULL?

Question 4

4.Which is a good reason to avoid SELECT * in application code?

Question 5

5.What does an alias (AS) change?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

In SQLite, what does SELECT 7 / 2; return?

Answer: 3

Both numbers are integers, so the fraction is dropped. Use 7 / 2.0 for 3.5.

What does SELECT name price FROM products; return?

Answer: One column: the names, under the heading price

Without a comma, price is read as an alias for name.

What is marks + 5 when marks is NULL?

Answer: NULL

Arithmetic with NULL gives NULL.

Which is a good reason to avoid SELECT * in application code?

Answer: It reads columns the app does not need and can expose private ones

List the columns you need: faster, safer and stable when the table changes.

What does an alias (AS) change?

Answer: Only the heading of the column in this query's result

An alias is a temporary name that lives only in the query result.

What does SELECT name FROM students WHERE marks > 80 return?Related

Answer: Names of students with marks above 80

SELECT name picks one column; WHERE marks > 80 keeps only the rows with more than 80.

From SELECT and WHERE: finding rows

A column is left out of an INSERT and has no DEFAULT and no NOT NULL. What value does it get?Related

Answer: NULL

With no default, a missing column becomes NULL.

From INSERT

dept_id has the values 1, 1, 2, NULL, NULL. How many rows does SELECT DISTINCT dept_id return?Related

Answer: 3

DISTINCT treats NULLs as equal, so the result is 1, 2 and one NULL.

From DISTINCT and how a query runs

Which statement removes the table itself, not just its rows?Related

Answer: DROP TABLE

DROP removes the structure, indexes and data.

From UPDATE and DELETE safely

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 4 more topics in “SELECT: filtering and sorting” before its exam: WHERE: comparison and logic, IN, BETWEEN, LIKE and NULL, ORDER BY, LIMIT and OFFSET, DISTINCT and how a query runs.
  2. Module 2 exam: SELECT, filtering and sorting · 15 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Functions, CASE and NULL. 62 topics left in the course before the final exam.

← Previous: The SQL language and your tools