January 21st, 2012
SQL Select
The SELECT statement is commonly used to extract data from database tables(s). The SELECT syntax is as follows:
SELECT column1, column2, column3 FROM table;
SELECT Example Usage
The Persons Table
| ID | First_Name | Last_Name | Gender | Occupation |
|---|---|---|---|---|
| 123 | Fabian | Salvador | Male | Fireman |
| 345 | Abigail | Grant | Female | Fireman |
| 223 | Chloe | Strange | Female | Secretary |
If we want to extract the First_Name and Last_Name of everyone in the table, we use the following SELECT statement.
SELECT First_Name, Last_Name FROM Persons
With that, we will get the table but only with the First_Name and Last_Name columns of the table. The result would look like this:
| First_Name | Last_Name |
|---|---|
| Fabian | Salvador |
| Abigail | Grant |
| Chloe | Strange |
SELECT * FROM table
If you want to extract all columns, using the * is a common way to do this.
SELECT * FROM Persons
The result would be like this:
| ID | First_Name | Last_Name | Gender | Occupation |
|---|---|---|---|---|
| 123 | Fabian | Salvador | Male | Fireman |
| 345 | Abigail | Grant | Female | Fireman |
| 223 | Chloe | Strange | Female | Secretary |