PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - SELECT DISTINCT clause

The SELECT DISTINCT clause in PostgreSQL is used to retrieve unique values from a table, eliminating all duplicate rows. This can be particularly useful when you want to know all the distinct values present in a column or a set of columns.

Basic Syntax:

SELECT DISTINCT column1, column2, ...
FROM table_name
WHERE condition;
  • column1, column2, ...: The columns from which you want to select the distinct values.

  • table_name: The name of the table from which you want to retrieve data.

  • WHERE condition: Optional. It can be used to filter the results further.

Examples:

  1. Select distinct values from a single column:

    SELECT DISTINCT department FROM employees;
    

    This will retrieve all unique department values from the employees table.

  2. Select distinct values from multiple columns:

    SELECT DISTINCT department, job_title FROM employees;
    

    This retrieves unique combinations of department and job_title from the employees table. For instance, if there are multiple employees in the "HR" department with a job title of "Manager", this query will show that combination just once.

  3. Using DISTINCT with WHERE clause:

    SELECT DISTINCT department FROM employees WHERE location = 'New York';
    

    This retrieves unique department values for employees located in 'New York'.

  4. Counting distinct values:

    SELECT COUNT(DISTINCT department) FROM employees;
    

    This query counts the number of unique department values in the employees table.

Notes:

  • When using DISTINCT on multiple columns, the combination of values in these columns is what needs to be unique, not the individual values in each column.

  • It's important to be cautious when using DISTINCT, especially on large datasets, as it can be resource-intensive. This is because PostgreSQL must compare every row to every other row to ensure distinctness.

  • If you only need to know the number of distinct values and not the values themselves, using COUNT(DISTINCT column_name) is more efficient than retrieving all distinct values and counting them.

In summary, the SELECT DISTINCT clause in PostgreSQL allows you to easily retrieve unique values or combinations of values from your tables, making it a valuable tool in various data analysis and reporting tasks.

  1. How to Use SELECT DISTINCT in PostgreSQL:

    • Description: SELECT DISTINCT is used in PostgreSQL to retrieve unique values from a specified column in a table.
    • Example Code:
      SELECT DISTINCT column_name FROM your_table;
      
  2. SELECT DISTINCT with Multiple Columns in PostgreSQL:

    • Description: You can use SELECT DISTINCT with multiple columns to retrieve unique combinations of values across those columns.
    • Example Code:
      SELECT DISTINCT column1, column2 FROM your_table;
      
  3. Filtering NULL Values with SELECT DISTINCT in PostgreSQL:

    • Description: To exclude NULL values when using SELECT DISTINCT, you can add a WHERE clause.
    • Example Code:
      SELECT DISTINCT column_name FROM your_table WHERE column_name IS NOT NULL;
      
  4. Using WHERE Clause with SELECT DISTINCT in PostgreSQL:

    • Description: You can apply additional conditions using the WHERE clause to filter the results of SELECT DISTINCT.
    • Example Code:
      SELECT DISTINCT column_name FROM your_table WHERE condition;
      
  5. ORDER BY Clause with SELECT DISTINCT in PostgreSQL:

    • Description: To order the distinct values, use the ORDER BY clause along with SELECT DISTINCT.
    • Example Code:
      SELECT DISTINCT column_name FROM your_table ORDER BY column_name;
      
  6. LIMIT and OFFSET with SELECT DISTINCT in PostgreSQL:

    • Description: Control the number of distinct values returned using LIMIT and OFFSET.
    • Example Code:
      SELECT DISTINCT column_name FROM your_table LIMIT 10 OFFSET 5;
      
  7. SELECT DISTINCT and JOIN Operations in PostgreSQL:

    • Description: Utilize SELECT DISTINCT with JOIN operations to retrieve unique values from multiple tables.
    • Example Code:
      SELECT DISTINCT t1.column_name FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id;
      
  8. Combining SELECT DISTINCT with Other Aggregate Functions in PostgreSQL:

    • Description: Combine SELECT DISTINCT with aggregate functions like COUNT, AVG, etc., for more advanced queries.
    • Example Code:
      SELECT DISTINCT column_name, COUNT(*) FROM your_table GROUP BY column_name;
      
  9. Using SELECT DISTINCT in Subqueries in PostgreSQL:

    • Description: SELECT DISTINCT can be used within subqueries to filter unique values for further processing.
    • Example Code:
      SELECT * FROM your_table WHERE column_name IN (SELECT DISTINCT column_name FROM other_table);
      
  10. SELECT DISTINCT vs. GROUP BY in PostgreSQL:

    • Description: Understand the differences between SELECT DISTINCT and GROUP BY for achieving distinct values.
    • Example Code:
      SELECT column_name FROM your_table GROUP BY column_name;
      
  11. Using SELECT DISTINCT with Window Functions in PostgreSQL:

    • Description: Integrate window functions with SELECT DISTINCT for more advanced analytical queries.
    • Example Code:
      SELECT DISTINCT column_name, ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY some_column) FROM your_table;
      
  12. SELECT DISTINCT and UNION/UNION ALL in PostgreSQL:

    • Description: Combine unique results from multiple queries using UNION or include duplicates using UNION ALL.
    • Example Code:
      SELECT DISTINCT column_name FROM table1
      UNION
      SELECT DISTINCT column_name FROM table2;
      
  13. Comparing SELECT DISTINCT and SELECT with GROUP BY in PostgreSQL:

    • Description: Compare the usage of SELECT DISTINCT with GROUP BY and understand their implications.
    • Example Code:
      SELECT column_name FROM your_table GROUP BY column_name;