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.
Here's the template for a SELECT statement:
SELECT column1, column2, …
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
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
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_name | last_name | age | gender |
---|---|---|---|
Andy | Sherman | 8 | M |
Shelly | Johnson | 9 | F |
Martha | Smith | 8 | F |
Danny | Albert | 11 | M |
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!