ThoughtSpot acquires Mode to define the next generation of collaborative BI >>Learn More

SQL OR

Starting here? This lesson is part of a full-length tutorial in using SQL for Data Analysis. Check out the beginning.

In this lesson we'll cover:

The SQL OR operator

OR is a logical operator in SQL that allows you to select rows that satisfy either of two conditions. It works the same way as AND, which selects the rows that satisfy both of two conditions. Try OR out by running this query against data from the Billboard Music Charts:

SELECT *
  FROM tutorial.billboard_top_100_year_end
 WHERE year_rank = 5 OR artist = 'Gotye'

You'll notice that each row will satisfy one of the two conditions. You can combine AND with OR using parenthesis. The following query will return rows that satisfy both of the following conditions:

SELECT *
  FROM tutorial.billboard_top_100_year_end
 WHERE year = 2013
   AND ("group_name" ILIKE '%macklemore%' OR "group_name" ILIKE '%timberlake%')

You will notice that the conditional statement year = 2013 will be fulfilled for every row returned. In this case, OR is treated like one separate conditional statement because it's in parentheses, so it must be satisfied in addition to the first statement of year = 2013. You can think of the rows selected as being either of the following:

  • Rows where year = 2013 is true and "group_name" ILIKE '%macklemore%' is true
  • Rows where year = 2013 is true and "group_name" ILIKE '%timberlake%' is true
  • Rows where year = 2013 is true and "group_name" ILIKE '%macklemore%' is true and "group_name" ILIKE '%timberlake%' is true

Sharpen your SQL skills

Practice Problem

Write a query that returns all rows for top-10 songs that featured either Katy Perry or Bon Jovi.

Try it out See the answer

Practice Problem

Write a query that returns all songs with titles that contain the word "California" in either the 1970s or 1990s.

Try it out See the answer

Practice Problem

Write a query that lists all top-100 recordings that feature Dr. Dre before 2001 or after 2009.

Try it out See the answer

Next Lesson

SQL NOT

Get more from your data

Your team can be up and running in 30 minutes or less.