DISTINCT
When working with databases, especially large ones, it's often necessary to filter out duplicate entries to get a clear picture of the unique data points. This is where DISTINCT
comes into play. It helps us identify and work with unique values in a dataset, making our analysis cleaner and more precise.
In SQL, the DISTINCT
keyword is used to return only unique values from a specified column, filtering out all duplicate entries. This can be incredibly useful when you want to understand the variety or range of data within a particular column.
Imagine you have an inventory database that tracks the tools available in a workshop. The table might look something like this:
SELECT tools
FROM inventory;
This query could produce a result like:
tools |
Hammer |
Nails |
Nails |
Nails |
As you can see, "Nails" appears multiple times. If we want to know just the unique tools available, we can use DISTINCT
:
SELECT DISTINCT tools
FROM inventory;
Now, the result would be:
tools |
Hammer |
Nails |
By using DISTINCT
, we quickly filter out the duplicates and see that the unique tools in our inventory are "Hammer" and "Nails".
Filtering the results of a query is an essential skill in SQL. It allows you to get a clearer view of your data without having to scan through every single row, which can be especially cumbersome in large datasets.
Activity
Let's put this into practice with a hands-on activity!
Identify Unique Genres
Open your SQL editor and type the following query:
SELECT DISTINCT genre
FROM movies;
This query will return a list of unique genres from the movies
table. Analyzing these unique genres can help you understand the variety of movies available.
genre |
action |
comedy |
horror |
romance |
drama |
NULL |
Identify Unique Years
Now, let's modify the query to find unique years instead of genres. Change your code to:
SELECT DISTINCT year
FROM movies;
This query will give you a list of unique years in which movies were released, providing a historical perspective on your movie database.
year |
2009 |
2015 |
2012 |
2008 |
1999 |
1997 |
2006 |