Programming techniques - OCRThe use of SQL to search for data

Proficient programming requires knowledge of many techniques. These techniques allow for powerful, complex programs.

Part ofComputer ScienceComputational thinking, algorithms and programming

The use of SQL to search for data

Databases use their own type of programming language. This language is known as structured query language, or SQL.

Example

Consider this simple personnel table with four records:

Person IDTitleForenameSurnameEmail address
1001MrAlanTuringaturing@bitesize.com
1002MrsAdaLovelacealovelace@gcsecompsci.com
1003MissGraceHopperghopper@bitesizemail.co.uk
1004MrGeorgeBoolegboole@bbcbitesize.com
Person ID1001
TitleMr
ForenameAlan
SurnameTuring
Email addressaturing@bitesize.com
Person ID1002
TitleMrs
ForenameAda
SurnameLovelace
Email addressalovelace@gcsecompsci.com
Person ID1003
TitleMiss
ForenameGrace
SurnameHopper
Email addressghopper@bitesizemail.co.uk
Person ID1004
TitleMr
ForenameGeorge
SurnameBoole
Email addressgboole@bbcbitesize.com

Retrieving data

Data can be retrieved using the SELECT, FROM and WHERE

SELECT * FROM "personnel" WHERE "Title" = "Mr"

Note - * stands for wildcard, which means all records. This would retrieve the following data:

1001 Mr Alan Turing aturing@bitesize.com

1004 Mr George Boole gboole@bbcbitesize.com

The LIKE command can be used to find matches for an incomplete word, for example:

SELECT * FROM "personnel" WHERE "email address" LIKE "%com"

This would retrieve:

1001 Mr Alan Turing aturing@bitesize.com

1002 Mrs Ada Lovelace alovelace@gcsecompsci.com

1004 Mr George Boole gboole@bbcbitesize.com

Note - %com is also a wildcard which will return any value that contains “com”.

operators AND and OR can also be used to retrieve data.

SELECT * FROM "personnel" WHERE "Surname" = "Turing" OR "Hopper"

This would retrieve:

1001 Mr Alan Turing aturing@bitesize.com

1003 Miss Grace Hopper ghopper@bitesizemail.co.uk