Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases

SQL: Find all movies with any missing data

You need to find if any values in any of the columns are null. The columns are id, title, year released, genre;

SELECT * FROM movies WHERE id IS NULL OR title IS NULL OR year_released IS NULL or genre IS NULL;

This gave me the right answer.

Is there a simpler query ?

4 Answers

Alexandru Palita
Alexandru Palita
14,261 Points

Or this...

SELECT * FROM movies WHERE id IN (title,year_released,genre) IS NULL ;

helpful to remember, thanks!

Rachael Benedict
Rachael Benedict
7,242 Points

Hi, thank you for this answer. Please could you explain how this works as I would like to understand it better. Thank you :)

Alexandru Palita
Alexandru Palita
14,261 Points

The SQL IN Operator The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

https://www.w3schools.com/sql/sql_in.asp

Keep learning ! You will figure it out... Succes!

Rachael Benedict
Rachael Benedict
7,242 Points

Thank you for replying. I can understand all the IN examples in the link you posted below but I still don’t understand how the one in your own example is working :)

In the one below which is the last example from the link, it is saying select everything from customers table where the country column (from customer table) matches country column (from suppliers table) therefore displaying the rows where the countries match only

SELECT * FROM Customers WHERE Country IN (SELECT Country FROM Suppliers);

That makes sense to me but I can’t work out how your statement works and it looks like something I should know for the future. Thank you again and sorry for bothering!

Rachael

Alexandru Palita
Alexandru Palita
14,261 Points

You know what... You're right!

Now that i'm paying more attention , i understand that I read that so WRONG!

Sorry for any inconvenience.... my bad ... You've could just said: Hey dude ! Pay more attention :))... It's not what he's asking...

Thanks for noticing!

If you have like hundreds of columns not values... It's more advanced... Like you don't have to write hundreds of lines...

Check out this: https://stackoverflow.com/questions/63291/sql-select-columns-with-null-values-only https://stackoverflow.com/questions/12091272/find-all-those-columns-which-have-only-null-values-in-a-mysql-table

In the shortened query SELECT * FROM movies WHERE id IN (title, year_released, genre) IS NULL;, why do you use id between WHERE and IN?

Steven Parker
Steven Parker
231,248 Points

I think you got it. :+1:

SQL is known to get a bit wordy at times.

IS NULL is the way to indicate values that don't exist.

Here is my answer:

SELECT * FROM movies where year_released IS NULL;