SELECT: choosing the columns you need
Choose specific columns with SQL SELECT, rename them with AS, calculate new columns such as price with GST, and write readable queries with comments.
🎯 By the end of this lesson you will be able to:
- Select only the columns you need, in the order you want
- Rename columns in the result with AS
- Calculate new columns with arithmetic and ROUND()
Outline
What you'll learn and why it matters
Real tables can have dozens of columns, and you rarely need them all. Asking only for what you need makes results easier to read and queries faster. In this lesson you also learn to create brand-new columns by calculating them, which is how reports show things like totals and prices with tax.
Choosing columns
Instead of *, list the columns you want, separated by commas. They appear in the order you list them:
SELECT name, city FROM customers;Result: 10 rows
| name | city |
|---|---|
| Asha Patel | Mumbai |
| Ravi Kumar | Bengaluru |
| Meera Nair | Kochi |
| John Smith | London |
| Fatima Khan | Dubai |
| Li Wei | Singapore |
| Sara Lopez | Toronto |
| Arjun Singh | Delhi |
| Emma Brown | Sydney |
| Kofi Mensah | NULL |
Every row is still returned, but only with the two columns you asked for.
Renaming columns with AS
AS gives a column a different name in the result (an alias). The table itself is not changed.
SELECT name AS product, price AS price_inr FROM products;Result: 8 rows
| product | price_inr |
|---|---|
| Notebook A5 | 120 |
| Gel Pens (pack of 5) | 90 |
| Python Basics Book | 450 |
| SQL Pocket Guide | 350 |
| Desk Lamp | 1200 |
| USB-C Cable | 299 |
| Water Bottle | 399 |
| Coffee Mug | 249 |
Calculated columns
You can do arithmetic in a SELECT with +, -, * and /. Here we add 18% GST to every price. ROUND(value, 2) rounds to 2 decimal places:
SELECT name, price, ROUND(price * 1.18, 2) AS price_with_gst
FROM products;Result: 8 rows
| name | price | price_with_gst |
|---|---|---|
| Notebook A5 | 120 | 141.6 |
| Gel Pens (pack of 5) | 90 | 106.2 |
| Python Basics Book | 450 | 531 |
| SQL Pocket Guide | 350 | 413 |
| Desk Lamp | 1200 | 1416 |
| USB-C Cable | 299 | 352.82 |
| Water Bottle | 399 | 470.82 |
| Coffee Mug | 249 | 293.82 |
Walk through one row: the Notebook costs 120, and 120 × 1.18 = 141.6. The database works this out for every row and shows it in a new column called price_with_gst. Without AS, the column would be named after the whole expression, which is hard to read. (DB Browser may show whole numbers such as 531 as 531.0; it is the same value.)
Writing readable SQL
| Habit | Example |
|---|---|
| Write SQL keywords in CAPITALS and names in lowercase | SELECT name FROM products |
| Put each main part on its own line | SELECT ... on one line, FROM ... on the next |
Explain with comments: everything after -- on a line is ignored | -- Prices including 18% GST |
End every statement with ; | SELECT * FROM products; |
SQL keywords are not case-sensitive, so select works too, but capitals make queries much easier to scan.
Practice
Guided exercise
Product list
Show each product's name and category, with the columns called item and type.
- Start with
SELECT. - Write
name AS item, then a comma, thencategory AS type. - Finish with
FROM products;.
Show the solution
SELECT name AS item, category AS type
FROM products;Result: 8 rows
| item | type |
|---|---|
| Notebook A5 | Stationery |
| Gel Pens (pack of 5) | Stationery |
| Python Basics Book | Books |
| SQL Pocket Guide | Books |
| Desk Lamp | Electronics |
| USB-C Cable | Electronics |
| Water Bottle | Home |
| Coffee Mug | Home |
How to get there: Each column in the list can have its own alias. Commas go between columns, not after the last one.
Your turn
Sale prices
The shop is having a 10% sale. Show each product's name, its price, and a column sale_price that is 90% of the price.
Show the solution
SELECT name, price, price * 0.9 AS sale_price
FROM products;Result: 8 rows
| name | price | sale_price |
|---|---|---|
| Notebook A5 | 120 | 108 |
| Gel Pens (pack of 5) | 90 | 81 |
| Python Basics Book | 450 | 405 |
| SQL Pocket Guide | 350 | 315 |
| Desk Lamp | 1200 | 1080 |
| USB-C Cable | 299 | 269.1 |
| Water Bottle | 399 | 359.1 |
| Coffee Mug | 249 | 224.1 |
How to get there: Ten percent off means paying 90%, so multiply by 0.9. price - price * 0.1 gives the same answer.
Common mistakes
A comma after the last column: SELECT name, city, FROM customers
You see: near "FROM": syntax error
Fix: Commas go between columns only.
Forgetting a comma between columns: SELECT name city
You see: No error, but one column is shown and called city
Fix: Without a comma, the second word is treated as an alias. Add the comma.
Misspelling a column
You see: no such column: nmae
Fix: Check the column names in the Database Structure tab.
Expecting AS to rename the column in the table
You see: The table still has the old name
Fix: Aliases only affect the result of this query.
Recap
- List the columns you need after
SELECT, separated by commas. ASrenames a column in the result without changing the table.- Calculate new columns with arithmetic, and round them with
ROUND(value, 2). - Write keywords in capitals, one clause per line, with
--comments.
Key terms
- Alias
- A temporary name for a column (or table) in a query, given with AS.
- Calculated column
- A column in a result that is worked out from other values, such as price * 1.18.
- Comment
- Text after -- that the database ignores.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Which query shows only the name and email of every customer?
2. What does `AS` do in `SELECT price AS cost FROM products;`?
3. In SQLite, what is `9 / 2`?
4. What does `ROUND(352.8199, 2)` give?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.