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
dreamhost

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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 7 How to disable right click and drag and drop of images using jQuery? May 7 How to render Lists in React? May 7 What’s the difference between variables in CSS and SCSS (Sass)? May 7 How to define variables in SCSS (Sass)? May 7 How to show and hide an element on click in React? May 5 Use inline if to make a shorter conditional syntax in React

You might also like these

How to check if the page is single post page in WordPress?WordPressSimple Page Hit Counter in PHPPHPHow to check if a link has http or https in it in JavaScript?JavaScriptHow to swap images on hover using CSS?CSSSelf-Driving and Intelligent NetworksMiscHow to insert Bootstrap 4 in Angular 9 app?Angular