fbpx

SELECT

Welcome to this schoolhouse! It contains one classroom with 4 students. You're the substitute teacher for the day!

You need to take attendance for class. You need the first and last name of every student in the classroom.

So, you log into the teacher dashboard and click on the Students page...

...and identify the first and last name columns.

You then take attendance by referring to this data.

By focusing on the first and last name columns, you're performing a SELECT statement. You're blocking out all the other columns and only focusing on the ones that pertain to your needs.

SELECT statements are used to declare what fields, or columns, of data you'd like to view from a table.

How to code a SELECT

Here's the template for a SELECT statement:

SELECT column1column2, …

We'll use this to apply SELECT statements to our schoolhouse attendance scenario.

We stated we'd like to view the first and last name of every student. Let's say these values are stored in fields called first_name and last_name. We'd then place the following into our SELECT statement:

SELECT first_name, last_name

What if we wanted to view the age of each student too?

Then we'd just add on the age field!

SELECT first_name, last_name, age

And if we just wanted to view the age of each student, we'd remove the other fields!

SELECT age

What if we wanted to view all student attributes?

We'd use the "*" operator in the SELECT statement.

SELECT *

The "*" essentially means "all the fields in a table".

That's why it enables us to see all attributes of each record.

If we executed this query, what would it return? Exactly what we see in the Students page...

...but in an SQL results table.

Query Output

first_namelast_nameagegender
AndySherman8M
ShellyJohnson9F
MarthaSmith8F
DannyAlbert11M

Notice how each field in our results table corresponds to the columns in the Students page?

That's because the "*" in our SELECT tells SQL to include all fields in the results.

All the fields for students are First Name (first_name), Last Name (last_name), Age (age), and Gender (gender).

So, specifying "*" in our SELECT ensures all these fields are displayed.

I've grayed out the names, ages, and genders because technically speaking... those wouldn't appear with our query as is. We need a FROM statement to declare what database & table we're pulling this data from!