The SQL DISTINCT keyword retrieves unique values from a column or set of columns. Use it to remove duplicate rows from the result set of a SELECT query with any data type.
Here is the basic syntax:
SELECT DISTINCT column1, column2, column3
Plus, it is often used with aggregate functions such as COUNT, SUM, AVG, and MAX to get the count, sum, average, or maximum value of a unique set of values. For example, you can use the following query to count the number of genre values in the bookshop table.
SELECT COUNT(DISTINCT genre) FROM bookshop;
As a result, you would get the total number of unique book genres. It is important to note that using the DISTINCT keyword can increase the query’s execution time, especially if the table has multiple duplicate values.