Filtering multiple criteria

Intermediate SQL with AI

Jasmin Ludolf

Senior Data Science Content Developer

Filtering multiple criteria

Filtering on multiple criteria like book genres and release dates

Intermediate SQL with AI

All requirements are true

Prompt: Show English films under 90 minutes released after 1990

SELECT title, release_year, duration
FROM films
WHERE release_year > 1990
AND language = 'English'
AND duration < 90;
|title                   |release_year|duration|
|------------------------|------------|--------|
|Showdown in Little Tokyo|1991        |79      |
|Stone Cold              |1991        |88      |
|Teenage Mutant Ninja ...|1991        |88      |
...
Intermediate SQL with AI

Either OR

Prompt: Show English or French language films released after 1990

SELECT title, release_year, language 
FROM films
WHERE (language = 'English' OR language = 'French') AND release_year > 1990;
|title                   |release_year|language|
|------------------------|------------|--------|
|Beastmaster 2: Throug...|1991        |English |
|Bill & Ted's Bogus Jo...|1991        |English |
|Boyz n the Hood         |1991        |English |
...
Intermediate SQL with AI

Parentheses

SELECT title, release_year, language 
FROM films
WHERE language = 'English' OR language = 'French' AND release_year > 1990;
|title              |release_year|language|
|-------------------|------------|--------|
|The Broadway Melody|1929        |English |
|Hell's Angels      |1930        |English |
|A Farewell to Arms |1932        |English |
|42nd Street        |1933        |English |
...
Intermediate SQL with AI

Data ranges

Prompt: Show films titles and release year released between 1990 and 1999

SELECT title, release_year
FROM films
WHERE release_year BETWEEN 1990 AND 1999;
|title                      |release_year|
|---------------------------|------------|
|Arachnophobia              |1990        |
|Back to the Future Part III|1990        |
...
  • Equivalent to release_year >= 1990 AND release_year <= 1999
Intermediate SQL with AI

Checking multiple values

Prompt: Show film titles and country for films Japan, Italy, or Spain

SELECT title, country
FROM films
WHERE country = 'Japan' OR country = 'Italy' OR country = 'Spain';
|title                         |country|
|------------------------------|-------|
|Seven Samurai                 |Japan  |
|A Fistful of Dollars          |Italy  |
|The Good, the Bad and the Ugly|Italy  |
...
Intermediate SQL with AI

Checking multiple values

Prompt: Show film titles and country for films Japan, Italy, or Spain

SELECT title, country
FROM films
WHERE country IN ('Japan', 'Italy', 'Spain');
|title                         |country|
|------------------------------|-------|
|Seven Samurai                 |Japan  |
|A Fistful of Dollars          |Italy  |
|The Good, the Bad and the Ugly|Italy  |
...
Intermediate SQL with AI

Excluding multiple values

Prompt: Show film titles and language except those in English and French

SELECT title, language
FROM films
WHERE language NOT IN ('English', 'French')';
|title        |language|
|-------------|--------|
|Metropolis   |German  |
|Pandora's Box|German  |
|Seven Samurai|Japanese|
...
Intermediate SQL with AI

Verify complex filters

$$

  • AND: require multiple conditions

  • OR: accept alternatives

  • BETWEEN: ranges

  • IN / NOT IN: multiple values

  • Parentheses to group conditions and control logic

AI on a computer

Intermediate SQL with AI

Let's practice!

Intermediate SQL with AI

Preparing Video For Download...