Part 01 / Topic 6 of 10
Your first queries
SELECT is how you ask the database for values. It doesn't need a table — try these:
SELECT 1 + 1;
?column?
----------
2
(1 row)
?column? is Postgres saying "you didn't name this value". Name it with AS:
SELECT 'hello, SQL' AS greeting;
greeting
------------
hello, SQL
(1 row)
Note: text uses single quotes in SQL. Double quotes mean something else (column and table names — you will meet them in Part 2).
The database can compute, tell the time, and work with text:
SELECT now(); -- output varies per run
now
----------------------------------
2026-11-29 18:10:45.722817+05:30
(1 row)
SELECT 250 * 3 AS total_paise, 250 * 3 / 100 AS rupees;
total_paise | rupees
-------------+--------
750 | 7
(1 row)
Spot the surprise: 750 / 100 gave 7, not 7.5. Dividing integers keeps the result an integer — the same trap as in many programming languages. Make either side a decimal and you get the real answer:
SELECT 10 / 4 AS int_div, 10 / 4.0 AS num_div, 10 % 4 AS remainder;
int_div | num_div | remainder
---------+--------------------+-----------
2 | 2.5000000000000000 | 2
(1 row)
Text joins with ||, and functions work like you'd hope:
SELECT 'Asha' || ' from ' || 'Pune' AS intro, upper('postgres') AS shouted;
intro | shouted
----------------+----------
Asha from Pune | POSTGRES
(1 row)
🧪 Quick quiz — answer from memory, then check.
- What does
SELECT 7 / 2;return? - How do you give a selected value a readable column name?
Show the answers
3— both sides are integers, so the division stays an integer.7 / 2.0gives3.5.- With
AS:SELECT 7 / 2.0 AS half;
Done here? The practice set for Part 1 — 6 exercises with solutions — is on the part page.