SQL SELECT DISTINCT Statement

The SQL SELECT DISTINCT syntax

The general syntax is:

  1. SELECT DISTINCT columnname
  2. FROM tablename

Can be used with COUNT and other aggregates

  1. SELECT COUNT (DISTINCT columnname)
  2. FROM tablename

 


 

SQL SELECT Examples

Problem: List all supplier countries in alphabetical order.

  1. SELECT DISTINCT Country
  2. FROM Supplier
  3. ORDER BY COUNTRY

Results: 16 rows

Country
Australia
Brazil
Canada
Denmark

 


Problem: List the number of supplier countries

  1. SELECT COUNT (DISTINCT Country)
  2. FROM Supplier

Results:

Count
16

댓글 남기기