Example 1
Count how many distinct customers placed orders
There are 4 rows but only 3 distinct customers — Alice appears twice. COUNT(DISTINCT customer) returns 3. COUNT(*) on the same table would return 4.
Source table data Rows loaded before the example query runs.
Setup
CREATE TABLE orders (id INT, customer VARCHAR(50), status VARCHAR(20));
INSERT INTO
orders (id, customer, status)
VALUES
(1, 'Alice', 'shipped'),
(2, 'Alice', 'pending'),
(3, 'Bob', 'shipped'),
(4, 'Carol', 'shipped');Validated query Shared across supported engines.
SQL
SELECT
COUNT(DISTINCT customer) AS unique_customers
FROM
orders;Expected result Returned rows for the shared example.
| unique_customers |
|---|
| 3 |
Output is identical across all engines.