SQL Manipulation (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

SQL manipulation

What is SQL manipulation?

  • SQL provides commands to add, change, and remove records from a database table

  • These are known as data manipulation commands

  • They are used when you need to maintain or correct data in a database

Adding/changing data

Command

Description

Example

INSERT

Adds new data to a database table

INSERT INTO users (name, age)
VALUES ('John Doe',25);
(inserts a new user with the name 'John Doe' and age 25)

UPDATE

Edit data in a database table

UPDATE users
SET name = 'Bob', age = 56
WHERE ID = 4;
(updates name and age details for user ID = 4)

Examples

Table: Employees

Table listing employee details: ID, Name, Salary, Department, City. Three entries: Fynn Roberts, Zarmeen Azra, George Rope with respective details.
  • Insert a new employee into the Employees table with the 'Name', 'Salary', 'Department' and 'City' fields

Command:

INSERT INTO Employees (Name, Salary, Department, City)

VALUES ('George Rope', 47250, 'Sales', 'Leeds');

Output:

Table listing employees with columns for ID, Name, Salary, Department, and City. Highlighted row shows George Rope, ID 4, salary 47250, in Sales at Leeds.

Table: Employees

Table listing employees with columns for ID, Name, Salary, Department, and City. Example: Fynn Roberts, HR, £45,000, based in London.
  • Update employee ID 3 to a salary of 47500 and city to London

Command:

UPDATE Employees

SET Salary = 47500, City = 'London'

WHERE ID=3;

Output

Table showing employee details with columns for ID, Name, Salary, Department, and City. Employees: Fynn Roberts, Zarmeen Azra, and Ella Stanley.

Deleting data

Command

Description

Example

DELETE

Removes data from a database table

DELETE FROM users
WHERE age < 18;
(deletes all users younger than 18 from the 'users' table)

DELETE FROM users
WHERE name="John";
(deletes a record where the name is John)

Example

Table: Employees

ID

Name

Salary

Department

City

1

Fynn Roberts

45000

HR

London

2

Zarmeen Azra

52000

Sales

Manchester

3

Ella Stanley

39500

Marketing

Birmingham

4

George Rope

47250

Sales

Leeds

  • Delete all records from the Employees table whose department is 'Marketing'

Command:

DELETE FROM Employees
WHERE Department = 'Marketing' ;

Output:

ID

Name

Salary

Department

City

1

Fynn Roberts

45000

HR

London

2

Zarmeen Azra

52000

Sales

Manchester

3

George Rope

47250

Sales

Leeds

Summary

SQL command

Purpose

Example

INSERT

Add a new record

INSERT INTO Customer (customerID, name, city) VALUES (3, 'Cara Smith', 'Edinburgh');

UPDATE

Modify existing data

UPDATE Customer SET city='Glasgow' WHERE customerID=3;

DELETE

Remove a record

DELETE FROM Customer WHERE customerID=3;

Worked Example

Scenario: The Restaurant Employee Database

A local restaurant uses a single database table, Employee, to store staff records.

The table structure and a small sample of data are shown below. Note that fullTime stores a Boolean value (where the absence of the '✓' in the sample data implies they are part-time, or equivalent to FALSE):

Attribute Name

Key

Type

empID

PK

Number

empName

Text

jobTitle

Text

startDate

Date

fullTime

Boolean

contactNum

Text

(i) Adding a Record

A new employee, Leo Sanchez, is hired as a server. He is part-time (False) and starts on 10/05/2026. He is assigned employee ID 555777 and contact number 07700123456

Write a single SQL statement that will add this new employee's details to the Employee table

[2]

(ii) Modifying a Record

Employee Rayyan Patel (ID 259631) has recently changed their contact number to 07778589500 and has been promoted to Manager

Write a single SQL statement that will update Rayyan Patel's record to reflect both the new contact number and the new jobTitle

[2]

(iii) Removing Records

The restaurant decides to remove all part-time servers from the database. Part-time employees are identified where the fullTime field is FALSE

Write an efficient SQL statement to delete all records where the employee is a server AND is part-time

[2]

Answer

(i)

INSERT INTO Employee (empID, empName, jobTitle, startDate, fullTime, contactNum) [1 mark]

VALUES (555777, 'Leo Sanchez', 'Server', '10/05/2026', FALSE, '07700123456'); [1 mark]

(ii)

UPDATE Employee SET contactNum = '07778589500', jobTitle = 'Manager' [1 mark]

WHERE empID = 259631; [1 mark]

(iii)

DELETE FROM Employee [1 mark]

WHERE jobTitle = 'Server' AND fullTime = FALSE; [1 mark]

Unlock more, it's free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Reviewer: James Woodhouse

Expertise: Computer Science & English Subject Lead

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.