In this workshop, we will be using basic SQL in a CLI to work with an existing blog database.
Make sure you have installed and setup PostgreSQL: installation instructions
Clone this workshop and cd
into it, then run these commands after using psql
:
CREATE DATABASE blog_workshop;
\c blog_workshop
- connect to the database
\i init.sql
- run SQL build file in blog_workshop (You can also do psql --file init.sql
in bash)
You are given the task of creating a blog. A simple blog would have users who can write blog posts, and blog posts that can contain comments and replies.
Users has no direct relationships to other tables, but a blog post has to have an author, so blog_posts
has a user_id
FOREIGN KEY
of users(id)
.
Comments have to be attached to a blog post, so post_id
has a post_id
FOREIGN KEY
of blog_posts(id)
.
Column | Type | Modifiers |
---|---|---|
id | serial (translates to integer and AUTO_INCREMENT) | PRIMARY KEY |
username | VARCHAR(255) | NOT NULL |
age | INTEGER | |
first_name | VARCHAR(255) | |
last_name | VARCHAR(255) | |
location | VARCHAR(255) |
Column | Type | Modifiers |
---|---|---|
id | SERIAL | PRIMARY KEY |
user_id | INTEGER | REFERENCES users(id) |
text_content | TEXT |
Column | Type | Modifiers |
---|---|---|
id | SERIAL | PRIMARY KEY |
post_id | INTEGER | REFERENCES blog_posts(id), |
reply_to | INTEGER | REFERENCES post_comments(id), |
user_id | INTEGER | REFERENCES users(id) |
text_content | TEXT |
- Using
SELECT
, retrieve all the information from theusers
table
Expected Result
id | username | age | first_name | last_name | location |
---|---|---|---|---|---|
1 | Sery1976 | 28 | Alisha | Clayton | Middlehill, UK |
2 | Notne1991 | 36 | Chelsea | Cross | Sunipol, UK |
3 | Moull1990 | 41 | Skye | Hobbs | Wanlip, UK |
4 | Spont1935 | 72 | Matthew | Griffin | Saxilby, UK |
5 | Precand | 19 | Erin | Gould | Stanton, UK |
6 | Ovion1948 | 53 | Reece | Sheppard | Easton in Gordano, UK |
7 | Thresuall | 21 | Daniel | Grant | Slackhall, UK |
8 | Brity1971 | 23 | Daniel | Brennan | Saxilby, UK |
- Using
SELECT
, retrieve a list of only username and location from theusers
table
Expected Result
username | location |
---|---|
Sery1976 | Middlehill, UK |
Notne1991 | Sunipol, UK |
Moull1990 | Wanlip, UK |
Spont1935 | Saxilby, UK |
Precand | Stanton, UK |
Ovion1948 | Easton in Gordano, UK |
Thresuall | Slackhall, UK |
Brity1971 | Saxilby, UK |
- Using
SELECT
andWHERE
, retrieve a list of users with all their data who are older than 50
Expected Result
id | username | age | first_name | last_name | location |
---|---|---|---|---|---|
4 | Spont1935 | 72 | Matthew | Griffin | Saxilby, UK |
6 | Ovion1948 | 53 | Reece | Sheppard | Easton in Gordano, UK |
- Using
SELECT
andWHERE
, retrieve the first, last name and location of the user who lives inSaxilby, UK
and is older than 40.
Expected Result
first_name | last_name | location |
---|---|---|
Matthew | Griffin | Saxilby, UK |
- Using the
WHERE
operator,LIKE
, retrieve a list of user IDs that have posted blog posts that contain the worddeparture
.
Expected Result
user_id |
---|
2 |
3 |
- Imagine an API request is made for blog posts with the IDs
3 and 6
. UsingWHERE
andIN
, show the blog posts' user id and text content.
Expected Result
user_id | text_content |
---|---|
3 | Far stairs now coming bed oppose hunted become his. You zealously departure had procuring suspicion. Books whose front would purse if be do decay. |
6 | Etiam in est nec neque dapibus pretium in in lectus. Proin consequat velit quis magna aliquam tristique. Sed ultricies nulla vel feugiat mattis. Aliquam erat volutpat. Aliquam ac vehicula diam, eget ultricies nisi. |
- You need to find out which of your users are teenagers and which are not. Using
CASE WHEN
andAS
, show a list of users by theirid
and a new column calledteenager
with the valuestrue
orfalse
.
Expected Result
id | teenager |
---|---|
1 | false |
2 | false |
3 | false |
4 | false |
5 | true |
6 | false |
7 | false |
8 | false |
So, we have retrieved data from the database. The next exercises will cover adding data
- Using
INSERT INTO
, add a blog post with the text "Hello world" to the user with ID1
, then runSELECT text_content FROM blog_posts WHERE user_id=1;
to test for the expected result.
Expected Result
text_content |
---|
Hello World |
- Using
UPDATE
, update the blog post from the previous question to change the author to the user with ID5
, then runSELECT user_id FROM blog_posts WHERE text_content='Hello world';
to test for the expected result.
Expected Result
user_id |
---|
5 |
Bonus Question
- Using
INSERT INTO
, add a comment with the textInteresting post
from the user_id of3
to the blog post containing the textPeculiar
(reply_to will be null), then runSELECT text_content FROM post_comments WHERE post_id = 2;
to test for the expected result.
Expected Result
text_content |
---|
Interesting post |