If you finished downloading QueryPie and connecting to the database already, it’s time to manage and find data. Let’s go over what we can do with the query SELECT, FROM, and WHERE.

📌Table of Contents

Query 1. Using SELECT for ALL columns
SELECT * FROM film;

Query 2. Using SELECT for a SPECIFIC column
SELECT film_id, title, rating, special_features FROM film;

Query 3. Using WHERE
SELECT film_id, title, rating, special_features FROM film WHERE rating='R';

# SELECT, FROM, WHERE

The most basic ‘Questions’ that we can ask in SQL are SELECT, FROM, and WHERE. They have an intuitive meaning, so it’s pretty easy to guess the function by reading them. Let’s go over them quickly.

📌SELECT: you’re telling the database what to show you
📌FROM: you’re giving the database a location to search
📌WHERE: you’re narrowing down/specifying the location

🔑 Starting now, we will refer to our ‘Questions’ as queries.

So let’s go over our first query.

Q1. What kind of data is in the ‘film’ table? Show me!

SELECT(the desired ‘columns’) FROM(in specific ‘table’)

This is the most basic query. We can start a conversation with any database
by just using this sentence.

For this example, we’re going to use data in the Film table inside the Sample Database sakila.

Applying this idea to Query 1, we can read it as “SELECT ‘columns’ FROM the ‘film’ table.” We don’t need the word “table” after the name of the table. SQL already knows that the word following the query FROM is going to be the name of a table. So write FROM film.

If you want to see all available data of every column in the specified table at once, use the asterisk symbol ( * ). The * symbol here means all data of all columns. Let’s say we want to show all the columns containing information inside the film table. In SQL, this roughly translates to “SELECT(show) *(all columns containing information) FROM(inside the) film(table)”. Remove all the parenthesis, and you will get the simple SQL query:

SELECT * FROM film;

Make sure to use a semicolon ( ; ) at the end of the sentence to let SQL know that this is the end of your query and you are ready to see the results.

✦ Click SQL Run at the top left to try your query
You should see something similar to the image shown below.

The result is a ton of columns loaded with information about movies!

But let’s say we want only to see information about the film itself without the extra clutter of the rental rate or duration of the movie.

Q2. There are too many columns here!! Let’s only look at ID, title, rating, and the special features in the film table.

SELECT(specific desired ‘column’s) FROM(in specific ‘table’)

To do so, we’re going to focus on the columns film ID, title, rating, and special features. Just plug in the exact name of the column you want after SELECT to filter the information. In this case, you can use this:

SELECT film_id, title, rating, special_features FROM film;

Q3. Okay, now let’s narrow it down more. Let’s only look at the film ID, title, rating, and special features of R-rated films.

SELECT(show this ‘column’) FROM(specific ‘table’) WHERE(meets condition)
If condition is number: WHERE film_id=5
If condition is character: WHERE rating=‘PG’ —  (Add single quotations!!)

Now that we know how to find specific data, let’s narrow it down some more. Let’s say we only want information about movies that are of particular rating (R, PG-13, PG, etc). To do this, we need to start using the SQL query WHERE.

Just add WHERE to the end of your query following your desired rating. In this example, we are going to look up R-rated movies. Because, why not?

Plugin WHERE rating=‘R’ at the end of the statement. Notice here that we’ve added single quotation marks around the letter R ( ‘ ’ ). This is because SQL needs this extra bracket when the condition is not a number. There are ways around this, but for beginners, it’s good to have a solid foundation. So let’s use those single quotations! Your final query should look something like this:

SELECT film_id, title, rating, special_features FROM film WHERE rating='R';

✦ Remember to click SQL Run at the top left to execute your query

# Practice Time

Try using SELECT, FROM, and WHERE in this example scenario:

📰 A customer at your movie store approaches you and asks for a list of all the films that have a rental duration of 3 days. She’s also inquiring about the price (rental rate). How can you look up this information? Don’t forget to also search for the ID number so you can easily find the movies on the shelves.

Here’s the answer:

SELECT film_id, title, rental_rate, rental_duration FROM film WHERE rental_duration=3;