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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Solution to Could not cast value of type ‘NSTaggedPointerString’ to ‘NSNumber’SwiftModules and its Core features in JavaScriptJavaScriptHow to use a Subquery to Insert Multiple Rows in SQL Table?SQL/MySQLNATURAL JOIN in PostgresPostgresIntroduction to components and templates Part 3: Data bindingAngularHow to create a simple slider with CSS and jQuery?CSS