Yogesh Chauhan's Blog

Select statement in Postgres with examples

in Postgres on April 22, 2020

SELECT statement syntax


SELECT column1, column2, ... FROM table;

OR 

SELECT * FROM table;

NOTE: (;) at the end of the SELECT statement is not a part of the SQL statement. It's just tells PostgreSQL engine the end of an SQL statement. You can use the semicolon to separate two SQL statements as well.

Database

Before we start, here are the links of the databases that I am using.

Northwind Data

Let's select one column from customers table

Postgres has the same way as SQL to select data from database tables.


SELECT contact_name FROM customers;

Select statement in Postgres with examples
Select statement in Postgres with examples

Let's select more than one columns from customers.

We can just add as many columns as we want to get those columns in results of the SELECT query in Postgres. Same as SQL.


SELECT customer_id, contact_name, contact_title  FROM customers;

Select statement in Postgres with examples
Select statement in Postgres with examples

What about all columns?

For all columns, just like SQL, use SELECT * from table_name.


SELECT *  FROM customers;

Select statement in Postgres with examples
Select statement in Postgres with examples

Can we make a full address by concating two or more columns?

Let's try that on address by using SELECT statement with expressions.


SELECT address || city || postal_code AS Full_address FROM customers;

Select statement in Postgres with examples
Select statement in Postgres with examples

Seems like we can do that. I have not added the comma (,) in between the name of the city and zip code and address but we can always make it nice and more readable.

What about removing duplicates?

We can use DISTINCT with SELECT just like SQL.


SELECT DISTINCT contact_name FROM customers;

Select statement in Postgres with examples
Select statement in Postgres with examples

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to add a Bar Chart in Angular App?AngularCreate a currency (dollar) to coins convertor in ReactReactQuery to increment or decrement value in MySQL ignoring negative valuesSQL/MySQLKilling A Project Part 4: What Would Be Your Top 3 Priorities For Planning The Cancellation?MiscellaneousHow to hide and show menu on scroll using JavaScript and CSS?CSSArray.from() Method in JavaScriptJavaScript