SQL - Select
In SQL, queries begin with the select clause. A typical query statement needs only two parts, we select what from where. The what will represent columns of your table you wish to select and the where represents the table name of your data table.
To avoid typing a list of every single column in your table, you can use the astric (*) to select all table columns from a table. However, query speeds are very important and always selecting every table column can dramatically decrease your SQL performance. As good practice, it is best to only select the table columns that you wish to use or display.
SQL Code:
SELECT * FROM employees;
SQL Table:
| ID | Lastname | Firstname | Title |
| 1 | Johnson | David | crew |
| 2 | Hively | Jessica | crew |
| 9 | Hicks | Freddy | crew |
| 10 | Harris | Joel | crew |
| 11 | Davis | Julie | manager |
Our result should print every table column and every bit of data stored in each row thus far.
SQL Code:
SELECT Lastname,Firstname FROM employees;
SQL Table:
| Lastname | Firstname |
| Johnson | David |
| Hively | Jessica |
| Hicks | Freddy |
| Harris | Joel |
| Davis | Julie |
SQL - Select Multiple Tables
Multiple columns from different tables can also be selected. The syntax is relatively the same, the difference being when you choose that table column you wish to select, you must now name the table of the desired table column.
The period is necessary to differentiate between the table name and the column name, other than that all the same query rules apply.
SQL Code:
SELECT employees.Fisrtname, employees.Lastname, crew.experience FROM
employees, crew;
SQL - Select Functions
We havn't mentioned anything about SQL Functions yet and we will eventually cover them in greater detail, for the moment we would like to introduce this concept of using a selection query with a function. For instance we can query our database program for the current_timestamp or retrieve the number of rows in our table using the COUNT() function.
SQL Code:
SELECT CURRENT_TIMESTAMP;
This will return a server timestamp representing the date and time of when the query was executed, more on this toward the end of the tutorial.
SQL Code:
SELECT COUNT(Lastname) FROM employees;
We will dive deeper into detail as the tutorial progresses, but be aware of this concept of selecting date by means of calling a function as it will prove more useful later on.
|