Query Design (SQA National 5 Computing Science): Revision Note

Exam code: X816 75

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Updated on

Query design

What is a query?

  • A query is a request for specific information from a database

  • Before any SQL is written, students must design the query

  • The design is a simple plan that identifies the required:

    • Fields, the data that will appear in the final output

    • Tables, the tables the database must read to obtain the fields

    • Search criteria, the rules that filter the data

    • Sort order, the way the results should be ordered

  • These elements must be selected using the ERD and data dictionary provided in the question

  • Good design reduces mistakes and helps ensure the final SQL solution meets the requirements

Designing queries with multiple tables and fields

  • Some queries require data from more than one table

  • In the exam, students must be able to:

    • Identify all required tables

    • Identify all output fields

  • If a query uses fields from different tables, the SQL written later will require a join using the primary key and foreign key

  • Students do not write the join during the design stage

  • They only select the correct tables and fields

Example

  • A query asks for hotel name, phone number, town, and train station details

  • A correct design would include:

    • Tables: Hotel, Resort

    • Fields: hotelName, phoneNumber, town, trainStation

  • This is sufficient for full marks in the design stage

Designing search criteria

  • Search criteria determine which records are included in the results

  • Students must use correct field names and appropriate operators

  • Common operators include:

    • =

    • <

    • >

    • <=

    • >=

    • AND

    • OR

Examples

  • town = "Ayr"

  • swimmingPool = true AND starRating >= 4

  • Both examples show valid criteria based on a data dictionary

Designing sort order

  • Sort order controls how the final results are organised

  • Students must be able to design:

    • A single-field sort

    • A two-field sort

  • Two-field sorting is required knowledge

  • The first field is the main sort, the second deals with ties

Example

  • The requirement states: “Show hotels with the highest star rating first, then sort towns alphabetically.”

  • The correct sort order in the design is:

    • starRating DESC

    • town ASC

Final query design

Component

Required design

Field(s)

hotelName, phoneNumber, town, starRating

Table(s)

Hotel, Resort

Search Criteria

town = "Ayr" AND starRating >= 4

Sort Order

town ASC, starRating DESC

Examiner Tips and Tricks

  • The table contains everything needed for full marks in the design phase before any SQL is written

Worked Example

A relational database is used by a professional Esports organisation to track player performance and team statistics. The database contains two linked tables: Player (containing individual playerID, playerScore, and specialty) and Team (containing teamID, teamLocation, and trophiesWon).

Query requirement:

The organisation wishes to display a list containing the player's ID, the team's location, and the player's score for all players whose specialty is "Sniper" AND who belong to a team that has won more than 10 trophies.

The list must be displayed with the highest player score first, and players with the same score must be listed in alphabetical order of team location.

Complete the table below to design the solution for this query.

[4]

Answer

Component

Expected answer

Marks

Field(s)

playerID, teamLocation, playerScore

[1 mark]

Table(s)

Player, Team

[1 mark]

Search Criteria

specialty = "Sniper" AND trophiesWon > 10

[1 mark]

Sort Order

playerScore DESC, teamLocation 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.