SQL Distinct

The SQL DISTINCT statement is used to query a table in which some columns may contain duplicate values. In this situation using the DISTINCT keyword will only return distinct values.

SQL DISTINCT Syntax

SELECT DISTINCT “column_name(s)”
FROM “table_name”

By adding DISTINCT to the SQL SELECT statement query will only return DISTINCT values.

SQL DISTINCT Example

To demonstrate the above example assume that we have the following table called Games

ID Title Release Date Genres Platform
1001 GTA IV April 29, 2008 Action-adventure PS3,XBOX360
1002 Gran Turismo 5 November 24, 2010 Racing PS3
1003 Assassin’s Creed November 14, 2007 Third person PS3,XBOX360
1004 Battlefield: Bad Company June 26, 2008 First person shooter PS3,XBOX360
1005 LittleBigPlanet October 01, 2008 Puzzle platformer PS3

To SELECT all the DISTINCT values from the Platform column we would use the following query:

SELECT DISTINCT Platform
FROM Games

Once executed the resultset would look like this:

Platform
PS3,XBOX360
PS3

Give it a go and once you feel comfortable using the SQL DISTINCT statement move on to our next tutorial SQL WHERE.