FAQ: Queries - Distinct

This community-built FAQ covers the “Distinct” exercise from the lesson “Queries”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development
Data Science

Learn SQL

FAQs on the exercise Distinct

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

You should explain which “Nails” of the three is showed after doing this query “SELECT DISTINCT tools
FROM inventory;”. I think it’s the first.

3 posts were split to a new topic: After selecting distinct values how do you sort the result, largest to smallest, alphabetically, etc?

How can I order the results of the DISTINCT command whether ASC or DESC?

In the “Distinct” exercise, sometimes we see some NULL values in the output. How could that be explained? Isn’t the DISTINCT argument supposed to display only the already existing values that are unique?

Great question! Quote from the SQLite documentation:

SQLite was modified to work the same as Oracle, PostgreSQL, and DB2. This involved making NULLs indistinct for the purposes of the SELECT DISTINCT statement and for the UNION operator in a SELECT. NULLs are still distinct in a UNIQUE column. This seems somewhat arbitrary, but the desire to be compatible with other engines outweighed that objection.

This means that SELECT DISTINCT treats NULL like any other value - it is allowed, but duplicates will be removed.

To learn more about how the SQLite handles NULL values please refer to -> NULL Handling in SQLite Versus Other Database Engines.

Using ORDER BY clause :slight_smile:

The whole idea of DISTINCT is that it does not matter. These are duplicated values.