InterBase Quick Start: Part IV - Removing Duplicate Rows with DISTINCT
Go Up to InterBase Quick Start: Part IV - Retrieving Data
Columns often contain duplicate entries. The DISTINCT keyword allows you to remove duplicate values from the result. It enables you to retrieve only distinct (different) values.
Selecting One of Each
- If you want to retrieve all the job codes from the
JobTable, you can do that using the following query:SELECT job_code FROM Job
As you can see, the result of this query contains many values, some of them duplicates. To remove duplicate values from the result, you have to use the
DISTINCTkeyword. - Modify the previous query to include the
DISTINCTkeyword:SELECT DISTINCT job_code FROM Job
This produces the desired result, each job code is listed only once.
- Now enter a query that specifies two columns using the
DISTINCTkeyword:SELECT DISTINCT job_code, job_grade FROM Job
DISTINCTapplies to all columns thay you specify in aSELECTstatement. In this case, the result shows duplicate job codes. That is becauseDISTINCTtreats thejob_codeandjob_gradetogether, so the combination of values in the result is distinct (no duplicates). You can see the expected result of this query in the image below:
