Beginner: asking questions · SELECT: filtering and sorting · topic 6 of 62 · 30 min
SELECT, columns, aliases and expressions
Explanation
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.
SELECT name, price FROM products;
-- Basmati Rice 5kg | 650
-- Toor Dal 1kg | 160
-- ... 8 rows in allComputed 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.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + - | Add, subtract | 650 + 160 | 810 |
| * | Multiply | 2 * 650 | 1300 |
| / | Divide | 7 / 2 and 7 / 2.0 | 3 and 3.5 |
| % | Remainder | 7 % 2 | 1 |
| || | Join text | 'Asha' || ' ' || 'Verma' | Asha Verma |
SELECT order_id, product_id, qty * price AS line_total FROM order_items;
-- 101 | 1 | 1300
-- 101 | 2 | 480
-- ... 12 rowsGST 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).
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 | 767Aliases 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
SELECT 2499 * 0.9 AS sale_price; -- 2249.1
SELECT 'Asha' || ' ' || 'Verma'; -- Asha VermaNULL 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
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.
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;Practice (0/4 done)
From products, return two columns: name and price, for every product.
From order_items, return order_id, product_id and the line total (qty multiplied by price) named line_total, for every line.
Show prices with 18% GST. From products, return name, price, and price * 1.18 rounded to 2 decimal places, named price_with_gst.
The shop wants the value of its stock. From products, return name, price, stock and price * stock named stock_value.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- WHERE: comparison and logicSELECT: filtering and sorting · same module
- IN, BETWEEN, LIKE and NULLSELECT: filtering and sorting · same module
- ORDER BY, LIMIT and OFFSETSELECT: filtering and sorting · same module
- SELECT and WHERE: finding rowsBackend Development · Databases and SQL basics
- INSERTChanging data, transactions and ACID
- UPDATE and DELETE safelyChanging data, transactions and ACID
Questions people ask about this
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.
A column is left out of an INSERT and has no DEFAULT and no NOT NULL. What value does it get?Related
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.
Which statement removes the table itself, not just its rows?Related
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
- 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.
- Module 2 exam: SELECT, filtering and sorting · 15 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Functions, CASE and NULL. 62 topics left in the course before the final exam.