PostgreSQL SELECT DISTINCT

Use SELECT DISTINCT to return only unique values from a column or a combination of columns, removing duplicates from the result set.

Key Topics

1. Basic Usage

SELECT DISTINCT column_name
FROM table_name;

This returns each unique value once.

2. Multiple Columns

SELECT DISTINCT column1, column2
FROM table_name;

The combination of column1 and column2 values is considered unique.

Best Practices

  • Use SELECT DISTINCT sparingly if you only need to remove duplicates; consider data design issues that cause duplication.
  • Combine DISTINCT with well-chosen indexes to improve performance.

Key Takeaways

  • SELECT DISTINCT eliminates duplicate rows in the result set.
  • Multiple columns are evaluated together for uniqueness.