ShareYourGroupShareYourGroupSQL

Lesson 2 of 26 · see the practice database

Choosing columns

SELECT * returns every column. That is handy for a first look, but most of the time you want only a few. A real table may have fifty columns, and nobody wants to read all of them to find a name and a phone number.

Naming the columns you want

List the column names after SELECT, with commas between them.

SELECT name, city FROM customers;

Result

namecity
Asha VermaLudhiana
Ravi KumarDelhi
Meena GillAmritsar
Kabir KhanMumbai
Sunita RaoChennai
Arjun MehtaPune
Priya NairKochi
Imran AliDelhi
Neha JoshiJaipur
Tara SinghLudhiana
Vikram DasKolkata
Joy ThomasMumbai

12 rows

The result has only those two columns, in the order you listed them. You choose the order. It does not have to match the table.

SELECT price, name FROM products;

Result

pricename
650Basmati Rice 5 kg
160Toor Dal 1 kg
145Sunflower Oil 1 L
280Tea 500 g
60Notebook 200 pages
90Ball Pen pack of 10
120Geometry Box
750School Bag
110LED Bulb 9 W
399Extension Board
499Earphones
1299Power Bank

16 rows

Showing the first 12 of 16 rows.

A missing comma or a misspelt column gives an error.

SELECT name, town FROM customers;

What the database says

Error: no such column: town

There is no column called town. The message tells you exactly that.

Why not always use the star?

Use * when exploring. Name your columns in anything you keep.

Renaming a column with AS

AS gives a column a different heading in the result. The new name is called an alias. The table itself is not changed.

SELECT name AS product, price AS rupees
FROM products;

Result

productrupees
Basmati Rice 5 kg650
Toor Dal 1 kg160
Sunflower Oil 1 L145
Tea 500 g280
Notebook 200 pages60
Ball Pen pack of 1090
Geometry Box120
School Bag750
LED Bulb 9 W110
Extension Board399
Earphones499
Power Bank1299

16 rows

Showing the first 12 of 16 rows.

If the alias contains a space, put it in double quotation marks.

SELECT name AS "Customer name", city AS "Home city"
FROM customers;

Result

Customer nameHome city
Asha VermaLudhiana
Ravi KumarDelhi
Meena GillAmritsar
Kabir KhanMumbai
Sunita RaoChennai
Arjun MehtaPune
Priya NairKochi
Imran AliDelhi
Neha JoshiJaipur
Tara SinghLudhiana
Vikram DasKolkata
Joy ThomasMumbai

12 rows

Calculations

A column in the result does not have to be a column of the table. It can be worked out. Here each price is shown with 18 percent added.

SELECT name, price, price * 1.18 AS price_with_tax
FROM products;

Result

namepriceprice_with_tax
Basmati Rice 5 kg650767.0
Toor Dal 1 kg160188.79999999999998
Sunflower Oil 1 L145171.1
Tea 500 g280330.4
Notebook 200 pages6070.8
Ball Pen pack of 1090106.19999999999999
Geometry Box120141.6
School Bag750885.0
LED Bulb 9 W110129.79999999999998
Extension Board399470.82
Earphones499588.8199999999999
Power Bank12991532.82

16 rows

Showing the first 12 of 16 rows.

The arithmetic signs are the usual ones: +, -, * for multiply and / for divide. Without the alias, the heading would be the sum itself, which is ugly. Always name a calculated column.

You can use two columns in one calculation. This is the value of the stock of each product.

SELECT name, price, stock, price * stock AS stock_value
FROM products;

Result

namepricestockstock_value
Basmati Rice 5 kg6504026000
Toor Dal 1 kg1607512000
Sunflower Oil 1 L145608700
Tea 500 g28000
Notebook 200 pages6030018000
Ball Pen pack of 109015013500
Geometry Box120455400
School Bag7502015000
LED Bulb 9 W11020022000
Extension Board3993513965
Earphones49900
Power Bank12991823382

16 rows

