Nishaglobal Education logo

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.

Lesson 2 of 1220 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. Choosing columns
  3. Renaming columns with AS
  4. Calculated columns
  5. Writing readable SQL
  6. Practice
  7. Common mistakes
  8. Recap
  9. Quiz
  10. Mark lesson complete

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:

SQL
SELECT name, city FROM customers;

Result: 10 rows

Query result
namecity
Asha PatelMumbai
Ravi KumarBengaluru
Meera NairKochi
John SmithLondon
Fatima KhanDubai
Li WeiSingapore
Sara LopezToronto
Arjun SinghDelhi
Emma BrownSydney
Kofi MensahNULL

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.

SQL
SELECT name AS product, price AS price_inr FROM products;

Result: 8 rows

Query result
productprice_inr
Notebook A5120
Gel Pens (pack of 5)90
Python Basics Book450
SQL Pocket Guide350
Desk Lamp1200
USB-C Cable299
Water Bottle399
Coffee Mug249

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:

SQL
SELECT name, price, ROUND(price * 1.18, 2) AS price_with_gst
FROM products;

Result: 8 rows

Query result
namepriceprice_with_gst
Notebook A5120141.6
Gel Pens (pack of 5)90106.2
Python Basics Book450531
SQL Pocket Guide350413
Desk Lamp12001416
USB-C Cable299352.82
Water Bottle399470.82
Coffee Mug249293.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

HabitExample
Write SQL keywords in CAPITALS and names in lowercaseSELECT name FROM products
Put each main part on its own lineSELECT ... 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.

  1. Start with SELECT.
  2. Write name AS item, then a comma, then category AS type.
  3. Finish with FROM products;.
Show the solution
Solution
SELECT name AS item, category AS type
FROM products;

Result: 8 rows

Query result
itemtype
Notebook A5Stationery
Gel Pens (pack of 5)Stationery
Python Basics BookBooks
SQL Pocket GuideBooks
Desk LampElectronics
USB-C CableElectronics
Water BottleHome
Coffee MugHome

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
Solution
SELECT name, price, price * 0.9 AS sale_price
FROM products;

Result: 8 rows

Query result
namepricesale_price
Notebook A5120108
Gel Pens (pack of 5)9081
Python Basics Book450405
SQL Pocket Guide350315
Desk Lamp12001080
USB-C Cable299269.1
Water Bottle399359.1
Coffee Mug249224.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.
  • AS renames 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. 1. Which query shows only the name and email of every customer?

  2. 2. What does `AS` do in `SELECT price AS cost FROM products;`?

  3. 3. In SQLite, what is `9 / 2`?

  4. 4. What does `ROUND(352.8199, 2)` give?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.