Reading (Querying) Data in PostgreSQL

Simple Querying

To select all rows and columns from a given table:

SELECT * FROM table_name;

To select all rows but selected columns only:

SELECT column_name, column_name FROM table_name;

Conditional Querying

To select all columns but only rows meeting a specific condition:

SELECT * FROM table_name WHERE condition;

To select only selected columns only from rows meeting a specific condition:

SELECT column_name, column_name FROM table_name WHERE condition;

Distinctive Querying

To get distinct values from a given column:

SELECT DISTINCT column_name FROM table_name;

Aliasing

To temporarily rename a column name in querying results:

SELECT column_name AS new_temporary_column_name FROM table_name;

Pattern Matching

To select only columns meeting a specific pattern:

SELECT * table_name WHERE column_name LIKE pattern;

In a pattern, _ stands for any single character and % stands for `zero or more characters.

LIKE pattern always regards an entire string. Therefore to look for a match within an entire string the pattern should start and end with %.

We use cookies and similar technologies to enhance the quality of services, maintain statistics and adjust marketing content. You will find more information in the Cookies Policy.

By clicking OK you grant consent to processing of your personal data by us and our Trusted Partners with the purpose of maintain statistics and adjustment of the marketing content pursuant to the Privacy Policy. If you wish to not grant that consent and/or limit its extent click Settings.