SQL (CIE IGCSE Computer Science)

Revision Note

Test Yourself
Amy Fellows

Expertise

Computer Science

SQL

  • Records in a database can be searched and data can be manipulated using Structured Query Language (SQL)
  • SQL statements can be written to query the data in the database and extract useful information
  • SQL statements follow this structure:
    • SELECT the fields you want to display 
    • FROM the table/tables containing the data you wish to search
    • WHERE the search criteria 

A Database Table Containing Movie Details


MovieID


Name


Genre


Certificate


Rating

M23

Moana

Family

U

8.1

M8

Shaun of the Dead

Comedy

18

8.7

M56

Die Hard

Action

18

8.4

M34

Big

Family

PG

8.5

Example

SELECT Name, Rating

FROM Movie

WHERE Rating>8.4;

The results of this query would be:


Name


Rating

Shaun of the Dead

8.7

Big

8.5

  • The two fields -  Name and Rating have been extracted from the Movie table and then the records have been filtered by Rating
  • This example uses the > operator to search for records where the rating is greater than 8.4
  • There are several other comparison operators which can be used to create the filter criteria in the WHERE line of a SQL query

SQL Comparison Operators


Operator


Description

>

Greater than

>=

Greater than or equal to

<

Less than

<=

Less than or equal to

=

Equal to

<>

Not equal to

Example

SELECT Name,Rating

FROM Movie

WHERE Genre=”Family” AND Certificate=”U”;

The results of this query would be:


Name


Rating

Moana

8.1

  • The two fields Name and Rating have been extracted from the Movie table and the records have been filtered by both Genre and Certificate
  • This query uses the AND logical operator to include multiple criteria in the WHERE line of the SQL query
  • Another logical operator which can be used in the WHERE statement is OR
    • For example, WHERE Genre=”Comedy” OR Genre=”Family”

Worked example

A database table, Dogs2023, is used to keep a record of all dogs registered at a veterinary practice


DogID


Name


Breed


Age


Gender

DG12

Smokey

Poodle

12

M

DG34

Harvey

Springer Spaniel

10

M

DG48

Maisie

Labradoodle

3

F

DG49

Maggie

Labradoodle

3

F

DG88

Winston

Bulldog

7

M

DG95

Shona

Golden Retriever

6

F

Complete the structured query language (SQL) to return the name and breed of all Female dogs.

SELECT Name,Breed

________ Dogs2023

WHERE _______;

[2]

FROM  [1]

Gender=”F” [1]

Write an SQL query to find out the name and breed of all dogs aged 10 years old or older

[4]

SELECT Name, Breed [1]

FROM Dogs2023 [1]

WHERE Age>=10; [2]

ORDER BY

  • You can enter a fourth line to the statement using the ORDER BY command, followed by ASC or DESC
    • If you enter ASC the results of the query will be sorted in ascending order
    • If you enter DESC the results of the query are sorted in descending order

Example

SELECT Name,Genre, Certificate, Rating

FROM Movie

ORDER BY Name ASC

  • The results of this query would be:


Name


Genre


Certificate


Rating

Big

Family

PG

8.5

Die Hard

Action

18

8.4

Moana

Family

U

8.1

Shaun of the Dead

Comedy

18

8.7

  • The query has returned four fields and all records because there were no WHERE criteria. The records are sorted by Name alphabetically
    • If numbers are sorted in ascending order they go from the lowest number at the top of the table to the highest number at the bottom
    • Descending order is the highest number to the lowest

SUM and COUNT commands

  • The SUM command can be used to add numerical data
  • The COUNT command can be used to count items of data


ProductID


ProductName


Price


QuantityInStock

1

Sausages

1.99

3

2

Chips

2.99

2

3

Beans

2.50

5

4

Bananas

2.10

12

5

Avocado

1.00

3

Example

SELECT SUM(QuantityInStock)

FROM ProductTable;

  • This query will add up all of the numbers in the QuantityInStock field
    • The result of this query would be 25 

Example

SELECT COUNT(*)

FROM ProductTable

WHERE Price>2;

  • This query will count all the records with a price greater than 2
    • The result of this query would be 3
    • This is because there are three products with a price greater than £2 (Chips, Beans, Bananas)

You've read 0 of your 0 free revision notes

Get unlimited access

to absolutely everything:

  • Downloadable PDFs
  • Unlimited Revision Notes
  • Topic Questions
  • Past Papers
  • Model Answers
  • Videos (Maths and Science)

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

the (exam) results speak for themselves:

Did this page help you?

Amy Fellows

Author: Amy Fellows

Amy has been a passionate Computing teacher for over 9 years, teaching Computer Science across the East Midlands, at all levels. Amy has also been a GCSE examiner for seven years.