- Understand the purpose of a SQL query and the role of
SELECTandFROM - Choose which fields should appear in query results, including when to use
SELECT * - Use a simple
WHEREclause to filter records using comparison operators - Combine two simple conditions using
ANDorOR
- I can write a complete
SELECT ... FROM ...;query for one table - I can decide whether to list specific field names or use
* - I can write simple
WHEREconditions for text, whole numbers, and decimal values - I can put quotation marks around text values and leave number values unquoted
- I can use
ANDwhen both conditions must be true andORwhen either condition can be true
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 prior knowledge — it's fine if you're unsure.
1. In a database table, what is a field?
2. Which table in the films database stores the title, year, certificate, runtime, rating, and genre of each film?
3. The value 8.4 is stored in the rating field. Which data type is suitable?
Key vocabulary
=, <>, >, <, >=, or <=.SQL: first queries with SELECT and WHERE
What a SELECT query does
A database is useful because we can ask precise questions about the data it stores. A SQL query is one of those questions written in a formal language that the database can understand. In this lesson, every query retrieves data from one table in the films database. The result is a new table on screen: it might show every record, or only the records that match a condition.
The simplest useful query has two parts: SELECT and FROM. SELECT says which fields should be displayed. FROM says which table contains those fields. SQL keywords are often written in capital letters because it makes queries easier to read, although MySQL will still understand them in lower case.
This query displays the title and year fields for every record in the film table. The semicolon at the end tells DataGrip that the query is finished.
Choosing fields carefully
When an exam question asks you to display particular fields, list only those field names after SELECT, separated by commas. For example, SELECT title, rating, genre displays exactly three fields. The field names must match the database spelling, so runtime_mins is correct but runtime is not.
The shortcut SELECT * means "display every field". It is very useful when exploring a table for the first time, because it lets you see the field names and sample values. However, if a question asks for only title and rating, using * would display extra fields and would not be the best answer.
Filtering records with WHERE
A WHERE clause filters records. It comes after FROM and contains a condition that must be true for a record to appear. A condition usually compares a field to a value. For text values, put the value in quotation marks. For number values, do not use quotation marks.
The query above displays only films whose certificate field equals "12A". Because certificate is a text field, the value is quoted. The next query uses a number field, so the value is not quoted:
Comparison operators
The operator in a condition decides how the field is compared to the value. The most common operators are = for equal to, <> for not equal to, > for greater than, < for less than, >= for greater than or equal to, and <= for less than or equal to. Choose the operator that matches the wording of the question. "More than 8.0" means > 8.0; "at least 8.0" means >= 8.0.
Simple AND and OR
You can filter using two simple conditions. Use AND when both conditions must be true. For example, a film must be both a drama and rated at least 8.0:
Use OR when either condition can be true. For example, the next query displays films that are either comedy films or animation films:
In this first SQL lesson, keep conditions simple: one table, one or two conditions, and clear field names. Later lessons will build your confidence with more detailed filtering.
Worked examples
Question: Display the title, year, and genre for every film.
title, year, and genre.film table, so the query uses FROM film.Question: Display the title and certificate of all films with certificate 15.
title and certificate, so list those fields after SELECT.film table.certificate is a text field, so the value is written as "15".Question: Display the title, year, and rating of films made after 2015.
title, year, and rating.year > 2015.year is a number field, so 2015 is not placed in quotation marks.Question: Display the title and rating of drama films rated 8.0 or above.
title and rating.AND. Quote the text value "Drama", but do not quote the decimal value 8.0.Write a SQL query for this request:
Display the title, year, and genre of all films that are either horror films or western films.
Answer the following:
- Which fields should go after
SELECT? - Which table should go after
FROM? - Should the two conditions be combined with
ANDorOR? - Write the complete SQL query.
title,year, andgenrefilm- Use
OR, because a film can be horror or western. It does not need to be both. - SELECT title, year, genre FROM film WHERE genre = "Horror" OR genre = "Western";
SELECT film FROM title; reverses the structure. Field names go after SELECT; the table name goes after FROM.WHERE genre = "Sci-Fi". Number values do not: WHERE rating > 8.0.genre = "Comedy" AND genre = "Animation" will not find films, because one record cannot have both genre values at the same time. Use OR when either value is acceptable.* displays every field. If the question asks for title and rating, list only those two fields.In SQL questions, read the wording in this order: what fields should be displayed? Then which table contains those fields? Then which records should be included? This maps directly to SELECT, FROM, then WHERE.
If the value is text, quote it. If the value is a number, do not quote it. A small syntax error can lose the mark even when your idea is right, so check the final semicolon, commas between field names, and the spelling of every field.
Questions 1–5 are auto-checked. Questions 6–9 are self-marked — write your answer, then reveal the model answer to check your work.
1. Which SQL keyword states which fields should appear in the results? TYPE 1
2. What does SELECT * mean? TYPE 1
3. Which condition correctly finds films with genre Sci-Fi? TYPE 1
4. Which operator means "not equal to" in these SQL queries? TYPE 1
5. A query needs films where genre is Action and rating is more than 8.0. Which word combines the conditions? TYPE 1
6. Write a SQL query to display the title, year, and rating for every film. TYPE 2
7. Write a SQL query to display the title and runtime_mins of films with a runtime greater than 150 minutes. TYPE 2
8. Practical: In DataGrip, run a query that displays every field for every record in the director table. Paste your SQL and record how many director records are returned. TYPE 3
director table contains 15 records.
9. Practical: In DataGrip, write and run a query to display title, genre, and rating for films that are "Action" and have a rating of at least 8.0. Paste your SQL and record how many records are returned. TYPE 3
Suggested timing: 60 minutes. Warm up + vocabulary 10 min; live query modelling 20 min; worked examples 10 min; now you try 5 min; task set 15 min.
Practical setup: Pupils need Laragon running and the films database available in DataGrip. Use a query console connected to the films database.
Key misconception to address: Text values need quotation marks but field names do not. Keep returning to the pattern WHERE field = "text value" and compare it with WHERE year > 2015.
Live demo suggestion: Start with SELECT * FROM film;, then replace * with two named fields. Add one WHERE condition, then change only the operator/value so pupils see the query structure staying stable.
Extension question: Ask pupils to write two different queries that use OR: one using the genre field and one using the certificate field.
SQA command words covered: "write", "identify", "describe". Emphasise that a SQL "write" answer needs the full query, not just the condition.