- Build confidence writing
WHEREconditions for one-table SQL queries - Choose comparison operators that match wording such as "more than", "at least", and "not equal to"
- Handle text values and number values correctly in SQL conditions
- Debug common SQL syntax errors by reading the query and the error message carefully
- I can write complete
SELECT ... FROM ... WHERE ...;queries using thefilmtable - I can use
=,<>,>,<,>=, and<=accurately - I can decide when to use
ANDand when to useOR - I can spot missing commas, missing quotes, wrong table names, and wrong data types in broken SQL
Use films.sql in DataGrip. If the films database is not already available, download the file, open it in DataGrip, and run the whole script.
Answer before the lesson begins. These check your memory of DDD5 — it's fine if you're unsure.
1. Which clause filters records so only matching rows appear?
2. Which condition correctly finds films with certificate 15?
3. A query needs films that are Comedy or Animation. Which word combines the conditions?
Key vocabulary
rating >= 8.0.=, <>, >, <, >=, or <=."Drama".Filtering with conditions
Filtering is about precision
In DDD5, you learned that a SQL query can display selected fields from a table and use WHERE to include only certain records. This lesson slows that idea down and gives you more practice. Most National 5 SQL questions are not trying to trick you with long code; they are checking whether you can translate a short English request into a precise condition.
The basic structure stays steady. First choose the fields for SELECT. Then choose the table for FROM. Then write the filter after WHERE. In this lesson, all queries use one table only: film. That means you should not add other table names or try to connect tables together. The skill is choosing the correct condition.
This query asks for the title and rating fields, from the film table, but only for records where the rating is at least 8.0.
Choosing the right comparison operator
Small wording changes matter. More than means >. Less than means <. At least means >=, because the boundary value is included. No more than means <=. Not equal to means <>. If a question says "films rated 8.0 or above", the value 8.0 must be included, so the condition is rating >= 8.0, not rating > 8.0.
Text values and number values
A common SQL error is putting quotation marks in the wrong place. Field names are not quoted in these lessons: write genre, not "genre". Text values are quoted: write "Sci-Fi", "PG", or "15". Number values are not quoted: write year > 2015, runtime_mins <= 120, or rating < 7.5.
The certificate "15" looks like a number, but in the films database it is a text value because certificates can also be "PG", "U", "12A", and "18". Always think about the field's data type, not just the characters you can see.
Using AND and OR without overcomplicating it
Use AND when every condition must be true for the same record. For example, if a film must be both a Sci-Fi film and released after 2010, use AND. A film that is Sci-Fi but from 1979 fails the second condition. A film from 2019 that is Drama fails the first condition.
Use OR when either condition is acceptable. For example, if the question asks for films with certificate "U" or "PG", a record only has to match one of those certificate values to appear.
Reading error messages
When DataGrip shows a red error message, do not delete everything and start again. Read from the top of the query and check the structure. Are the selected fields separated by commas? Is the table called film, not films? Are text values inside quotation marks? Is there a comparison operator between the field and the value? Error messages often point near the problem, but not always exactly at it, so use them as clues.
Debugging SQL is a normal part of working with databases. The best habit is to fix one issue at a time and run the query again. If the error changes, you are making progress. If the query runs but the result is empty, the syntax may be correct but the logic might be wrong, such as using AND where OR was needed.
Worked examples
Question: Display the title and rating of films rated at least 8.5.
title and rating.film table.>=, not >.Question: Display the title, certificate, and genre of films with certificate 12A.
title, certificate, and genre.certificate is a text field, because values can include letters such as PG and 12A."12A".Question: Display the title, genre, and year of Sci-Fi films released after 2010.
"Sci-Fi" and the year must be greater than 2010."Sci-Fi" is quoted. The number value 2010 is not quoted.AND because both conditions must be true.Broken query:
title rating is missing a comma between field names.film, not films.Drama is a text value, so it needs quotation marks.A pupil writes this query to find Drama films released after 2000:
Answer the following:
- Find two syntax errors in the query.
- Find one data type mistake in the condition.
- Write the corrected query.
SELECT title yearis missing a comma, and the table should befilm, notfilms.Dramaalso needs quotation marks.yearis a number field, so2000should not be in quotation marks.- SELECT title, year FROM film WHERE genre = "Drama" AND year > 2000;
> 8.0, but "8.0 or above" is >= 8.0. Read the phrase before choosing the operator."Horror" and "PG" need quotes. Number values such as 2019 and 7.5 do not.SELECT title, year, rating. Without commas, MySQL cannot separate the field names properly.certificate = "U" AND certificate = "PG" asks one record to have two different certificates at once. Use OR when either value is acceptable.When asked to write a SQL query, underline the comparison phrase before writing the answer: more than, less than, at least, no more than, equal to, or not equal to. Then decide whether the value is text or a number. This two-second check prevents most filtering errors.
If you are fixing broken SQL, work in this order: commas between fields, correct table name, quotation marks around text values, no quotation marks around number values, then the correct AND or OR.
Questions 1-5 are auto-checked. Questions 6-7 are self-marked SQL writing and debugging questions. Questions 8-10 are practical DataGrip filtering tasks.
1. Which condition means "rating is at least 8.0"? TYPE 1
2. Which value is written correctly for the text field genre? TYPE 1
3. Which word is best for "films that are Horror and have a rating below 7.0"? TYPE 1
4. Which condition finds films whose certificate is not "15"? TYPE 1
5. What is wrong with SELECT title year FROM film;? TYPE 1
6. Write a SQL query to display the title, year, and certificate of films with certificate "15" and rating of at least 8.0. TYPE 2
7. Debug this query. It should display title and rating for Drama films released after 2000. TYPE 2
title, table name film, quotes around "Drama", and no quotes around the number 2000. This returns 5 records.8. Practical: In DataGrip, run a query that displays title and rating for films with a rating below 7.5. Paste your SQL and record how many films are returned. TYPE 3
9. Practical: In DataGrip, write and run a query to display title and certificate for films with certificate "U" or certificate "PG". Paste your SQL and record how many records are returned. TYPE 3
10. Practical debugging: Type this broken query into DataGrip, run it, read the error message, then fix it. It should find Horror films with rating no more than 7.0. Paste the corrected SQL and record the result count. TYPE 3
film, quotes around "Horror", and the operator <=. This returns 2 records: Us and Nope.Suggested timing: 60 minutes. Warm up + vocabulary 10 min; filtering notes and live demo 20 min; worked examples 10 min; now you try 5 min; DataGrip task set 15 min.
Practical setup: Pupils need Laragon running, DataGrip open, and the films database selected. Use a query console connected to the films database.
Key misconception to address: Certificates such as "15" look numeric but are text values. Keep comparing certificate = "15" with rating >= 8.0.
Live demo suggestion: Start with a working one-condition query. Deliberately remove a comma, change film to films, and remove quotes around a text value. Ask pupils to predict the error before running each version.
Extension question: Ask pupils to write two different queries for "family-friendly" certificates: one using OR for "U" and "PG", and one adding a rating condition with AND.
SQA command words covered: "write", "identify", "describe", "explain". Emphasise that debugging questions need both the corrected SQL and a short reason for each fix.