DEV Community

Cover image for Getting Started with SQL DISTINCT
DbVisualizer
DbVisualizer

Posted on

Getting Started with SQL DISTINCT

SQL DISTINCT is an essential feature for eliminating duplicate rows in query results. This article offers a quick overview and examples to help you incorporate it into your queries efficiently.

Unique city queries

When querying a table with a large dataset, you may want to find unique entries for a specific column. For example, to get a list of distinct cities from an orders table, you can use.

SELECT DISTINCT city 
FROM orders;
Enter fullscreen mode Exit fullscreen mode

Using DISTINCT across columns

To find unique combinations of data from multiple columns, such as customer names, cities, and products, use the following query.

SELECT DISTINCT customername, city, product 
FROM orders;
Enter fullscreen mode Exit fullscreen mode

Aggregate function with DISTINCT

DISTINCT can be combined with aggregate functions like COUNT to analyze unique data. For instance, counting unique customers in each city is done with this query.

SELECT city, COUNT(DISTINCT customername) AS UniqueCustomersCount
FROM orders
GROUP BY city;
Enter fullscreen mode Exit fullscreen mode

Filter with WHERE clause

For targeted queries, DISTINCT can be paired with conditions. To get a list of unique customers in New York, use this.

SELECT DISTINCT customername, address, phone
FROM orders
WHERE city = 'New York';
Enter fullscreen mode Exit fullscreen mode

FAQ

What does DISTINCT do?

It ensures unique rows in query results by removing duplicates.

How to use DISTINCT in a query?

Place it after SELECT and specify the columns where duplicates should be removed.

What’s the difference between UNIQUE and DISTINCT?

DISTINCT filters output; UNIQUE enforces constraints on table entries.

Can DISTINCT combine with other clauses?

Yes, it integrates well with WHERE, GROUP BY, and more.

Summary

SQL DISTINCT is a valuable tool for decluttering your database queries and extracting unique insights. By mastering its uses, you’ll improve the efficiency and clarity of your data analysis workflows.

For more examples and practical guidance, explore the full guide SQL DISTINCT: A Comprehensive Guide.

Understanding SQL essentials like DISTINCT ensures a solid foundation for tackling more advanced database challenges.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Image of Datadog

Keep your GPUs in check

This cheatsheet shows how to use Datadog’s NVIDIA DCGM and Triton integrations to track GPU health, resource usage, and model performance—helping you optimize AI workloads and avoid hardware bottlenecks.

Get the Cheatsheet

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay