SQL

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.

idfirstNamelastNameage
1JohnSmith21
2PatrickCasey26
3SamAdams19
4MikeGarciaNULL

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.

idfirstNamelastNameage
1JohnSmith21
2PatrickCasey26
3SamAdams19
4MikeGarciaNULL

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.

idlastNameage
1Smith21
2Casey26
3Adams19
4GarciaNULL

Next: SQL Where