

The query returns the unique combination of bcolor and fcolor from the distinct_demo table.
Postgresql insert into select how to#
The following statement demonstrates how to use the DISTINCT clause on multiple columns: SELECT DISTINCT bcolor,įcolor Code language: SQL (Structured Query Language) ( sql )īecause we specified both bcolor and fcolor columns in the SELECT DISTINCT clause, PostgreSQL combined the values in both bcolor and fcolor columns to evaluate the uniqueness of the rows. SELECT DISTINCT bcolorīcolor Code language: SQL (Structured Query Language) ( sql ) PostgreSQL DISTINCT multiple columns The following statement selects unique values in the bcolor column from the t1 table and sorts the result set in alphabetical order by using the ORDER BY clause. Third, query the data from the distinct_demo table using the SELECT statement: SELECT id,ĭistinct_demo Code language: SQL (Structured Query Language) ( sql ) PostgreSQL DISTINCT one column example ( 'blue', 'blue') Code language: SQL (Structured Query Language) ( sql ) Second, insert some rows into the distinct_demo table using the following INSERT statement: INSERT INTO distinct_demo (bcolor, fcolor) ) Code language: SQL (Structured Query Language) ( sql ) In this tutorial, you just execute the statement in psql or pgAdmin to execute the statements.įirst, use the following CREATE TABLE statement to create the distinct_demo table that consists of three columns: id, bcolorand fcolor. Note that you will learn how to create a table and insert data into a table in the subsequent tutorial. Let’s create a new table called distinct_demo and insert data into it for practicing the DISTINCT clause.

Notice that the DISTINCT ON expression must match the leftmost expression in the ORDER BY clause. It is a good practice to always use the ORDER BY clause with the DISTINCT ON(expression) to make the result set predictable. The order of rows returned from the SELECT statement is unspecified therefore the “first” row of each group of the duplicate is also unspecified. PostgreSQL also provides the DISTINCT ON (expression) to keep the “first” row of each group of duplicates using the following syntax: SELECT DISTINCT ON (column1) column_alias,Ĭolumn2 Code language: SQL (Structured Query Language) ( sql ) In this case, the combination of values in both column1 and column2 columns will be used for evaluating the duplicate. If you specify multiple columns, the DISTINCT clause will evaluate the duplicate based on the combination of values of these columns. In this statement, the values in the column1 column are used to evaluate the duplicate.

Table_name Code language: SQL (Structured Query Language) ( sql ) The following illustrates the syntax of the DISTINCT clause: SELECT DISTINCT column1 The DISTINCTclause can be applied to one or more columns in the select list of the SELECT statement. The DISTINCT clause keeps one row for each group of duplicates. The DISTINCT clause is used in the SELECT statement to remove duplicate rows from a result set. Introduction to PostgreSQL SELECT DISTINCT clause

Summary: in this tutorial, you will learn how to use the PostgreSQL SELECT DISTINCT clause to remove duplicate rows from a result set returned by a query.
