Using the SQL SELECT clause with the DISTINCT Keyword

Introducing the SELECT DISTINCT SQL Statement

Alright, welcome back to our journey with SQL! If you’re just tuning in, we:

In this article, we delve slightly deeper into SQL queries, with the consideration that SQL databases and tables can have a lot of duplicate data, and you might not always want that duplication! This is where today’s subject comes in: using the SELECT clause with the DISTINCT keyword.

Sometimes when you’re managing a database or table, you only want unique (distinct) values when executing SQL queries. Thus, you can get around a large number of duplicate values using the DISTINCT keyword.

The Basic SELECT DISTINCT Syntax

Here’s the general format of what a SELECT DISTINCT query might look like:

SELECT DISTINCT column_1,column_2  FROM table_name;

Next, we’ll take a look a very simplistic example of why you might want to only pull unique values from a database.

Why Use the DISTINCT Keyword?

So we’ve generally been working from a popular public SQL “sample” or “sandbox” database that deals with DVD rentals. In one of the tables, “film”, there are a number of columns containing a wide range of information. For example, we can see below querying the release_year column of the film table, a few films were released in 2006. If we’re looking for a unique list, this is not a good start!

2017-01-30-001-SELECT-DISTINCT-Displaying-Duplicate-Table-Values

Using the SELECT DISTINCT Query in pgAdmin

Working off our example above, we want to see if 2006 is the only release year. To accomplish this, let’s try the below query:

SELECT DISTINCT release_year FROM film;

Below, the query in action through pgAdmin, after hitting F5 to execute and refresh. We can see 2006 is the only unique release year in this table. Zoinks!

2017-01-30-002-SELECT-DISTINCT-Displaying-Unique-Table-Values

Let’s try another example. Perhaps we’re interested in gathering pricing information for some revenue forecasting and analysis. Below is a query we would use to get the distinct rental prices from the film table:

SELECT DISTINCT rental_rate FROM film;

2017-01-30-003-SELECT-DISTINCT-Displaying-Unique-Rental-Rate-Table-Values

We see that there are only 3 price points in this table. What fun in simplicity!

Wrap-Up

Thanks for joining! Next post, we’ll be looking at the SELECT WHERE statement. Check the SQL learning & education page for a running list of articles. Cheers!

 

1 reply

Trackbacks & Pingbacks

  1. […] The journey continues in learning SQL. In case you missed it, the past couple of posts were learning to use SELECT DISTINCT and Restoring an SQL database with table schema […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *