Skip to topic
    ← Back to course topics

    Structured Query Language (SQL) — AQA A-Level Computer Science

    Structured Query Language (SQL) is a declarative language designed for defining, manipulating, and querying data within relational database management systems. For AQA A-Level Computer Science, students must master both Data Definition Language (DDL) operations like table creation and Data Manipulation Language (DML) commands including complex multi-table queries.

    Structured Query Language (SQL) explained

    This statement asks you to write SQL that works across more than one table and performs all four core data operations: retrieve (SELECT), update (UPDATE), insert (INSERT) and delete (DELETE).

    Read the full explanation

    For retrieval across tables, you join them on a shared key, for example SELECT Customer.Name, Orders.OrderDate FROM Customer INNER JOIN Orders ON Customer.CustomerID = Orders.CustomerID; The join condition matches a foreign key to its primary key, which is how a relational database links related records. For updates and deletes you must identify the correct rows with a WHERE clause, because omitting it affects every row in the table. An UPDATE that changes a price for one product is written UPDATE Product SET Price = 9.99 WHERE ProductID = 103; and a DELETE is written DELETE FROM Orders WHERE OrderID = 5012;.

    Your focus

    1. Be able to use SQL to retrieve, update, insert and delete data from multiple tables of a relational database.

    Structured Query Language (SQL) exam tips

    Topic Overview

    Structured Query Language (SQL) is the global standard declarative language used to manage, query, and maintain relational databases. Unlike procedural programming languages, SQL specifies what data must be retrieved or modified rather than detailing the step-by-step algorithms required to process it.

    Within the AQA A-Level specification, SQL is divided into Data Definition Language (DDL) commands for schema design and Data Manipulation Language (DML) commands for operational data processing. Mastering SQL provides the foundational skillset required for backend software development, database administration, and Paper 2 database architecture assessment.

    Key Concepts
    • →Declarative paradigm: SQL code defines the required outputs and logical constraints without specifying execution control flows.
    • →Data Definition Language (DDL): Statements such as CREATE TABLE, ALTER TABLE, and DROP TABLE that establish database schema, data types, and integrity constraints.
    • →Data Manipulation Language (DML): Statements including SELECT, INSERT INTO, UPDATE, and DELETE used to query and modify records.
    • →Relational join operations: Combining data across multiple tables using primary key and foreign key equivalences in WHERE or INNER JOIN clauses.
    • →Aggregation and grouping: Utilizing aggregate functions (COUNT, SUM, AVG, MIN, MAX) in conjunction with GROUP BY and HAVING clauses.
    Marking Points
    • Writes a SELECT statement that joins two tables on matching primary and foreign key columns.
    • Uses a WHERE clause in every UPDATE and DELETE so only the intended rows change.
    • Names the table and the columns correctly in an INSERT, with values matching the column order.
    • Recognises that a child-table row must reference a key that already exists in the parent table.
    • Identifies which rows a given statement will affect by reasoning through the sample data.
    Examiner Tips
    • 💡Read each SQL statement and state the exact rows it will affect before selecting an answer.
    • 💡When choosing between UPDATE and INSERT, check whether the record already exists.
    • 💡Check the join condition uses the correct key pair from the table definitions given in the question.
    • 💡Always write SQL keywords in UPPERCASE (e.g., SELECT, FROM, WHERE, ORDER BY) to improve readability and ensure clarity for the examiner.
    • 💡Qualify column names with their respective table name (e.g., Student.StudentID) whenever a column exists in more than one table in your query.
    • 💡Double-check commas in CREATE TABLE statements; missing commas between column definitions or putting a comma before the closing bracket is a frequent syntax slip.
    Common Mistakes
    • Omits the WHERE clause from an UPDATE or DELETE, which changes or removes every row; add a condition that identifies the intended rows.
    • Joins tables on columns that are not related keys, producing meaningless combinations; join on the foreign key matching the primary key.
    • Inserts a foreign key value that has no matching parent record, breaking referential integrity; insert the parent row first or use an existing key.
    • Believing SQL is an imperative or procedural programming language; SQL is declarative because the query optimiser determines the retrieval algorithm, not the programmer.
    • Using the WHERE clause instead of the HAVING clause when filtering aggregated data groups created by GROUP BY.
    • Assuming strings can be unquoted; string and date literals must always be enclosed in single or standard quotation marks, unlike numeric values.
    Revision Plan
    1. 1Day 1-3: Master basic DML syntax by writing single-table SELECT, INSERT, UPDATE, and DELETE queries.
    2. 2Day 4-6: Practice multi-table queries involving primary-to-foreign key relationships and aggregate functions using GROUP BY and HAVING.
    3. 3Day 7-9: Learn DDL commands by writing CREATE TABLE and ALTER TABLE scripts with comprehensive primary and foreign key constraints.
    4. 4Day 10-12: Complete past AQA Paper 2 database exam questions under timed conditions and review against official mark schemes.
    Exam Question Types
    • 📋Writing multi-table SELECT queries with filtering, sorting, and aggregate functions based on an entity-relationship scenario.
    • 📋Writing DDL scripts to create relational tables complete with specified data types, primary keys, and foreign keys.
    • 📋Updating or deleting existing records matching specific conditions using UPDATE ... SET or DELETE FROM statements.
    • 📋Tracing or explaining the output table returned by a provided SQL statement applied to sample data.
    Command Word Expectations (AQA)
    Write

    Formulate syntactically accurate SQL code that satisfies all criteria and constraints specified in the scenario without procedural code.

    Explain

    Set out the logical operation of an SQL clause, constraint, or command, making clear relationships between relational theory and query outcome.

    How Students Lose Marks (Examiner Pitfalls)
    Pitfall: Omitting the join condition when querying multiple tables in a SELECT statement, resulting in an unintentional Cartesian product (cross join).
    ❌ Weak Answer (Loses Marks):SELECT StudentName, SubjectName FROM Student, Subject WHERE Grade = 'A';
    Example improved answer:SELECT Student.StudentName, Subject.SubjectName FROM Student, Subject, Enrolment WHERE Student.StudentID = Enrolment.StudentID AND Subject.SubjectID = Enrolment.SubjectID AND Enrolment.Grade = 'A';
    Examiner Tip: Always count the number of tables listed in your FROM clause. If there are n tables, you need at least n - 1 explicit join conditions in your WHERE or ON clause to properly link primary and foreign keys.
    Pitfall: Confusing the syntax of UPDATE and ALTER TABLE commands when modifying data versus modifying table structure.
    ❌ Weak Answer (Loses Marks):ALTER TABLE Customer SET Discount = 0.1 WHERE CustomerID = 45;
    Example improved answer:UPDATE Customer SET Discount = 0.1 WHERE CustomerID = 45;
    Examiner Tip: Remember that ALTER TABLE is a Data Definition Language (DDL) command used to change columns or datatypes, whereas UPDATE is a Data Manipulation Language (DML) command used to alter existing record values.
    Step-by-Step Worked Solutions

    Question: A relational database contains two tables: Doctor (DoctorID, DoctorName, Specialism) Appointment (AppointmentID, DoctorID, PatientName, AppDate, Fee) Write an SQL query to display the DoctorName, PatientName, and Fee for all appointments scheduled on '2025-10-15' where the Fee is greater than 50.00. The results should be ordered by Fee in descending order.

    1. 1.Step 1: Identify required output fields: Doctor.DoctorName, Appointment.PatientName, Appointment.Fee for the SELECT clause.
    2. 2.Step 2: Identify tables involved: Doctor and Appointment for the FROM clause.
    3. 3.Step 3: Establish the join condition: Doctor.DoctorID = Appointment.DoctorID.
    4. 4.Step 4: Add filtering criteria: AppDate = '2025-10-15' AND Fee > 50.00 in the WHERE clause.
    5. 5.Step 5: Apply sorting constraint: ORDER BY Fee DESC.
    Final Answer: SELECT Doctor.DoctorName, Appointment.PatientName, Appointment.Fee FROM Doctor, Appointment WHERE Doctor.DoctorID = Appointment.DoctorID AND Appointment.AppDate = '2025-10-15' AND Appointment.Fee > 50.00 ORDER BY Appointment.Fee DESC;

    Question: Write an SQL statement to create a table named 'Module' with the following attributes: - ModuleCode: fixed-length string of 6 characters, Primary Key - Title: variable-length string up to 50 characters, cannot be empty - Credits: integer value - DepartmentID: integer value, referencing DepartmentID in the Department table as a Foreign Key.

    1. 1.Step 1: Begin with the DDL statement CREATE TABLE Module ( ... ).
    2. 2.Step 2: Define ModuleCode as CHAR(6) and specify PRIMARY KEY constraint.
    3. 3.Step 3: Define Title as VARCHAR(50) and add the NOT NULL constraint.
    4. 4.Step 4: Define Credits as INT or INTEGER.
    5. 5.Step 5: Define DepartmentID as INT or INTEGER and specify the foreign key constraint referencing Department(DepartmentID).
    Final Answer: CREATE TABLE Module ( ModuleCode CHAR(6) NOT NULL, Title VARCHAR(50) NOT NULL, Credits INT, DepartmentID INT, PRIMARY KEY (ModuleCode), FOREIGN KEY (DepartmentID) REFERENCES Department(DepartmentID) );
    Active Recall Memory Test
    What SQL keyword is used to filter records after an aggregation has been carried out via GROUP BY?
    Key Fact: HAVING
    Which DML statement is used to add new records into an existing table?
    Key Fact: INSERT INTO
    What is the primary difference between CHAR and VARCHAR data types in an SQL table definition?
    Key Fact: CHAR stores fixed-length character strings padding unused space, whereas VARCHAR stores variable-length strings allocating only needed storage.
    Why is SQL classified as a declarative language rather than a procedural one?
    Key Fact: Because the programmer writes code stating what data to retrieve rather than specifying the precise procedural control flow and algorithms to fetch it.
    Frequently Asked Questions
    Do I lose marks if I write SQL keywords in lowercase in the AQA exam?
    Technically SQL is case-insensitive, and mark schemes will accept lowercase keywords if the syntax is valid. However, writing keywords in UPPERCASE is standard industry convention and makes your syntax immediately clear to the examiner, reducing the risk of misinterpretation.
    What is the difference between WHERE and HAVING in SQL?
    The WHERE clause filters individual rows before any grouping or aggregation takes place. The HAVING clause filters groups of rows after the GROUP BY clause and aggregate functions (like COUNT, SUM, or AVG) have been calculated.
    Can I use INNER JOIN instead of comma-separated tables in the FROM clause for AQA?
    Yes, both approaches are fully accepted on AQA mark schemes. You may use standard ANSI JOIN syntax (FROM TableA INNER JOIN TableB ON TableA.ID = TableB.ID) or relational filtering (FROM TableA, TableB WHERE TableA.ID = TableB.ID).
    How do I specify a compound primary key in a CREATE TABLE statement?
    To specify a compound (composite) primary key, declare it as a table-level constraint at the end of the column list rather than inline. For example: PRIMARY KEY (StudentID, CourseID).
    What data types does AQA expect in SQL DDL questions?
    AQA typically expects standard SQL types: INT or INTEGER for whole numbers, REAL or FLOAT for decimal numbers, CHAR(n) for fixed strings, VARCHAR(n) for variable strings, DATE for calendar dates, and BOOLEAN for true/false flags.