Back to Yugabyte Db

SELECT DISTINCT

community/learn/content/01-SQL-Basics/02-fundamentals/03-select-distinct.md

2026.1.0.0-b29881 B
Original Source

SELECT with DISTINCT on one column

In this exercise we query the orders table and return only the distinct values in the order_id column.

SELECT DISTINCT order_id
FROM orders;

The query should return 830 rows

SELECT with DISTINCT including multiple columns

In this exercise we query the orders table and return only the distinct values based on combining the specified columns.

SELECT DISTINCT order_id,
                customer_id,
                employee_id
FROM orders;

The query should return 830 rows

SELECT with DISTINCT ON expression

In this exercise we query the orders table and ask to keep just the first row of each group of duplicates.

SELECT DISTINCT ON (employee_id)employee_id AS id_number,
                   customer_id
FROM orders
ORDER BY id_number,
         customer_id;

The query should return 9 rows