<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Akers Newman (Kunle Mabadeje) on Medium]]></title>
        <description><![CDATA[Stories by Akers Newman (Kunle Mabadeje) on Medium]]></description>
        <link>https://medium.com/@akers.newman?source=rss-7620118d5898------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ZGePDTyFH15DT37r</url>
            <title>Stories by Akers Newman (Kunle Mabadeje) on Medium</title>
            <link>https://medium.com/@akers.newman?source=rss-7620118d5898------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 04:10:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@akers.newman/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[SQL Case Study (Tiny Shop Sales)]]></title>
            <link>https://medium.com/@akers.newman/sql-case-study-tiny-shop-sales-99ea864a7288?source=rss-7620118d5898------2</link>
            <guid isPermaLink="false">https://medium.com/p/99ea864a7288</guid>
            <category><![CDATA[join]]></category>
            <category><![CDATA[subquery]]></category>
            <category><![CDATA[cte]]></category>
            <dc:creator><![CDATA[Akers Newman (Kunle Mabadeje)]]></dc:creator>
            <pubDate>Wed, 24 May 2023 05:06:34 GMT</pubDate>
            <atom:updated>2023-05-24T05:06:34.326Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AjqgsWAi1bHKGxJoXTiv8Q.png" /></figure><p><strong><em>“Imagination is the beginning of creation. You imagine what you desire, you will what you imagine, and at last, you create what you will.” — George Bernard Shaw</em></strong></p><p>One of my mentors in data analytics once told me that if you can imagine the solution, you are very likely to solve SQL problems and I have found that to be very true. When faced with SQL questions, there is always that tendency to think of the ‘formula’ to use — should I use a subquery or a join or CTE? But I have found that the ability to picture or imagine the solution is far more important than the syntax itself. It’s also worth noting that SQL offers flexibility in terms of syntax. Different individuals may write SQL queries in slightly different ways, yet arrive at the same solution!</p><p>Being able to picture the solution involves analyzing the problem, breaking it down into logical steps, and determining the most appropriate approach. It’s about understanding the relationships between tables, identifying the necessary data transformations, and selecting the right SQL constructs to achieve the desired outcome.</p><p>Don’t want to bore you with my thought, let’s go straight to the challenge for today.</p><p>The challenge for today is provided by Data in Motion and you can find the dataset here <a href="https://d-i-motion.com/lessons/customer-orders-analysis/">SQL Case Study 1: Tiny Shop Sales — Data in Motion (d-i-motion.com)</a></p><p>You have the option to copy the dataset and upload it to your own environment, allowing you to execute the code and run the queries (in my case, I used MySQL).</p><pre>CREATE SCHEMA dim_c1;<br>CREATE TABLE customers (<br>    customer_id integer PRIMARY KEY,<br>    first_name varchar(100),<br>    last_name varchar(100),<br>    email varchar(100)<br>);<br><br>CREATE TABLE products (<br>    product_id integer PRIMARY KEY,<br>    product_name varchar(100),<br>    price decimal<br>);<br><br>CREATE TABLE orders (<br>    order_id integer PRIMARY KEY,<br>    customer_id integer,<br>    order_date date<br>);<br><br>CREATE TABLE order_items (<br>    order_id integer,<br>    product_id integer,<br>    quantity integer<br>);<br><br>INSERT INTO customers (customer_id, first_name, last_name, email) VALUES<br>(1, &#39;John&#39;, &#39;Doe&#39;, &#39;johndoe@email.com&#39;),<br>(2, &#39;Jane&#39;, &#39;Smith&#39;, &#39;janesmith@email.com&#39;),<br>(3, &#39;Bob&#39;, &#39;Johnson&#39;, &#39;bobjohnson@email.com&#39;),<br>(4, &#39;Alice&#39;, &#39;Brown&#39;, &#39;alicebrown@email.com&#39;),<br>(5, &#39;Charlie&#39;, &#39;Davis&#39;, &#39;charliedavis@email.com&#39;),<br>(6, &#39;Eva&#39;, &#39;Fisher&#39;, &#39;evafisher@email.com&#39;),<br>(7, &#39;George&#39;, &#39;Harris&#39;, &#39;georgeharris@email.com&#39;),<br>(8, &#39;Ivy&#39;, &#39;Jones&#39;, &#39;ivyjones@email.com&#39;),<br>(9, &#39;Kevin&#39;, &#39;Miller&#39;, &#39;kevinmiller@email.com&#39;),<br>(10, &#39;Lily&#39;, &#39;Nelson&#39;, &#39;lilynelson@email.com&#39;),<br>(11, &#39;Oliver&#39;, &#39;Patterson&#39;, &#39;oliverpatterson@email.com&#39;),<br>(12, &#39;Quinn&#39;, &#39;Roberts&#39;, &#39;quinnroberts@email.com&#39;),<br>(13, &#39;Sophia&#39;, &#39;Thomas&#39;, &#39;sophiathomas@email.com&#39;);<br><br>INSERT INTO products (product_id, product_name, price) VALUES<br>(1, &#39;Product A&#39;, 10.00),<br>(2, &#39;Product B&#39;, 15.00),<br>(3, &#39;Product C&#39;, 20.00),<br>(4, &#39;Product D&#39;, 25.00),<br>(5, &#39;Product E&#39;, 30.00),<br>(6, &#39;Product F&#39;, 35.00),<br>(7, &#39;Product G&#39;, 40.00),<br>(8, &#39;Product H&#39;, 45.00),<br>(9, &#39;Product I&#39;, 50.00),<br>(10, &#39;Product J&#39;, 55.00),<br>(11, &#39;Product K&#39;, 60.00),<br>(12, &#39;Product L&#39;, 65.00),<br>(13, &#39;Product M&#39;, 70.00);<br><br>INSERT INTO orders (order_id, customer_id, order_date) VALUES<br>(1, 1, &#39;2023-05-01&#39;),<br>(2, 2, &#39;2023-05-02&#39;),<br>(3, 3, &#39;2023-05-03&#39;),<br>(4, 1, &#39;2023-05-04&#39;),<br>(5, 2, &#39;2023-05-05&#39;),<br>(6, 3, &#39;2023-05-06&#39;),<br>(7, 4, &#39;2023-05-07&#39;),<br>(8, 5, &#39;2023-05-08&#39;),<br>(9, 6, &#39;2023-05-09&#39;),<br>(10, 7, &#39;2023-05-10&#39;),<br>(11, 8, &#39;2023-05-11&#39;),<br>(12, 9, &#39;2023-05-12&#39;),<br>(13, 10, &#39;2023-05-13&#39;),<br>(14, 11, &#39;2023-05-14&#39;),<br>(15, 12, &#39;2023-05-15&#39;),<br>(16, 13, &#39;2023-05-16&#39;);<br><br>INSERT INTO order_items (order_id, product_id, quantity) VALUES<br>(1, 1, 2),<br>(1, 2, 1),<br>(2, 2, 1),<br>(2, 3, 3),<br>(3, 1, 1),<br>(3, 3, 2),<br>(4, 2, 4),<br>(4, 3, 1),<br>(5, 1, 1),<br>(5, 3, 2),<br>(6, 2, 3),<br>(6, 1, 1),<br>(7, 4, 1),<br>(7, 5, 2),<br>(8, 6, 3),<br>(8, 7, 1),<br>(9, 8, 2),<br>(9, 9, 1),<br>(10, 10, 3),<br>(10, 11, 2),<br>(11, 12, 1),<br>(11, 13, 3),<br>(12, 4, 2),<br>(12, 5, 1),<br>(13, 6, 3),<br>(13, 7, 2),<br>(14, 8, 1),<br>(14, 9, 2),<br>(15, 10, 3),<br>(15, 11, 1),<br>(16, 12, 2),<br>(16, 13, 3);</pre><p>Question 1–3:</p><pre>-- 1) Which product has the highest price? Only return a single row.<br><br>select product_name as prod_max_price<br>from (<br>select product_name, max(price)<br>from products) as dm;<br><br>-- 2)  Which customer has made the most orders?<br><br>select o.customer_id, first_name, last_name, count(*) as order_count<br>from orders o<br>left join customers<br>using (customer_id)<br>group by o.customer_id, o.customer_id, first_name, last_name<br>limit 3;<br><br>-- 3) What&#39;s the total revenue per product?<br><br>select product_id, product_name, sum((price * quantity)) as revenue<br>from products p<br>left join order_items oi<br>using (product_id)<br>group by product_id, product_name<br>order by revenue;</pre><p>The initial three questions enable us to investigate the connections among various tables by identifying a shared characteristic, typically a primary key. By combining these tables based on this common attribute, we can merge the data and achieve the desired result.</p><p>Question 4–7:</p><pre>-- 4) Find the day with the highest revenue<br><br>SELECT order_date, sum(quantity * price) as revenue<br>from orders o <br>left join order_items oi<br>using (order_id)<br>left join products p<br>using (product_id)<br>group by order_date<br>order by revenue desc<br>limit 1;<br><br>-- 5) Find the first order (by date) for each customer.<br><br>select c.customer_id, first_name, last_name, min(order_date) as first_order_date<br>from customers c<br>left join orders o <br>using (customer_id)<br>group by c.customer_id;<br><br>-- 6) Find the top 3 customers who have ordered the most distinct products<br><br>SELECT customer_id, first_name, last_name, count(distinct product_id) as unique_product<br>from customers c<br>left join orders o<br>using (customer_id)<br>left join order_items oi<br>using (order_id)<br>group by customer_id<br>order by unique_product desc<br>limit 3;<br><br><br>-- 7) Which product has been bought the least in terms of quantity?<br><br>SELECT product_name, sum(quantity) total_qty<br>from order_items oi<br>left join products p<br>using (product_id)<br>group by product_id<br>order by total_qty asc<br>limit 3;</pre><p>One of the advantages of SQL is its ability to apply aggregate functions like `MIN()` and `MAX()` to date fields, allowing you to easily find the earliest and latest dates for an event. By using `MIN(order_date)` and grouping the results by `customer_id`, you can simplify the syntax and retrieve the desired information for question 5.</p><p>Question 8–10</p><pre><br>-- 8) What is the median order total?<br><br>WITH CTE AS (<br>  SELECT<br>    SUM(price * quantity) AS revenue,<br>    product_name,<br>    product_id<br>  FROM<br>    products p<br>  JOIN<br>    order_items oi USING (product_id)<br>  GROUP BY<br>    product_name,<br>    product_id<br>  ORDER BY<br>    revenue ASC<br>)<br>SELECT<br>  AVG(revenue) AS median_revenue<br>FROM (<br>  SELECT<br>    revenue,<br>    ROW_NUMBER() OVER (ORDER BY revenue) AS row_num,<br>    COUNT(*) OVER () AS total_rows<br>  FROM<br>    CTE<br>) subquery<br>WHERE<br>  row_num IN (FLOOR((total_rows + 1) / 2), CEIL((total_rows + 1) / 2));<br><br><br>-- 9) For each order, determine if it was ‘Expensive’ (total over 300), ‘Affordable’ (total over 100), or ‘Cheap’.<br><br>select order_id,<br>       CASE<br>          WHEN revenue &gt; 300 THEN &#39;Expensive&#39;<br>          WHEN revenue &gt; 100 THEN &#39;Affordable&#39;<br>          ELSE &#39;Cheap&#39;<br>          END AS price_bracket<br>          from (<br>select order_id, sum((price * quantity)) as revenue<br>from products p<br>left join order_items oi<br>using (product_id)<br>group by order_id<br>) as total_order;<br><br>-- 10) Find customers who have ordered the product with the highest price.<br><br>select c.customer_id, first_name, last_name<br>from customers c<br>left join orders<br>using (customer_id)<br>left join order_items oi<br>using (order_id)<br>left join products p<br>using (product_id)<br>where price = (<br>select max(price)<br>from products<br>);</pre><p>Question 8 can be considered the most challenging in this challenge since MySQL (as of the time of this writing) lacks a specific syntax for finding the median. However, as I mentioned earlier, if you can imagine it, you can solve it.</p><p>The median, which is a statistical measure representing the middle value in an ordered data set, cannot be directly calculated using a built-in aggregate function in MySQL. Nonetheless, SQL techniques such as sub-queries or CTEs can be employed to determine the median value.</p><p>Kunle Mabadeje</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99ea864a7288" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>