Beginner SQL Skills Challenge!
Howdy, SQL geeks. Hope this post finds you swell! Over the past few months, we’ve gained a ton of ground in learning SQL, or at least I have. 🙂 Let’s take a moment to: Test our knowledge of SQL skills learned thus far Start seeing SQL queries less as statements of code, and more as […]
Howdy, SQL geeks. Hope this post finds you swell!
Over the past few months, we’ve gained a ton of ground in learning SQL, or at least I have. 🙂
Let’s take a moment to:
- Test our knowledge of SQL skills learned thus far
- Start seeing SQL queries less as statements of code, and more as real world business challenges
In this article, we’ll have a recap plus three sections:
- Recap of the training database we’ve been working with
- General statements of each business problem
- Hints and thoughts about how to approach each problem
- Solutions to each problem
Recap: Our Training Database
Our training database is a best/old faithful. We’ve been using the surprisingly popular DVD rental database in a .tar file for our practice database.
Contained within this databases are various tables with fictitious information, including: customer information, film production information, business/pricing information and so forth.
In our challenges, we’ll execute various SQL queries to extract pieces of insight for business tasks. It’s assumed in this article that you’ve installed PostgreSQL via pgAdmin and have followed this article series so far, using the DVD rental training database.
Without further ado, let’s begin.
The SQL Challenges
Alright, here we go:
- How rentals were returned after July 17, 2005?
- How many actors have a last name that starts with the letter A?
- How many unique districts are represented in the customer database?
- Can you return the actual list of districts from challenge #3?
- Â How many films have a rating of R and a replacement cost between $5 and $15?
- How many films have the word Truman somewhere in the title?
How to Approach the Challenges
Right, then. In this section, we’ll add some color commentary (read: hints) to our challenges. This should help you understand the mechanics of the solution, while ensuring you can’t see the answers all in one screen. 😉
Challenge 1: Rentals Returned After July 17, 2005
As with all challenges, a problem well stated is half (or more) solved. So let’s look at the high levels of the ask, and work our way down. We need information on rentals, so this means we’ll probably be querying the rentals table.
We would want to first examine the rentals table in a concise manner by doing:
SELECT * FROM rental LIMITÂ 5;
Once you’ve surveyed the table, we really only need one column returned (pun not intended) and we only want the sum figure of returns, where (HINT!!) the return date was after (think a logical operator here) July 17, 2005.
Challenge 2: Actors That Have a Last Name Beginning with “A”
Like our first challenge, let’s work from the “top down”. We need actor information, so querying the actor table would be a great place to start. Similar to last time, we need a count of values matching a condition. The difference versus challenge #1  is we need to find match a pattern like or such as an actor’s last name that begins with the letter A.
Challenge 3: Number of Unique Districts in the Customer Database
The title and description could cause some confusion here. You may need to do some basic SELECT * FROM table_name LIMIT X; queries to make sure you’ve got the right table. Once you do, we’re looking for an amount of distinct values in the database. Order of operations matters.
Challenge 4: Returning the Actual Lists of Districts from Challenge #3
Not much to hint at here – getting challenge #3 is the key here. You’ll really only be simplifying the correct query in challenge #3 to get the correct answer here.
Challenge 5: Cheap, (Mildly) Naughty Films
This one might be the longest query yet in this challenge. So we’re looking up film information, thus should know which table to query. We’re returning a value where a certain rating must be returned, and (HINT) we need to layer in one more lens of qualification. That lens dictates we specify a range between (cough, hint!) two values.
Challenge 6: Where in the World are Films Containing “Truman”?
This challenge is more of a recency test than retention of older concepts. You’ll need to employ pattern matching again for this business challenge/query to find titles that have some match like Truman in the title.
Challenge Solutions
Is that your final answer? Below are the queries, with screenshots of what I did.
Solution 1: Rentals Returned After July 17, 2005
SELECT COUNT(return_date) FROM rental
WHERE return_date > ‘2005-07-17’;
Solution 2: Actors That Have a Last Name Beginning with “A”
SELECT COUNT(*) FROM actor
WHERE last_name LIKE ‘A%’;
Solution 3: Number of Unique Districts in the Customer Database
SELECT COUNT(DISTINCT(district)) FROM address;
Solution 4: Returning the Actual Lists of Districts from Challenge #3
SELECT DISTINCT(district) FROM address;
Solution 5: Cheap, (Mildly) Naughty Films
SELECT COUNT(*) FROM film
WHERE rating = ‘R’
AND replacement_cost BETWEEN 5 AND 15;
Solution 6: Where in the World are Films Containing “Truman”?
SELECT COUNT(*) FROM film
WHERE title LIKE ‘%Truman%’;
Wrap Up
Well done for completing these challenges! You shall indeed pass. 🙂
Soon, we’ll be covering aggregate SQL functions, such as MIN, MAX, AVG and SUM.
If you’re just joining us, here’s a running list of our articles so to date (4/16/2017):
- Starting PostgreSQL from the absolute beginning
- Getting Started with PostgreSQL and pgAdmin
- Creating, Restoring and Deleting PostgreSQL Databases
- Restoring PostgreSQL Databases with Table Schema Only
- The SELECT Clause, PostgreSQL Statement Fundamentals
- SELECT PostgreSQL Clause with DISTINCT Keyword
- SELECT WHERE: The Beginnings of Segmentation
- COUNT in PostgreSQL, Fundamental Statements
- The LIMIT Statement in PostgreSQL
- Starting to Sort in PostgreSQL: ORDER BY Clause
- Specifying Ranges of Data with BETWEEN in PostgreSQL
- More Segmentation: Using the IN Statement for PostgreSQL
- Pattern Matching: The LIKE Statement in PostgreSQL