At this point, we have successfully stored data inside the cities table. Now the next step is to retrieve that data from the database. To do this, PostgreSQL provides the
MSMuhammad SufiyanSoftware Engineer · Jul 26
Backend Engineering HubT-
At this point, we have successfully stored data inside the cities table.
Now the next step is to retrieve that data from the database.
To do this, PostgreSQL provides the SELECT statement.
The Basic SELECT Query
The simplest way to fetch data from a table is:
select * from cities;
When we run this query, PostgreSQL returns all rows from the cities table.
You should see all the cities we inserted earlier:
Tokyo
Delhi
Shanghai
Sao Paulo
What Does * Mean?
The * symbol (star) is a shortcut.
It means:
“Select all columns from the table.”
So:
select * from cities;
is the same as saying:
“Give me every column and every row from the cities table.”
This is very useful when:
exploring a table
checking inserted data
learning the structure of a table
Selecting Specific Columns
Instead of fetching all columns, we can choose only the columns we need.
Example: Name and Country
select name, country from cities;
This query returns:
only the name column
and the country column
All other columns are ignored.
Example: Name and Population
select name, population from cities;
Again, only the specified columns are returned.
Selecting More Than Two Columns
There is no limit to how many columns you can select.
select name, population, area from cities;
You can list as many columns as you want, as long as they exist in the table.
Changing Column Order
The order of columns in the result depends on how you write the query, not how the table was created.
For example:
select area, name, population from cities;
PostgreSQL will return:
1. area 2. name 3. population
Even though the table itself was defined in a different order.
Selecting the Same Column Multiple Times
You can even select the same column more than once:
select name, name, name from cities;
This will return the name column three times.
At first, this might look pointless, but it can be useful later when:
working with expressions
creating calculated columns
formatting output
Basic SELECT Syntax Recap
The general structure is:
select column1, column2, ...
from table_name;
Or, to get everything:
select * from table_name;
Summary
SELECT is used to retrieve data from a table
* means all columns
You can select specific columns
Column order in results is flexible
Columns can even be repeated
Now that we know how to fetch data, the next step is to filter it.
What’s Next?
In the next blog, we’ll learn how to:
filter rows using WHERE
retrieve only specific records from a table
Just send the next topic whenever you’re ready 👍
HIRINGMINE CAREER SIGNAL
This writing is proof of expertise.
Explore the author’s verified skills, projects and availability—or start a professional conversation.