How to use SQL distinct Query in Magento 2?

Magento 2 distinct SQL Query is used to return results as unique records for the given field by skipping duplicate values from the SQL Response.

If a Specific Column in a table contains, multiple duplicate values and you want to skip it and fetch only single records for the same column, You can use distinct keywords in a query to filter out duplicate values.

You can Learn about, A List of Direct SQL Queries in Magento 2.

Just use distinct keywords on a query like, $connection->select()->distinct()

You can use a sample query that looks like this,

$connection = $this->resourceConnection->getConnection();

$select = $connection->select()->distinct()
            ->where('image_id > ?', $imageId)
            ->limit(10);

$select->from(TABLE_NAME);
return $connection->fetchCol($select);

Here, We have used a distinct() function that will be used to fetch distinct records from a given SQL Query.