SQL: Select distinct

This entry is part 8 of 20 in the series SQL Tutorial

For most real world tables, there will be repeated values similar to the table below.

IDartistealbumyear
1Rod StewartMaggie May1973
2New OrderSubstance1987
3NirvanaSmell Like Teen Spirit1992
4Rod StewartOut of Order1988

Using select count, we will have 4 records in the table.

However, we know that we only have 3 artistes in this table.  How do we correctly display the 3 artistes?  We use select distinct query statement.

SELECT DISTINCT column_name,column_name
FROM table_name;

select distinct artiste from artist

To count the unique artistes:

select count(distinct artiste) from artist

SQL: Select count

This entry is part 6 of 20 in the series SQL Tutorial

IDartistealbumyear
1Rod StewartMaggie May1973
2New OrderSubstance1987
3NirvanaSmell Like Teen Spirit1992

In a database table, we can use select count to retrieve the number of records in a table.

Syntax:  select count(*) from table_name

select count(*) from artiste

We can also use the syntax where we count a particular column such as id or album.

select count(id) from artiste
select count(album) from artiste