Data Querying with SELECT FROM Statement

The SELECT statement is the foundational basis of data querying. We use the SELECT FROM statement to provide instructions on what data to collect and from where to collect this data.

To see this in practice, suppose that we have the following table structure:

| employee_id | first_name | last_name |
|-------------|------------|-----------|
| 1001        | John       | Smith     |
| 1002        | Sarah      | Johnson   |
| 1003        | Michael    | Brown     |
| 1004        | Emily      | Davis     |
| 1005        | David      | Wilson    |
| 1006        | Jessica    | Miller    |
| 1007        | Robert     | Taylor    |
| 1008        | Lisa       | Anderson  |
| 1009        | James      | Thomas    |
| 1010        | Amanda     | Jackson   |

From this table we wish to fetch the first name of employees. we can simply write the following query.

SELECT first_name
FROM employees;

The results of this query are given below.

| first_name |
|------------|
| John       |
| Sarah      |
| Michael    |
| Emily      |
| David      |
| Jessica    |
| Robert     |
| Lisa       |
| James      |
| Amanda     |

Notice that the results table returns all the rows of the column name first_name in the table Employees. By specifying the name of the column, the SELECT and the table in the FROM statement will only return the data from that column and table.

SELECT Multiple Columns/Fields

As we have done in the example above, we can specify any number of column names we want to retrieve from our table of interest. Suppose we want the first_name , last_name, and employee_id from the table, we can write a query below to retrieve this information

SELECT first_name, last_name, employee_id
FROM employees;

The results of this query are given below.

| first_name | last_name | employee_id |
|------------|-----------|-------------|
| John       | Smith     | 1001        |
| Sarah      | Johnson   | 1002        |
| Michael    | Brown     | 1003        |
| Emily      | Davis     | 1004        |
| David      | Wilson    | 1005        |
| Jessica    | Miller    | 1006        |
| Robert     | Taylor    | 1007        |
| Lisa       | Anderson  | 1008        |
| James      | Thomas    | 1009        |
| Amanda     | Jackson   | 1010        |

SELECT *

Tables often have many columns, sometimes too many that it would not make sense to explicitly write all of them on a query. For a case such as this, we typically use the special operation * which indicates that we want all the columns associated with that table. This is also commonly used to see how many columns exist in a table. Let's see the use and output of this on the Employees table.

SELECT *
FROM employees;
| employee_id | first_name | last_name |
|-------------|------------|-----------|
| 1001        | John       | Smith     |
| 1002        | Sarah      | Johnson   |
| 1003        | Michael    | Brown     |
| 1004        | Emily      | Davis     |
| 1005        | David      | Wilson    |
| 1006        | Jessica    | Miller    |
| 1007        | Robert     | Taylor    |
| 1008        | Lisa       | Anderson  |
| 1009        | James      | Thomas    |
| 1010        | Amanda     | Jackson   |

Notice that we now have more columns returned. In fact, we have all the columns from the table returned to us. The * is a special character used in many programming languages to indicate that we want all the available options of a particular condition. In SQL, it is a convenient way to select all columns from a table instead of hardcoding each individual column name.

In the next sections, we will build on this foundation to create even more complex queries.