SELECT: Choosing Columns
List the columns you want after SELECT, separated by commas. The result shows them in that order.
SELECT is the question "show me...". You choose which columns you want and from which table. Every report, dashboard and app screen built on a database starts with a SELECT. Asking only for the columns you need keeps results small and fast.
Example
SELECT name, price FROM products;
-- Result:
-- name | price
-- -------------------+------
-- Cotton T-shirt | 499
-- Denim Jeans | 1299
-- Wireless Earbuds | 1499
-- Smart Watch | 3999
-- Steel Water Bottle | 349
-- Table Lamp | 899
-- Notebook Pack | 199
-- Python Book | 599
-- Yoga Mat | 799▶ Try it yourselfCalculations and new names (AS)
You can calculate in the SELECT and give the result a clear name with AS.
Example
SELECT name,
price,
ROUND(price * 1.18, 2) AS price_with_gst,
price * stock AS stock_value
FROM products;
-- Result:
-- name | price | price_with_gst | stock_value
-- -------------------+-------+----------------+------------
-- Cotton T-shirt | 499 | 588.82 | 59880
-- Denim Jeans | 1299 | 1532.82 | 77940
-- Wireless Earbuds | 1499 | 1768.82 | 52465
-- Smart Watch | 3999 | 4718.82 | 0
-- Steel Water Bottle | 349 | 411.82 | 69800
-- Table Lamp | 899 | 1060.82 | 22475
-- Notebook Pack | 199 | 234.82 | 59700
-- Python Book | 599 | 706.82 | 23960
-- Yoga Mat | 799 | 942.82 | 11985▶ Try it yourselfDISTINCT: no repeats
Example
SELECT DISTINCT category FROM products;
-- Result:
-- category
-- -----------
-- Fashion
-- Electronics
-- Home
-- Books
-- Sports▶ Try it yourselfOnly ask for the columns you need. On big tables,
SELECT * is slower and sends data nobody uses.Key points
- SELECT columns FROM table.
- Use * only when exploring.
- AS gives columns friendly names.
📝 Test yourself
1. How do you rename a column in the result?
2. What does DISTINCT do?
3. Fill in the keyword to show each city only once:
SELECT city FROM customers;
