AND

AND, OR and IN

SQLite in your browser
Lesson concept What you'll do
1 / 4

Pick rows that pass more than one test.

AND needs both tests, OR either one, and IN matches any value in a list.

The database
11 rows
book_idtitleauthorgenreyearpages
1MockingjaySuzanne CollinsScience Fiction2010390
2The ShiningStephen KingHorror1977447
3The Catcher in the RyeJ.D. SalingerFiction1951234
4Harry Potter and the Philosopher's StoneJ.K. RowlingFantasy1997223
5Little WomenLouisa May AlcottFiction1868449
6War and PeaceLeo TolstoyFiction18691225
7The Girl with the Dragon TattooStieg LarssonThriller2005465
8Sense and SensibilityJane AustenRomance1811400
9The Fellowship of the RingJ.R.R. TolkienFantasy1954423
10A Study in ScarletArthur Conan DoyleMystery1887108
11The Da Vinci CodeDan BrownThriller2003454
Click a name to type it
Your query
 
Hint: click the green edit button on the board, or type it yourself
Query output Ready to run

Run your query to see the result table.

Press the green Run button or Ctrl+Enter.

Study notes

How AND, OR, IN works

  • When do I use AND?

    When every test must be true: genre = 'Science Fiction' AND year < 1960 keeps only old Science Fiction.

  • When do I use OR?

    When any one test is enough: genre = 'Fantasy' OR genre = 'Horror' keeps books of either genre. A book can't be Fantasy AND Horror here, so AND would find nothing.

  • What does IN do?

    WHERE genre IN ('Fantasy', 'Horror')

    It's a short way to write several ORs on one column. Put the values in brackets, separated by commas.

  • What if I mix AND and OR?

    AND is worked out before OR, like * before +. Use brackets to be sure: (genre = 'Fantasy' OR genre = 'Horror') AND pages < 300.

  • How is this like Excel?

    The same as AND and OR inside IF: =IF(AND(D2="Science Fiction", E2<1960), ...), or COUNTIFS with two conditions.

Know someone fighting with a spreadsheet? Send them this page.