SELECT…FROM… statement
The select statement is used to select data from the database.
To select all the records from a table, the following command is used (where Table_Name would be replaced by the name of the table in the database):
SELECT * FROM Table_Name;If the table were named 'Pupil' then the command would read:
SELECT * FROM Pupil;All records in the pupil table would be returned. The asterisk * is used to return all data found in the table.
To aid understanding of the remaining examples, here is the data held in the pupil table.
| PupilID | First Name | Surname | Class | Pastoral Care Teacher | Merit Points |
| 0001 | Nisa | Singh | 1T1 | Mr Harley | 74 |
| 0002 | Davie | Boland | 1I2 | Mrs Aitken | 12 |
| 0003 | Amy | Jones | 1M1 | Miss Armstrong | 55 |
| 0004 | James | McNamee | 1T1 | Mr Harley | 88 |
| 0005 | Pawel | McTavish | 1I1 | Mrs Aitken | 19 |
| 0006 | Antonia | Gallagher | 1M2 | Miss Armstrong | 21 |
| 0007 | Amy | McNamee | 1T1 | Mr Harley | 63 |
| 0008 | Tabiso | Matazinadze | 1T1 | Mr Harley | 45 |
| 0009 | Beatrice | Panner | 1T2 | Mr Harley | 77 |
| 0010 | Tom | Glacney | 1M1 | Miss Armstrong | 91 |
| PupilID | 0001 |
|---|---|
| First Name | Nisa |
| Surname | Singh |
| Class | 1T1 |
| Pastoral Care Teacher | Mr Harley |
| Merit Points | 74 |
| PupilID | 0002 |
|---|---|
| First Name | Davie |
| Surname | Boland |
| Class | 1I2 |
| Pastoral Care Teacher | Mrs Aitken |
| Merit Points | 12 |
| PupilID | 0003 |
|---|---|
| First Name | Amy |
| Surname | Jones |
| Class | 1M1 |
| Pastoral Care Teacher | Miss Armstrong |
| Merit Points | 55 |
| PupilID | 0004 |
|---|---|
| First Name | James |
| Surname | McNamee |
| Class | 1T1 |
| Pastoral Care Teacher | Mr Harley |
| Merit Points | 88 |
| PupilID | 0005 |
|---|---|
| First Name | Pawel |
| Surname | McTavish |
| Class | 1I1 |
| Pastoral Care Teacher | Mrs Aitken |
| Merit Points | 19 |
| PupilID | 0006 |
|---|---|
| First Name | Antonia |
| Surname | Gallagher |
| Class | 1M2 |
| Pastoral Care Teacher | Miss Armstrong |
| Merit Points | 21 |
| PupilID | 0007 |
|---|---|
| First Name | Amy |
| Surname | McNamee |
| Class | 1T1 |
| Pastoral Care Teacher | Mr Harley |
| Merit Points | 63 |
| PupilID | 0008 |
|---|---|
| First Name | Tabiso |
| Surname | Matazinadze |
| Class | 1T1 |
| Pastoral Care Teacher | Mr Harley |
| Merit Points | 45 |
| PupilID | 0009 |
|---|---|
| First Name | Beatrice |
| Surname | Panner |
| Class | 1T2 |
| Pastoral Care Teacher | Mr Harley |
| Merit Points | 77 |
| PupilID | 0010 |
|---|---|
| First Name | Tom |
| Surname | Glacney |
| Class | 1M1 |
| Pastoral Care Teacher | Miss Armstrong |
| Merit Points | 91 |
The following SQL command could be used to select specific data from the table:
SELECT PupilID, Surname, Class FROM Pupil;This would return:
| PupilID | Surname | Class |
| 0001 | Singh | 1T1 |
| 0002 | Boland | 1I2 |
| 0003 | Jones | 1M1 |
| 0004 | McNamee | 1T1 |
| 0005 | McTavish | 1I1 |
| 0006 | Gallagher | 1M2 |
| 0007 | McNamee | 1T1 |
| 0008 | Matazinadze | 1T1 |
| 0009 | Panner | 1T2 |
| 0010 | Glacney | 1M1 |
| PupilID | 0001 |
|---|---|
| Surname | Singh |
| Class | 1T1 |
| PupilID | 0002 |
|---|---|
| Surname | Boland |
| Class | 1I2 |
| PupilID | 0003 |
|---|---|
| Surname | Jones |
| Class | 1M1 |
| PupilID | 0004 |
|---|---|
| Surname | McNamee |
| Class | 1T1 |
| PupilID | 0005 |
|---|---|
| Surname | McTavish |
| Class | 1I1 |
| PupilID | 0006 |
|---|---|
| Surname | Gallagher |
| Class | 1M2 |
| PupilID | 0007 |
|---|---|
| Surname | McNamee |
| Class | 1T1 |
| PupilID | 0008 |
|---|---|
| Surname | Matazinadze |
| Class | 1T1 |
| PupilID | 0009 |
|---|---|
| Surname | Panner |
| Class | 1T2 |
| PupilID | 0010 |
|---|---|
| Surname | Glacney |
| Class | 1M1 |