Showing the first 12 of 16 rows.

A warning about dividing whole numbers

In SQLite, and in PostgreSQL and SQL Server, dividing one whole number by another gives a whole number. The fraction is dropped.

SELECT 7 / 2 AS whole, 7 / 2.0 AS with_decimals;

Result

wholewith_decimals
33.5

1 row

To keep the decimals, make one of the numbers a decimal, as in 2.0. MySQL and Oracle keep the decimals by themselves. This small difference causes many wrong averages, so remember it.

Notice also that this query has no FROM. In SQLite, MySQL, PostgreSQL and SQL Server, you can use SELECT as a calculator.

Fixed text in a column

A value in single quotation marks is a piece of text. It is repeated on every row.

SELECT name, 'INR' AS currency, price
FROM products;

Result

namecurrencyprice
Basmati Rice 5 kgINR650
Toor Dal 1 kgINR160
Sunflower Oil 1 LINR145
Tea 500 gINR280
Notebook 200 pagesINR60
Ball Pen pack of 10INR90
Geometry BoxINR120
School BagINR750
LED Bulb 9 WINR110
Extension BoardINR399
EarphonesINR499
Power BankINR1299

16 rows

Showing the first 12 of 16 rows.

Text always goes in single quotation marks in SQL. Double quotation marks are for names of columns and tables.

Joining text together

The operator || glues pieces of text together. This is standard SQL and works in SQLite, PostgreSQL and Oracle.

SELECT name || ' (' || city || ')' AS customer
FROM customers;

Result

customer
Asha Verma (Ludhiana)
Ravi Kumar (Delhi)
Meena Gill (Amritsar)
Kabir Khan (Mumbai)
Sunita Rao (Chennai)
Arjun Mehta (Pune)
Priya Nair (Kochi)
Imran Ali (Delhi)
Neha Joshi (Jaipur)
Tara Singh (Ludhiana)
Vikram Das (Kolkata)
Joy Thomas (Mumbai)

12 rows

In MySQL, use CONCAT(name, ' (', city, ')'). In SQL Server, use + or CONCAT.

How a simple query is laid out

SELECT column1, column2, calculation AS alias
FROM table_name;

As queries grow, each new part gets a line of its own. That layout makes long queries easy to read from top to bottom.

Try it yourself

Exercise 1. Show only the name and salary of every employee.

Solve it in the practice box

Show one answer
SELECT name, salary FROM employees;
namesalary
Anita Kapoor95000
Rohit Bansal60000
Deepa Iyer48000
Manoj Yadav48000
Farah Sheikh42000
Gopal Reddy30000
Harpreet Kaur36000
Nikhil Jain33000

8 rows

Exercise 2. Show each product's name and its stock, with the stock column renamed to in_stock.

Solve it in the practice box

Show one answer
SELECT name, stock AS in_stock FROM products;
namein_stock
Basmati Rice 5 kg40
Toor Dal 1 kg75
Sunflower Oil 1 L60
Tea 500 g0
Notebook 200 pages300
Ball Pen pack of 10150
Geometry Box45
School Bag20
LED Bulb 9 W200
Extension Board35
Earphones0
Power Bank18

16 rows

Showing the first 12 of 16 rows.

Exercise 3. Show each employee's name and yearly salary. The table holds the monthly salary. Name the new column yearly.

Solve it in the practice box

Show one answer
SELECT name, salary * 12 AS yearly FROM employees;
nameyearly
Anita Kapoor1140000
Rohit Bansal720000
Deepa Iyer576000
Manoj Yadav576000
Farah Sheikh504000
Gopal Reddy360000
Harpreet Kaur432000
Nikhil Jain396000

8 rows

Examples on this page were run with SQLite 3.50.4 on 12 November 2026. The results shown are the real results.

This is an independent learning site. It is not connected with the makers of any database product. MySQL, PostgreSQL, SQL Server, Oracle and SQLite are named only so that you know where a piece of SQL works. The shop, its customers and its staff in the practice database are made up.