Your First Queries
Run real SQL right now — compute values, name results, and understand the anatomy of a basic SELECT before we touch tables.
Enough concepts — let us write SQL. The wonderful thing about PostgreSQL is that you can run a query without any tables at all, which makes it a perfect, friendly calculator for learning the shape of the language.
SQL you can run with no tables
The keyword SELECT means "compute and give me back these values."
On its own, it can do arithmetic and produce text. Run this:
PostgreSQL evaluated 2 + 2 and handed back a one-row, one-column
result containing 4. You just ran a complete, valid SQL query.
Naming results with AS
That last result column had an unhelpful auto-generated name. You
can name a result column yourself with AS:
Notice three things that will recur all course long:
- You can
SELECTseveral values at once, separated by commas. - Text values go in single quotes:
'PostgreSQL'. Double quotes mean something different in SQL (they refer to names), so stick to single quotes for text. AS namelabels each result column. The label is for you — it does not change any stored data.
The anatomy of a query
Here is the basic shape you will type thousands of times, with the parts named:
SELECT— what you want back (columns or computed values).FROM— where to get it (which table).
When you are just computing values, like SELECT 2 + 2, you can
skip FROM entirely. The moment you want stored data, you add
FROM table_name.
SQL keywords and your style
SQL keywords like `SELECT` and `FROM` are not case-sensitive — `select`, `Select`, and `SELECT` all work. By strong convention, this course writes keywords in UPPERCASE and names in lowercase, because it makes queries far easier to read at a glance.
Statements end with a semicolon
Each SQL statement ends with a semicolon ;. It marks where one
instruction stops. With a single statement it is often optional, but
it becomes essential when you run several statements together — so we
will always include it as a good habit.
A first look at reading from a table
To prove the FROM half works, here is a tiny pre-loaded table.
Reading from it is just SELECT columns FROM the table:
Try editing it: replace name, population with * to get every
column, or with just country to get one. You are already steering
a query — choosing exactly what comes back. The whole Querying
Data section expands on this.
Check your understanding
What does the SELECT keyword do in a query?
It permanently deletes the chosen columns.
It specifies which columns or computed values you want the database to return.
It creates a brand-new table.
It sorts the table alphabetically.
In the query SELECT 5 * 3 AS total;, what is the role of AS total?
It multiplies the result by a value called total.
It stores the result permanently in a table named total.
It gives the resulting column the friendly name "total" in the output.
It is required or the query will fail.
Which part of a query tells the database which table to read from?
SELECT
FROM
AS
The semicolon