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
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.
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.
Select distinct values from a single column:
SELECT DISTINCT department FROM employees;
This will retrieve all unique department
values from the employees
table.
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.
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'.
Counting distinct values:
SELECT COUNT(DISTINCT department) FROM employees;
This query counts the number of unique department
values in the employees
table.
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.
How to Use SELECT DISTINCT in PostgreSQL:
SELECT DISTINCT
is used in PostgreSQL to retrieve unique values from a specified column in a table.SELECT DISTINCT column_name FROM your_table;
SELECT DISTINCT with Multiple Columns in PostgreSQL:
SELECT DISTINCT
with multiple columns to retrieve unique combinations of values across those columns.SELECT DISTINCT column1, column2 FROM your_table;
Filtering NULL Values with SELECT DISTINCT in PostgreSQL:
SELECT DISTINCT
, you can add a WHERE
clause.SELECT DISTINCT column_name FROM your_table WHERE column_name IS NOT NULL;
Using WHERE Clause with SELECT DISTINCT in PostgreSQL:
WHERE
clause to filter the results of SELECT DISTINCT
.SELECT DISTINCT column_name FROM your_table WHERE condition;
ORDER BY Clause with SELECT DISTINCT in PostgreSQL:
ORDER BY
clause along with SELECT DISTINCT
.SELECT DISTINCT column_name FROM your_table ORDER BY column_name;
LIMIT and OFFSET with SELECT DISTINCT in PostgreSQL:
LIMIT
and OFFSET
.SELECT DISTINCT column_name FROM your_table LIMIT 10 OFFSET 5;
SELECT DISTINCT and JOIN Operations in PostgreSQL:
SELECT DISTINCT
with JOIN operations to retrieve unique values from multiple tables.SELECT DISTINCT t1.column_name FROM table1 t1 INNER JOIN table2 t2 ON t1.id = t2.id;
Combining SELECT DISTINCT with Other Aggregate Functions in PostgreSQL:
SELECT DISTINCT
with aggregate functions like COUNT, AVG, etc., for more advanced queries.SELECT DISTINCT column_name, COUNT(*) FROM your_table GROUP BY column_name;
Using SELECT DISTINCT in Subqueries in PostgreSQL:
SELECT DISTINCT
can be used within subqueries to filter unique values for further processing.SELECT * FROM your_table WHERE column_name IN (SELECT DISTINCT column_name FROM other_table);
SELECT DISTINCT vs. GROUP BY in PostgreSQL:
SELECT DISTINCT
and GROUP BY
for achieving distinct values.SELECT column_name FROM your_table GROUP BY column_name;
Using SELECT DISTINCT with Window Functions in PostgreSQL:
SELECT DISTINCT
for more advanced analytical queries.SELECT DISTINCT column_name, ROW_NUMBER() OVER (PARTITION BY column_name ORDER BY some_column) FROM your_table;
SELECT DISTINCT and UNION/UNION ALL in PostgreSQL:
UNION
or include duplicates using UNION ALL
.SELECT DISTINCT column_name FROM table1 UNION SELECT DISTINCT column_name FROM table2;
Comparing SELECT DISTINCT and SELECT with GROUP BY in PostgreSQL:
SELECT DISTINCT
with GROUP BY
and understand their implications.SELECT column_name FROM your_table GROUP BY column_name;