SQL (Cambridge (CIE) IGCSE Computer Science)
Revision Note
Written by: James Woodhouse
Reviewed by: Lucy Kirkham
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 database table | SELECT * FROM users; SELECT name, age |
FROM | Specifies the tables to retrieve data from | SELECT name, age FROM users; |
WHERE | Filters the data based on a specified condition | SELECT * FROM users |
AND | Combines multiple conditions in a WHERE clause | SELECT * FROM users |
OR | Retrieves data when at least one of the conditions is true | SELECT * FROM users |
WILDCARDS | '*' and '%' symbols are used for searching and matching data | SELECT * FROM users; SELECT * FROM users WHERE name LIKE 'J%'; |
ORDER BY | How data is organised (sorted) when it is retrieved | SELECT Forename, Lastname FROM Students (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 sum of a field | SELECT SUM(Salary) FROM tbl_people; |
COUNT | Counts the number of files which match the set criteria | SELECT COUNT(*) FROM tbl_people WHERE Salary > 50000; |
Examples
Select all the fields from the Customers table
Command:
SELECT * FROM Customers; |
---|
Output:
ID | Name | Age | City | Country |
---|---|---|---|---|
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 |
---|
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 |
---|
Output:
Name | Country |
---|---|
John Doe | USA |
Jane Doe | UK |
Select all fields of customers who are from 'London' or 'Paris'
Command:
SELECT * |
---|
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 |
Kangaroo | Yes | 1 |
Lion | Yes | 1-6 |
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]
Last updated:
You've read 0 of your 5 free revision notes this week
Sign up now. It’s free!
Did this page help you?