Introducing the SELECT DISTINCT SQL Statement
Alright, welcome back to our journey with SQL! If you’re just tuning in, we:
- Started SQL from the absolute beginning
- Got up and running with PostgreSQL and pgAdmin
- Created, restored and deleted/dropped SQL databases
- Restored databases with table schema only
- Used the SELECT clause as our “Hello World!” for SQL queries
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!
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!
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;
We see that there are only 3 price points in this table. What fun in simplicity!
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!