🔒 Pro · Component 1 · 1.3.2 Databases
1.3.2b SQL
OCR H446 · A Level Computer Science · ~14 min read
Notes
Video
Slides
Worksheet
Quiz

What is SQL?

SQL (Structured Query Language) is the standard language for interacting with relational databases. It allows users and applications to create database structures, insert and update data, delete data, and retrieve data using queries. SQL is a declarative language — you specify WHAT data you want, not HOW to get it.

SELECT — Retrieving Data

The SELECT statement retrieves data from one or more tables.

SELECT column1, column2
FROM tableName
WHERE condition
ORDER BY column1 ASC;
  • SELECT * — retrieve all columns
  • FROM — specifies the table
  • WHERE — filters rows that meet a condition
  • ORDER BY — sorts results (ASC = ascending, DESC = descending)

Example — retrieve all students with grade A:

SELECT StudentName, Grade
FROM Student
WHERE Grade = 'A'
ORDER BY StudentName ASC;

Comparison Operators in WHERE

OperatorMeaning
=Equal to
<> or !=Not equal to
>, <Greater than, less than
>=, <=Greater/less than or equal
LIKE 'A%'Pattern matching (% = any characters, _ = one character)
BETWEEN x AND yWithin a range (inclusive)
IN (x, y, z)Matches any value in a list
AND, OR, NOTLogical operators to combine conditions

JOIN — Querying Multiple Tables

A JOIN combines rows from two or more tables based on a related column (usually a primary key / foreign key pair).

SELECT Student.StudentName, Course.CourseName
FROM Student
INNER JOIN Enrolment ON Student.StudentID = Enrolment.StudentID
INNER JOIN Course ON Enrolment.CourseCode = Course.CourseCode
WHERE Course.CourseName = 'Computer Science';
  • INNER JOIN — returns only rows where there is a match in BOTH tables (most commonly used).
  • LEFT JOIN — returns ALL rows from the left table, plus matching rows from the right (unmatched right rows show NULL).
  • RIGHT JOIN — returns ALL rows from the right table, plus matching rows from the left.
Exam tip: For OCR H446, you mostly need INNER JOIN. Know the syntax precisely: INNER JOIN table2 ON table1.key = table2.key.

INSERT — Adding Data

INSERT INTO Student (StudentID, StudentName, DOB, Grade)
VALUES ('S003', 'Fatima Ali', '2006-05-10', 'B');

Always list the column names in the same order as the values.

UPDATE — Modifying Data

UPDATE Student
SET Grade = 'A'
WHERE StudentID = 'S003';

Always include a WHERE clause with UPDATE — without it, every row in the table is updated!

DELETE — Removing Data

DELETE FROM Student
WHERE StudentID = 'S003';

Again, always include WHERE — without it, all rows are deleted. Referential integrity may prevent deleting a row that is referenced by a foreign key in another table.

CREATE TABLE — Defining Structure

CREATE TABLE Student (
  StudentID VARCHAR(5) PRIMARY KEY,
  StudentName VARCHAR(50) NOT NULL,
  DOB DATE,
  Grade CHAR(1)
);

Common data types:

TypeUse
INTEGER / INTWhole numbers
VARCHAR(n)Variable-length text up to n characters
CHAR(n)Fixed-length text exactly n characters
FLOAT / REALDecimal numbers
DATEDate value (YYYY-MM-DD)
BOOLEANTrue/false

Aggregate Functions

SQL includes built-in functions to perform calculations on groups of rows:

FunctionPurpose
COUNT(*)Counts the number of rows
SUM(col)Adds up all values in a column
AVG(col)Calculates the average value
MAX(col)Returns the highest value
MIN(col)Returns the lowest value
SELECT COUNT(*) AS TotalStudents, AVG(Score) AS AverageScore
FROM Student
WHERE Grade = 'A';

Use GROUP BY to group results and HAVING to filter groups (like WHERE but for groups):

SELECT Grade, COUNT(*) AS NumberOfStudents
FROM Student
GROUP BY Grade
HAVING COUNT(*) > 5;
Exam tip: You will be asked to write SQL in exams. Common patterns: SELECT with WHERE + AND/OR; INNER JOIN across two or three tables; INSERT/UPDATE/DELETE with correct syntax. Memorise the exact clause order: SELECT → FROM → JOIN → WHERE → GROUP BY → HAVING → ORDER BY.
⚠ Common Mistakes
  • Forgetting WHERE in UPDATE or DELETE — this updates or deletes ALL rows. Always specify which rows to affect with WHERE.
  • Getting JOIN syntax wrong — you must specify ON table1.key = table2.key. The columns joined must be the FK/PK pair. Don't forget the table prefix (e.g. Student.StudentID, not just StudentID) when columns exist in both tables.
  • Using = instead of LIKE for pattern matching — WHERE Name = 'A%' will look for a literal name "A%". Use WHERE Name LIKE 'A%' for pattern matching.
