SQL SELECT Operations (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

SQL select

What is SQL?

  • SQL (Structured Query Language) is a language used to create, update, delete and retrieve data from a database

  • Students are expected to use SELECT statements to query and organise data

  • The following examples all use a single database table named Customers and its contents can be seen below:

Table with column headers: ID, Name, Age, City, Country. Rows of data: 1, John Doe, 30, New York, USA; 2, Jane Doe, 25, London, UK; 3, Peter Lee, 40, Paris, France.

SELECT statement

  • The SELECT command is used to retrieve data from one or more database tables

Example

SELECT * FROM Customers;

  • This retrieves all records and all fields from the table Customers

Examiner Tips and Tricks

'*' is used to select all columns in a table, also known as a wildcard

FROM clause

  • The FROM clause specifies which table the data will be selected from

Example

SELECT ID, Name, Age FROM Customers;

  • This retrieves only the ID, Name and Age fields from the Customers table

A table with three columns: ID, Name, and Age. Two rows display data for John Doe (ID 1, Age 30) and Peter Lee (ID 3, Age 40).

WHERE clause

  • The WHERE clause filters results based on a condition

  • Conditions can use comparison operators such as =, <, >, and logical operators such as AND and OR

Examples

SELECT ID, Name, Age FROM Customers

WHERE Age > 25;

  • Retrieves all customers older than 25

A table with three columns: ID, Name, and Age. Two rows display data for John Doe (ID 1, Age 30) and Peter Lee (ID 3, Age 40).

SELECT * FROM Customers

WHERE City = 'London' AND Age < 30;

  • Retrieves customers who live in London and are under 30

SELECT * FROM Customers

WHERE City = 'London' OR City = 'Paris';

  • Retrieves customers who live in either London or Paris

Table with headers ID, Name, Age, City, Country. Rows: (1) Jane Doe, 25, London, UK. (2) Peter Lee, 40, Paris, France.

ORDER BY clause

  • The ORDER BY clause is used to sort the output of a query in ascending (ASC) or descending (DESC) order

  • At this level, you can order by up to two fields

Examples

SELECT Name, City FROM Customers

ORDER BY City;

  • Orders customers by City in ascending order (A–Z)

SELECT Name, City FROM Customers

ORDER BY City DESC, Name ASC;

  • Orders customers by City in descending order, and then by Name in ascending order

Summary

SQL keyword

Purpose

Example

SELECT

Choose which fields to retrieve

SELECT Name, Age FROM Customers;

FROM

Choose which table to retrieve from

FROM Customers

WHERE

Filter data using conditions

WHERE Age > 25

AND / OR

Combine multiple conditions

WHERE City='London' AND Age<30

ORDER BY

Sort the results (max two fields)

ORDER BY City, Name ASC

Worked Example

Scenario: The Global Dino Record Database

The DinoDiscoveries group has consolidated all their fossil findings into a single, comprehensive database table called DinoRecord.

The table structure includes the following attributes:

Attribute Name

Type

Key

Validation

dinoName

Text

length

Number

Range: >= 0.1 and <= 50

diet

Text

Restricted choice: Carnivore, Herbivorous, Omnivore

countryFound

Text

period

Text

Restricted choice: Cretaceous, Jurassic, Triassic

recordID

Number

PK

Write an SQL select statement that will display the dinosaur's name (dinoName), its diet, and the period it lived in, for all dinosaurs that were Herbivorous (plant-eaters) AND lived during the Jurassic period

The resulting list must be sorted alphabetically by dinoName.

[4]

Answer

SELECT dinoName, diet, period FROM DinoRecord

WHERE diet = 'Herbivorous' AND period = 'Jurassic' ORDER BY dinoName ASC;

  • SELECT dinoName, diet, period [1 mark]

  • FROM DinoRecord [1 mark]

  • WHERE diet = 'Herbivorous' AND period = 'Jurassic' [1 mark]

  • ORDER BY dinoName ASC; [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.