What I Learned Building the Sunrise Supermarket Database with SQL π
Working on the Sunrise Supermarket SQL project was one of those exercises that helped turn SQL from a collection of commands into something I could actually use to answer questions from data.
Instead of working with random tables, I worked with a supermarket scenario involving customers, orders, order items, and products. This made it easier to understand how SQL can be used to explore business data and extract useful information.
Here are some of the key things I learned.
1. SQL becomes more meaningful when you work with real-world scenarios
One of my biggest takeaways was that SQL is not just about writing queries. It is about asking questions and using data to answer them.
For example:
- Which products cost more than 100?
- Which customers are not from Nairobi?
- Which customers are from specific cities?
- Which products contain the word "Oil"?
- Which orders are still pending?
- Which products are the most expensive?
- How many orders has each customer placed?
These questions helped me understand that a database becomes useful when we can interact with it to retrieve information that supports decision-making.
2. Filtering data with WHERE
I learned how the WHERE clause allows us to retrieve only the records that meet a particular condition.
For example, to find products priced above 100:
SELECT *
FROM sunrise.products
WHERE unit_price > 100;
I also used WHERE with different operators, including:
-
>for greater than -
<>for not equal to -
BETWEENfor values within a range -
INfor matching multiple possible values -
LIKEfor searching patterns
For example, BETWEEN helped me find products within a specific price range, while IN allowed me to filter customers from Nairobi, Nakuru, or Mombasa.
This helped me understand that filtering is one of the most important skills when working with databases because we rarely need every record in a table.
3. Using LIKE to search for patterns
Another useful concept I learned was the LIKE operator.
For example:
SELECT *
FROM sunrise.products
WHERE product_name LIKE '%Oil%';
The % acts as a wildcard, meaning the word "Oil" can appear anywhere within the product name.
This is particularly useful when searching through text data where we don't know the exact value we're looking for.
4. Sorting results with ORDER BY
Filtering tells us which records we want, but sometimes we also need to control how those records are displayed.
That's where ORDER BY comes in.
For example, I used:
SELECT *
FROM sunrise.order_items
WHERE status = 'Pending'
ORDER BY order_date ASC;
This allowed me to retrieve pending orders and arrange them from the earliest order to the latest.
I also learned how to combine ORDER BY with LIMIT.
SELECT *
FROM sunrise.products
ORDER BY unit_price DESC
LIMIT 2;
This returns the two most expensive products.
The important lesson here was that SQL clauses can work together to answer much more specific questions.
5. Aggregate functions help turn rows into information
Another major concept I learned was aggregate functions.
Instead of looking at individual records, aggregate functions allow us to summarize data.
For example:
SELECT customer_id, COUNT(*) AS total_orders
FROM orders
GROUP BY customer_id;
This calculates the number of orders placed by each customer.
Some commonly used aggregate functions include:
-
COUNT()β counts records -
SUM()β calculates a total -
AVG()β calculates an average -
MIN()β finds the smallest value -
MAX()β finds the largest value
This was particularly interesting because it showed me how SQL can move beyond simply retrieving data and actually help summarize it.
6. Understanding the difference between WHERE and HAVING
One concept that initially required more attention was the difference between WHERE and HAVING.
I used HAVING to find customers who had placed more than one order:
SELECT customer_id, COUNT(*) AS total_orders
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 1;
The important thing I learned is:
WHERE filters individual rows, while HAVING filters grouped results.
So when working with aggregate functions and GROUP BY, HAVING becomes particularly useful.
7. Joins are what connect the database together π
This was probably one of the most important lessons from the project.
A database can contain several related tables, and the information we need may be spread across them.
In the Sunrise Supermarket database, for example, customers are connected to orders, while orders are connected to order items, which are connected to products.
INNER JOIN
I used an INNER JOIN to connect customers and orders:
SELECT
Customers.Full_Name,
Orders.Order_ID,
Orders.Status
FROM Customers
INNER JOIN Orders
ON Customers.Customer_ID = Orders.Customer_ID;
This allowed me to see a customer's name alongside their order information.
LEFT JOIN
I also learned about LEFT JOIN.
SELECT
Orders.Order_ID,
Order_Items.Product_ID,
Order_Items.Quantity
FROM Orders
LEFT JOIN Order_Items
ON Orders.Order_ID = Order_Items.Order_ID;
One thing that stood out to me was that a LEFT JOIN can still return a record from the left table even when there is no matching record in the right table. In that case, the columns from the right table return NULL.
This helped me understand why choosing the correct type of join matters.
8. Combining multiple tables
The project then took joins a step further by connecting four tables:
Customers β Orders β Order Items β Products
SELECT
Customers.Full_Name,
Orders.Order_ID,
Products.Product_Name,
Order_Items.Quantity
FROM Customers
JOIN Orders
ON Customers.Customer_ID = Orders.Customer_ID
JOIN Order_Items
ON Orders.Order_ID = Order_Items.Order_ID
JOIN Products
ON Order_Items.Product_ID = Products.Product_ID;
This produces a much more useful view of the data because I can see:
- Who placed the order
- The order ID
- What product was ordered
- How many units were ordered
This was the point where I started seeing how relational databases are designed to work together rather than treating every table as an isolated spreadsheet.
9. Combining JOIN, GROUP BY, and SUM()
The final challenge pushed the concept even further.
I used the joined tables together with GROUP BY and SUM() to calculate the total quantity ordered for each product:
SELECT
Products.Product_Name,
SUM(Order_Items.Quantity) AS Total_Quantity_Ordered
FROM Customers
JOIN Orders
ON Customers.Customer_ID = Orders.Customer_ID
JOIN Order_Items
ON Orders.Order_ID = Order_Items.Order_ID
JOIN Products
ON Order_Items.Product_ID = Products.Product_ID
GROUP BY Products.Product_Name;
This showed me how multiple SQL concepts can be combined to answer a business question rather than simply retrieve raw records.
For a supermarket, this kind of query could help identify which products are selling the most.
What I Took Away From the Project
The Sunrise Supermarket project taught me that learning SQL is not really about memorising syntax.
It's about understanding how data is structured, what question you're trying to answer, and which SQL tools can help you answer it.
My major takeaways were:
-
WHEREhelps filter individual records. -
Operators such as
IN,BETWEEN, andLIKEmake filtering more flexible. -
ORDER BYhelps organise query results. -
LIMITallows us to restrict the number of results returned. - Aggregate functions help summarise data.
-
GROUP BYallows us to analyse data by categories. -
HAVINGfilters grouped results. -
JOINallows us to bring related information from different tables together. - Combining these concepts allows SQL to answer meaningful business questions.
Final Thoughts:
Before working on this project, SQL could easily feel like a list of commands to memorize.
The Sunrise Supermarket database helped me see it differently.
Every query starts with a question.
Who are my customers? What are they buying? Which products are moving? How many orders are being placed?
SQL provides the tools to turn those questions into answers.
And that, for me, was the biggest lesson from this project: SQL isn't just about querying databases. It's about making data useful. π


Top comments (0)