✓ Notes completed!
Video coming soon
Click to advance · Arrow keys also work
Click slide or press arrow keys to navigate

Worksheet — 1.3.2b SQL

8 questions · 20 marks · instantly marked

Q1Write an SQL statement to retrieve the Name and Salary of all employees from a table called Employee where the Salary is greater than 30000, sorted by Salary in descending order.[3 marks]
✓ Mark scheme
SELECT Name, Salary [1]; FROM Employee [1]; WHERE Salary > 30000 ORDER BY Salary DESC [1]. Full answer: SELECT Name, Salary FROM Employee WHERE Salary > 30000 ORDER BY Salary DESC;
Q2A database has tables: Student(StudentID, Name, Year) and Result(ResultID, StudentID, Subject, Mark). Write an SQL query to retrieve the Name and Mark of all students in Year 12 who scored more than 70, using an INNER JOIN.[4 marks]
✓ Mark scheme
SELECT Student.Name, Result.Mark [1]; FROM Student INNER JOIN Result ON Student.StudentID = Result.StudentID [1]; WHERE Student.Year = 12 [1]; AND Result.Mark > 70 [1]. Must use table prefixes on ambiguous column names and correct ON clause linking FK to PK.
Q3Explain what the LIKE operator does in an SQL WHERE clause. Give one example of its use.[2 marks]
✓ Mark scheme
LIKE performs pattern matching in a WHERE clause — it allows searching for values that match a pattern using wildcards: % (matches any number of characters) and _ (matches exactly one character) [1]; example: WHERE Name LIKE 'J%' returns all rows where Name starts with 'J'; WHERE Email LIKE '%@gmail.com' returns all rows where Email ends with @gmail.com [1].
Q4Write an SQL INSERT statement to add a new student: StudentID = 'S100', Name = 'James Wong', Year = 13 into the Student table.[2 marks]
✓ Mark scheme
INSERT INTO Student (StudentID, Name, Year) [1]; VALUES ('S100', 'James Wong', 13) [1]. Column list and values must match in order. String values must be in quotes; numeric values (Year = 13) without quotes.
Q5Write an SQL UPDATE statement to change the Mark to 85 for the result with ResultID = 42 in the Result table. Explain why including a WHERE clause is essential.[3 marks]
✓ Mark scheme
UPDATE Result SET Mark = 85 WHERE ResultID = 42 [2 — 1 for UPDATE/SET syntax, 1 for WHERE clause]; Without WHERE, ALL rows in the Result table would have their Mark updated to 85 — so every result in the entire table would be changed, destroying all the other marks [1].
Q6Write an SQL statement to count the number of students in each Year group from the Student table and only show Year groups with more than 20 students.[3 marks]
✓ Mark scheme
SELECT Year, COUNT(*) AS StudentCount [1]; FROM Student GROUP BY Year [1]; HAVING COUNT(*) > 20 [1]. Note: HAVING is used (not WHERE) because the filter is on an aggregate function result (COUNT). WHERE filters individual rows BEFORE grouping; HAVING filters groups AFTER grouping.
Q7Explain the difference between INNER JOIN and LEFT JOIN. When would you choose LEFT JOIN over INNER JOIN?[2 marks]
✓ Mark scheme
INNER JOIN returns only rows where there is a matching row in BOTH tables — unmatched rows from either table are excluded [1]; LEFT JOIN returns ALL rows from the left (first) table, plus matching rows from the right table — where there is no match in the right table, the right-side columns show NULL. Use LEFT JOIN when you want to include records from the left table even if they have no corresponding records in the right table — e.g. list all students including those who haven't enrolled on any course yet [1].
Q8Write a CREATE TABLE statement for a table called Course with fields: CourseCode (VARCHAR 6, primary key), CourseName (VARCHAR 50, not null), and Credits (INTEGER).[3 marks]
✓ Mark scheme
CREATE TABLE Course ( [1]; CourseCode VARCHAR(6) PRIMARY KEY, CourseName VARCHAR(50) NOT NULL, Credits INTEGER [1]; ); [1 for correct closing bracket and overall structure]. Fields in any order are acceptable if syntax is correct; PRIMARY KEY and NOT NULL constraints must be present.
Topic Quiz
1 of 15
You scored
out of 15
🎯

Mini Test — 1.3.2b SQL

  • 10 questions · 10 marks · 10 minutes
  • 5 MCQ + 5 short answer
Card 1 of 15
Click to reveal
🎉
Complete!
TermDefinition
← 1.3.2a Relational Databases 1.3.2 Databases Next: 1.3.2c Normalisation →
🔒
Pro Content
Subscribe to access all 69 OCR H446 A Level lessons.
£7.99/month
or £59/year
Subscribe now →