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
1001MrAlanTuring[email protected]
1002MrsAdaLovelace[email protected]
1003MissGraceHopper[email protected]
1004MrGeorgeBoole[email protected]
Person ID1001
TitleMr
ForenameAlan
SurnameTuring
Email address[email protected]
Person ID1002
TitleMrs
ForenameAda
SurnameLovelace
Email address[email protected]
Person ID1003
TitleMiss
ForenameGrace
SurnameHopper
Email address[email protected]
Person ID1004
TitleMr
ForenameGeorge
SurnameBoole
Email address[email protected]

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 [email protected]

1004 Mr George Boole [email protected]

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 [email protected]

1002 Mrs Ada Lovelace [email protected]

1004 Mr George Boole [email protected]

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 [email protected]

1003 Miss Grace Hopper [email protected]