When you have a database, one of the most important things you are going to do with it is to query it. Stored data is useless without being able to access it at a later date. When getting data from a database you must execute a query. A query is simply a search on the database which will provide some results.

In the early days of computing there were many different databases. Each had a different way to access the data. Each used a slightly different way to query the data which meant that it was very difficult to transfer skills from one database to another. Essentially you had to specialise in a single database and learn how to query that one rather than learning one way to query. This is clearly a problem. Programmers are unable to transfer their skills from one database to another and employers find it more difficult to get the exact skills they need.

This was a unacceptable situation. Databases form the corner stone of computers in business and so the demand for skilled database operatives was high. It was recognised that a single way of querying the database was required. A committee called the ANSI-SQL group produced a standard for querying a database. Their standard, SQL, provided a standard way to query a database.

SQL stands for structured query language and looks very similar to a programming language. A query is written in SQL and sent to the database query engine. The database then executes the query and returns the results.

The first query everyone learns is the SELECT * query. SELECT is the one of the most important commands. It allows you to retrieve data from a table.

 

Table name - PERSONAL

ID

Surname

Forename

Age

1

Hamflett

A

25

2

Rubble

Barney

42

3

Flintstone

Fred

65

4

Doo

Scooby

12

5

Rubble

Betty

40

The above table, PERSONAL, contains the names of some people. In order to get all of this information from the database you must execute a SELECT command.

SELECT *

FROM PERSONAL

This SQL command will return all of the rows in the above table. The results are known as a result set. The SELECT part of the query allows you to select which fields you return. You can either say return all of them, like done above, or you can produce a comma delimited list.

SELECT age

FROM PERSONAL

This command produces the result set.

Age

25

42

65

12

40

The FROM statement basically says which table the results should come from. In this case the table is called PERSONAL so the results come from the table PERSONAL.