SQL Select
SQL Select
This tutorial covers SQL Select statement.
The previous tutorials briefly explored the concept of the “select” statement.
In SQL, “select” is one of the main statements of the programming language. Its usage focuses on retrieving (selecting) data from a database.
The syntax is very easy. The keywords are “select from”, signifying what data to retrieve and from where.
This page encompasses:
- selecting all columns
- selecting specific columns
Let’s use the table “student_info” from the previous tutorials.
id | firstName | lastName | age |
---|---|---|---|
1 | John | Smith | 21 |
2 | Patrick | Casey | 26 |
3 | Sam | Adams | 19 |
4 | Mike | Garcia | NULL |
Select all (*)
In SQL (and many other programming languages), the asterisk sign (*) represents all elements.
As such, we use it to retrieve all records from a table, with the help of the “select” statement.
The following is an example of a “select” statement.
SELECT * FROM student_info;
The above statement outputs the following result.
id | firstName | lastName | age |
---|---|---|---|
1 | John | Smith | 21 |
2 | Patrick | Casey | 26 |
3 | Sam | Adams | 19 |
4 | Mike | Garcia | NULL |
In other words, the whole table.
Select specific columns
Sometimes, we do not need to retrieve all columns.
Selecting specific columns is very straightforward. We write which columns we need, separated by commas.
For instance, let’s retrieve only the surname and the age of all students.
SELECT lastName, age FROM student_info;
The following is the outcome of the above statement.
id | lastName | age |
---|---|---|
1 | Smith | 21 |
2 | Casey | 26 |
3 | Adams | 19 |
4 | Garcia | NULL |
Next: SQL Where