fbpx

SELECT & FROM | BUS 104 – SQL Exam

SELECT explained

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.

FROM explained

When we navigated to the Students page…

…we stated that we'd like to view data on the Students page. We could've chosen different pages...

...but instead of choosing to view data on Grades and/or Curriculum, we chose Students.

By choosing the Students page specifically, we performed a FROM statement. Out of all the pages in the teacher dashboard, we chose the page pertaining to students. In SQL terms...

FROM statements are used to state out of all the tables in a database, what specific table you'd like to access data from.

How to code a SELECT & FROM

Here's the template for a SELECT and FROM statements:

SELECT column1column2, …
FROM database_name.table_name;

We'll use this to apply SELECT and FROM 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
FROM database_name.table_name;

Some things to note here before moving on to the FROM statement…

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
FROM database_name.table_name;

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

SELECT age
FROM database_name.table_name;

What if we wanted to view all student attributes?

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

SELECT *
FROM database_name.table_name;

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.

first_namelast_nameagegender
AndySherman8M
ShellyBeth9F
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.

Now for the FROM statement. Let's say the schoolhouse named their database schoolhouse_db. We'd then place the following into our query.

SELECT first_name, last_name
FROM schoolhouse_db.table_name;

The table containing student information is called students. Therefore, we'll place the following into our query.

SELECT first_name, last_name
FROM schoolhouse_db.students;

And there you have it! When you execute this query, you'd get the following results:

Query Output

first_namelast_name
AndySherman
ShellyJohnson
MarthaSmith
DannyAlbert
first_namelast_name
AndySherman
ShellyBeth
MarthaSmith
DannyAlbert

You've just learned the foundational blocks of any SQL query!

Next up with JOIN, we'll learn how to link together tables in a query so that we can consider data from multiple tables.

SQL Cram Kit

Want to unlock content? Get your SQL Cram Kit now!

ApplyPRACTICE SQL EXAM (PREVIEW ONLY)
ToolsSQL Nerd Notes (PREVIEW ONLY)
ConceptWhat is SQL?
ConceptSELECT & FROM
ConceptJOIN
ConceptWHERE (PREVIEW ONLY)
ConceptGROUP BY (PREVIEW ONLY)
ConceptHAVING (PREVIEW ONLY)
ConceptORDER BY (PREVIEW ONLY)
ConceptTroubleshooting errors (PREVIEW ONLY)

Leave a Comment