Data Querying with SELECT FROM Statement
The
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 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
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
In the next sections, we will build on this foundation to create even more complex queries.