Concept Flow - DISTINCT for unique values
Start Query
Read Table Data
Apply DISTINCT
Remove Duplicate Rows
Return Unique Rows
End Query
The query reads data from a table, applies DISTINCT to remove duplicates, and returns only unique rows.
SELECT DISTINCT city FROM customers;
| Step | Action | Input Data | Resulting Data |
|---|---|---|---|
| 1 | Read all city values from customers | [New York, Boston, New York, Chicago, Boston] | [New York, Boston, New York, Chicago, Boston] |
| 2 | Apply DISTINCT to remove duplicates | [New York, Boston, New York, Chicago, Boston] | [New York, Boston, Chicago] |
| 3 | Return unique city list | [New York, Boston, Chicago] | [New York, Boston, Chicago] |
| 4 | End query | - | - |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| city_list | empty | [New York, Boston, New York, Chicago, Boston] | [New York, Boston, Chicago] | [New York, Boston, Chicago] |
DISTINCT keyword removes duplicate rows from query results. Syntax: SELECT DISTINCT column_name FROM table; It returns only unique values of the selected columns. Duplicates are removed based on all selected columns combined. Useful to find unique entries in a column or set of columns.
DISTINCT do in a query?Customers?Orders with a column Product containing values: ['Pen', 'Pen', 'Book', 'Notebook', 'Book'], what will the query SELECT DISTINCT Product FROM Orders; return?Customers table?SELECT DISTINCT name, age FROM Customers;Sales with columns Region and SalesPerson. You want to find all unique salespersons regardless of region. Which query gives the correct result?Pass the test and this pattern moves unknown