Chapter 9

Structured Query Language (SQL)

SQL searches and manages data in a database using commands such as SELECT, FROM, WHERE and ORDER BY.

What is SQL?

SQL (Structured Query Language) is a language used to search, sort and edit the data held in a database. The most common IGCSE task is to retrieve selected records from a table.

The SELECT statement

A query names the fields to show with SELECT, the table to read with FROM, and an optional condition with WHERE. Results can be sorted with ORDER BY. Use * to show every field.

Example

List the name and price of every item costing more than 50 ringgit, cheapest first:

SELECT Name, Price FROM Products WHERE Price > 50 ORDER BY Price ASC;

Conditions and functions

Conditions use operators such as =, >, < and >=, and can be joined with AND or OR. Aggregate functions summarise data: SUM adds a numeric field and COUNT counts records; AVG, MAX and MIN are also available.

Key idea

Order of a basic query: SELECT fields FROM table WHERE condition ORDER BY field.

Remember

  • Text values go in quotes: WHERE Class = 'F5A'.
  • ORDER BY … ASC sorts low to high; DESC sorts high to low.
  • End a statement with a semicolon.

Stuck on this topic? A verified JomKelas tutor can walk you through it.

Find a verified tutor