0% found this document useful (0 votes)
4 views6 pages

5 Z3 XC M5 SB FJVG6 QM

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

Head to www.savemyexams.

com for more awesome resources

Cambridge (CIE) IGCSE Your notes


Computer Science
SQL
Contents
SQL

Page 1 of 6
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources

SQL
Your notes
SQL
What is SQL?
SQL (Structured Query Language) is a programming language used to interact with a DBMS.
The use of SQL allows a user to:
Select data
Order data
Sum data
Count data

Selecting data commands


Command Description Example

SELECT Retrieves data from a SELECT * FROM users;


database table (retrieves all data from the 'users' table)
SELECT name, age
FROM users
(retrieves names and ages from the 'users' table)

FROM Specifies the tables to SELECT name, age FROM users;


retrieve data from (retrieves names and ages from the 'users' table)

WHERE Filters the data based on a SELECT * FROM users


specified condition WHERE age > 30;
(Retrieves users older than 30)

AND Combines multiple SELECT * FROM users


conditions in a WHERE WHERE age > 18 AND city = 'New York';
clause (retrieves users older than 18 and from New York)

Page 2 of 6
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources

OR Retrieves data when at SELECT * FROM users


least one of the WHERE age < 18 OR city = 'New York';
Your notes
conditions is true (retrieves users younger than 18 or from New York)

WILDCARDS '*' and '%' symbols are SELECT * FROM users;


used for searching and (retrieves all columns for the 'users' table)
matching data
'*' used to select all SELECT * FROM users WHERE name LIKE 'J%';
columns in a table (retrieves users whose names start with 'J')
'%' used as a wildcard
character in the LIKE
operator

ORDER BY How data is organised SELECT Forename, Lastname FROM Students


(sorted) when it is WHERE StudentID < 10
retrieved ORDER BY Lastname, Forename ASC
(retrieves only the forename and lastname of all students
from the students table who have a studentID of less than 10
and displays in ascending order by lastname and forename)

SUM Adds up and outputs the SELECT SUM(Salary) FROM tbl_people;


sum of a field

COUNT Counts the number of SELECT COUNT(*)


files which match the set
criteria FROM tbl_people
WHERE Salary > 50000;

Examples
Select all the fields from the Customers table
Command:

SELECT * FROM Customers;

Output:

Page 3 of 6
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources

ID Name Age City Country


Your notes
1 John Doe 30 New York USA

2 Jane Doe 25 London UK

3 Peter Lee 40 Paris France

Select the ID, name & age of customers who are older than 25
Command:

SELECT ID, name, age


FROM Customers
WHERE Age > 25;

Output:

ID Name Age

1 John Doe 30

3 Peter Lee 40

Select the name and country of customers who are from a country that begins with 'U'
Command:

SELECT Name, Country


FROM Customers
WHERE Country LIKE 'U%';

Output:

Name Country

Page 4 of 6
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources

John Doe USA


Your notes
Jane Doe UK

Select all fields of customers who are from 'London' or 'Paris'


Command:

SELECT *
FROM Customers
WHERE City = 'London' OR City = 'Paris';

Output:

ID Name Age City Country

2 Jane Doe 25 London UK

3 Peter Lee 40 Paris France

Worked Example
Below is a table of animals called tbl_animals

Animal Breeding Number of Young

Red Fox Yes 4-6

Rabbit Yes 4-12

African Elephant Yes 1

Blue Whale No 1

Orangutan Yes 1

Polar Bear Yes 1-3

Dolphin Yes 1

Page 5 of 6
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers
Head to www.savemyexams.com for more awesome resources

Kangaroo Yes 1

Lion Yes 1-6 Your notes

Penguin Yes 1

Complete this SQL statement to display all of the animal breeds that are currently breeding and
there was only one young born this year [3]
SELECT .................................................
FROM .................................................
WHERE .................................................
Answer
SELECT Animal [1]
FROM tbl_animals [1]
WHERE Number of Young == 1 [1]

Page 6 of 6
© 2015-2024 Save My Exams, Ltd. · Revision Notes, Topic Questions, Past Papers

You might also like