SELECT & FROM | BUS 104 – SQL Exam

Still feeling nervous for your exam? Check out the SQL Cram Kit for practice questions that emulate the difficulty that you'll face on your exam. Each question includes a thorough step-by-step explanation where I walk you through the entire problem solving process!

SQL Cram Kit logo

SELECT explained

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

Small schoolhouse with blue sky and green grass

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

Four students seated in the schoolhouse

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

Teacher portal with students webpage pulled up, listing student info

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

The students webpage with the first and last name columns highlighted

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…

Teacher portal with students webpage pulled up, listing student info

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

Grades and curriculum tabs highlighted in the teacher portal

...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...

Teacher portal with students webpage pulled up, listing student info

...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:

first_namelast_name
AndySherman
ShellyBeth
MarthaSmith
DannyAlbert

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

SQL Follow-along Guide

It's no secret you retain info better when you write it down. That's why I created the SQL Follow-along Guide for you!

As you come upon key concepts highlighted in yellow, like this sentence here, you can fill-in-the-blanks on your Follow-along Guide so that you remember all the important stuff for later.

You can obtain the SQL Follow-along Guide for FREE by entering your email below!

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

Concepts

Each exam concept broken down with relatable situations to your life!

ToolsSQL Follow-along Guide (FREE)
LessonWhat is SQL?
LessonSELECT & FROM
LessonJOIN
LessonWHERE
LessonGROUP BY
LessonHAVING
LessonORDER BY
LessonTroubleshooting errors

Practice Problems

Include step-by-step explanations through each part of the problem!

SQL Cram Kit Logo

"The SQL Cram Kit was the best thing I could’ve done before my exam. It takes practice and more practice to be good at coding and that is what I needed."

Andrew Green
October 29, 2020

Rating: 5 out of 5.

Leave a Comment