YogeshChauhan . com

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
amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

Create dynamic selectors using SCSS (Sass)SCSSWhat’s a Web Storage API in JavaScript?JavaScriptHow to get Current Hour, Minute and Second in JavaScript?JavaScriptSafe Practice to add Links to cross-origin destinationUI/UXHow to use data-* Attributes in HTML?HTMLLearn how to add Scroll Indicator using CSS and JavaScript?CSS