Skip to main content

Filtering Numbers

Updated Aug 23, 2019 ·

Sample Table

We'll use the films table as example.

idtitlerelease_yearcountrydurationlanguagecertificationgrossbudget
1Inception2010USA148EnglishPG-13829895144160000000
2Parasite2019South Korea132KoreanR25759015211400000
3The Godfather1972USA175EnglishR2461209866000000
4Amélie2001France122FrenchR17420000010000000
5Coco2017USA105SpanishPG807082196175000000

WHERE Clause

To filter data, we use the WHERE clause. This clause helps us focus on data that is relevant to our specific questions.

  • Focus on specific data
  • Specify conditions
  • Filter for relevance

Comparison Operators

To filter numbers, we can use comparison operators like greater than. For example, to see films released after 1975, we use the greater than operator.

SELECT title
FROM films
WHERE release_year > 1975;

Output:

title
Inception
Parasite
Amélie
Coco

To find films release on a specific year, let's say 2017:

SELECT title
FROM films
WHERE release_year = 2017;

Output:

title
Coco

Not Equal To

To exclude films from the year 2017, we combine less than and greater than operators. This creates a "not equal to" condition in SQL.

SELECT title
FROM films
WHERE release_year <> 2017;

Output:

title
Inception
Parasite
The Godfather
Amélie

Below are a summary of comparison operators that we can use:

  • Greater than: after
  • Less than: before
  • Equal to
  • Greater than or equal to
  • Less than or equal to
  • Not equal to

WHERE with Strings

The WHERE clause also works with strings using the equals operator. We must use single quotation marks around the strings. For example, to filter titles where the country is Japan, we use single quotes around 'France'.

SELECT title
FROM films
WHERE country = 'France';

Output:

title
Amélie

Order of Execution

A final note on using WHERE: this clause comes after FROM in a query.

  • Written order: SELECT, FROM, WHERE, LIMIT
  • Execution order: FROM, WHERE, SELECT, LIMIT

As an example, this would be the correct way to write:

SELECT title
FROM films
WHERE country = 'USA'
LIMIT 2;

But it will be executed in this order:

FROM films 
WHERE country = 'USA'
SELECT title
LIMIT 2;

Output:

title
Inception
The Godfather
Coco

A few more examples

We'lll use the reviews table:

idfilm_idnum_usernum_criticimdb_scorenum_votesfacebook_likes
11100002508.82050015000
2285003008.6600012000
3375001509.2160009000
4490002008.6650080000
5595002758.61500010000
61110002609.02100015500
7287003108.7620012500
8376001609.31650095000

Select the film_id and imdb_score from the reviews table and filter on scores higher than 9.0.

SELECT film_id, imdb_score
FROM reviews
WHERE imdb_score > 9.0;

Output:

film_idimdb_score
39.2
39.3

Select the film_id and facebook_likes of the first 5 records with less than 15000 likes from the reviews table.

SELECT film_id, facebook_likes
FROM reviews
WHERE facebook_likes < 15000
LIMIT 5;

Output:

film_idfacebook_likes
115000
212000
39000
48000
510000

Count how many records have a num_votes of at least 15,000; use the alias films_over_15K_votes.

SELECT COUNT(*) AS films_over_15K_votes
FROM reviews
WHERE num_votes >= 15000;

Output:

films_over_15K_votes
4