Skip to content

Commit

Permalink
Merge pull request swcarpentry#314 from danmichaelo/ep5-sol
Browse files Browse the repository at this point in the history
Ep 5: Add solution to the exercise "NULL in a Set"
  • Loading branch information
henrykironde authored Jun 4, 2020
2 parents 147b416 + 590ef36 commit c6c4a73
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions _episodes/05-null.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,35 @@ detail in [the next section]({{ site.github.url }}/06-agg/).
> ## NULL in a Set
>
> What do you expect the query:
> What do you expect the following query to produce?
>
> ~~~
> SELECT * FROM Visited WHERE dated IN ('1927-02-08', NULL);
> ~~~
> {: .sql}
>
> to produce?
> What does it actually produce?
> > ## Solution
> >
> > You might expect the above query to return rows where dated is either '1927-02-08' or NULL.
> > Instead it only returns rows where dated is '1927-02-08', the same as you would get from this
> > simpler query:
> >
> > ~~~
> > SELECT * FROM Visited WHERE dated IN ('1927-02-08');
> > ~~~
> > {: .sql}
> >
> > The reason is that the `IN` operator works with a set of *values*, but NULL is by definition
> > not a value and is therefore simply ignored.
> >
> > If we wanted to actually include NULL, we would have to rewrite the query to use the IS NULL condition:
> >
> > ~~~
> > SELECT * FROM Visited WHERE dated = '1927-02-08' OR dated IS NULL;
> > ~~~
> > {: .sql}
> {: .solution}
{: .challenge}
> ## Pros and Cons of Sentinels
Expand Down

0 comments on commit c6c4a73

Please sign in to comment.