Putting It Together: How a Blog or Shop Uses These Queries
SQL in real Gwecode projects
You have now covered a full, genuinely useful slice of SQL: tables, SELECT, WHERE, ORDER BY, LIMIT, INSERT, UPDATE, DELETE, CREATE TABLE, data types, primary keys, JOIN, and the aggregate functions COUNT, SUM, and AVG. This lesson walks through how all of these pieces work together inside two familiar kinds of website.
A blog, from signup to homepage
Someone signs up on your Gwecode site, which runs an INSERT into a users table. They write their first post, which runs an INSERT into a posts table, storing their user id as the author_id. Your homepage then shows the newest posts with:
SELECT title FROM posts ORDER BY created_at DESC LIMIT 5;When a visitor opens one post, you show the author's username by joining posts to users:
SELECT posts.title, users.username
FROM posts
JOIN users ON posts.author_id = users.id
WHERE posts.id = 1;If the author spots a typo, you fix it with UPDATE. If a comment turns out to be spam, you remove it with DELETE, always with a careful WHERE clause. Your admin dashboard might show the total number of posts with COUNT(*).
A shop, from checkout to dashboard
A shop typically has a products table, a customers table, and an orders table linking the two together. When someone checks out, that runs an INSERT into orders, storing the customer's id. To show the shop's cheapest items first:
SELECT name, price FROM products ORDER BY price ASC LIMIT 10;To see how much a specific customer has spent in total, you combine WHERE with SUM:
SELECT SUM(price) FROM orders WHERE customer_id = 3;When stock runs low on an item, the shop owner fixes it with a careful UPDATE, and when a product is discontinued, it might be removed entirely with DELETE.
You are ready to build with this
Every one of these examples is built entirely from the statements you already know. There is no missing trick left to learn before you can start using SQL for a real project. The best next step is simply practice: sketch a small table for something you want to build, create it, insert a few rows, and start asking it questions.
Quick check
Q1 A shop wants to show its 5 most recently placed orders. Which combination of clauses helps achieve this?
Q2 Which SQL feature lets a blog show a post together with the username of its author, when the username is stored in a separate users table?
Want to save your progress? It's free.
Create a free account