<?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 Eloise Yemima on Medium]]></title>
        <description><![CDATA[Stories by Eloise Yemima on Medium]]></description>
        <link>https://medium.com/@eyemimasari?source=rss-2ffdcb0a6f43------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*wl_jS1hfkofw80T-bCZdgg.jpeg</url>
            <title>Stories by Eloise Yemima on Medium</title>
            <link>https://medium.com/@eyemimasari?source=rss-2ffdcb0a6f43------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 19:16:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eyemimasari/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[8 Week SQL — Week 1 (Danny’s Diner)]]></title>
            <link>https://medium.com/@eyemimasari/8-week-sql-week-1-dannys-diner-9d7edd58a833?source=rss-2ffdcb0a6f43------2</link>
            <guid isPermaLink="false">https://medium.com/p/9d7edd58a833</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[8-week-sql-challenge]]></category>
            <dc:creator><![CDATA[Eloise Yemima]]></dc:creator>
            <pubDate>Tue, 09 Dec 2025 10:38:01 GMT</pubDate>
            <atom:updated>2025-12-09T10:38:01.283Z</atom:updated>
            <content:encoded><![CDATA[<h3>8 Week SQL — Week 1 (Danny’s Diner)</h3><p>Hi there! Eloise here..</p><p>I’ve been thinking of polishing off my SQL during my free time (because I don’t want it to get rusty) and came across this 8 Week SQL Challenge by Data with Danny, so I thought I’d give it a try!</p><p><a href="https://8weeksqlchallenge.com/case-study-1/">Case Study #1 - Danny&#39;s Diner</a></p><p>In this post, I will be walking you through the first case study and bonus questions - explaining my thought process and decision making, as well as giving you a peek at the code and query result. I will be using postgresql for the entirety of this 8 week challenge. If you want to go straight to the answers, you can visit my github page below.</p><p><a href="https://github.com/ellieyeayea/8_Week_SQL">GitHub - ellieyeayea/8_Week_SQL: Doing the 8 Week SQL Challenge by Data with Danny</a></p><p>Disclaimer: This post is just me explaining my thought process and answer. The SQL code posted here is not the official answer from the creator, so there might be some misinterpretation from my part regarding the questions.</p><p>With that out of the way, lets crack on!</p><p>First of all, before even looking at the questions, we should check out the Entity Relationship Diagram (ERD). ERD will show you the connection between the tables in the schema, and will also show each column that is in each table. Think of it as like a map to the database! You do not have to memorize it, you just have to identify what kind of data is in each table and how the tables connect with each other.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/888/1*lwV5hU1BGuinIRRtNu_iSg.png" /><figcaption>Danny’s Diner Entity Relationship Diagram (ERD)</figcaption></figure><p>With this ERD for example, <em>members</em> table details all the members of Danny’s Diner and<em> menu</em> table details all the products that Danny’s Diner sell. While <em>sales </em>table details all transactions ever done in Danny’s Diner.</p><p>Each table is connected with id columns, by <em>customer_id </em>and<em> product_id. customer_id </em>connect <em>sales </em>and<em> members </em>table, while <em>product_id </em>connect <em>sales </em>and<em> menu </em>table. From this connection, we could identify if a customer is a member or not and what kind of product they ordered for each transaction.</p><p>Now that we understand the ERD, we could get on to the questions.</p><blockquote>Question #1: What is the total amount each customer spent at the restaurant?</blockquote><p>To answer this question, we need to see how much the customer spends on each transaction and sum it all up. Since each transaction row exclusively only allow 1 menu, we only need to join the <em>sales </em>table with <em>menu </em>table to get each product price. Then, using SUM()<strong> </strong>aggregate function, we sum all the price grouped by each <em>customer_id</em>.</p><pre>SELECT <br> s.customer_id,<br>    SUM(m.price)<br>FROM dannys_diner.sales as s<br> JOIN dannys_diner.menu as m<br>     ON s.product_id = m.product_id<br>GROUP BY s.customer_id;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U1RfAjP1O8f_PgjKq68LdQ.png" /><figcaption>Query Result #1</figcaption></figure><blockquote>Question #2: How many days has each customer visited the restaurant?</blockquote><p>Making use of COUNT() aggregate function, we could count the order_date in the <em>sales </em>table grouped by each <em>customer_id</em> to get how many days each customer visits the restaurant.</p><pre>SELECT <br> customer_id,<br> COUNT(DISTINCT order_date)<br>FROM dannys_diner.sales<br>GROUP BY customer_id;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NGfvUCI_ew32iaRKMBeDtg.png" /><figcaption>Query result #2</figcaption></figure><blockquote>Question #3: What was the first item from the menu purchased by each customer?</blockquote><p>For this question, I use postgresql DISTINCT ON() clause because it could fetch the first unique row based on the columns inside the brackets. Since we wanted to fetch the item per each customer, i put <em>customer_id</em> inside the clause. Then, I ordered the data based on the <em>order_date</em> so that the first order is placed at the very top and would be fetched by the DISTINCT ON() clause.</p><pre>SELECT DISTINCT ON (s.customer_id) <br> s.customer_id,<br>    s.order_date,<br>    m.product_name<br>FROM dannys_diner.sales as s<br> JOIN dannys_diner.menu as m<br>     ON s.product_id = m.product_id<br>ORDER BY s.customer_id, s.order_date;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UFvPBBd115nsRPUip4FWwA.png" /><figcaption>Query Result #3</figcaption></figure><blockquote>Question #4: What is the most purchased item on the menu and how many times was it purchased by all customers?</blockquote><p>This question is straightforward. We just have to utilize the COUNT() aggregate function grouped by each <em>product_id. </em>But since I also wanted to show the product name in the query result, I also have to group it by its <em>product_name. </em>The query is also ordered by the result of the COUNT() function to put the item with the highest count at the very top. Limit 1 is to limit the amount of row shown in the result, which ultimately remove other results but the first item.</p><pre>SELECT DISTINCT<br> s.product_id,<br>    m.product_name,<br>    COUNT(s.order_date) as cnt<br>FROM dannys_diner.sales as s <br> JOIN dannys_diner.menu as m<br>     ON s.product_id = m.product_id<br>GROUP BY s.product_id, m.product_name<br>ORDER BY cnt DESC<br>LIMIT 1;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XylljnEux53Fs_89B9xO3w.png" /><figcaption>Query Result #4</figcaption></figure><blockquote>Question #5: Which item was the most popular for each customer?</blockquote><p>Again, using the DISTINCT ON() clause to get the first unique instance for each customer. Combined with using COUNT() aggregate function to determine how much each item was ordered by each customer. Then ordering the results based on COUNT() results so that the most ordered item is placed at the top.</p><p>However, the downside of this method is if a customer has more than 1 most ordered item, it won’t show on the query result. In this case, customer B’s most ordered product is actually sushi, ramen, and curry with each item ordered exactly twice; But as you can see the result only show sushi as the most ordered item. Ultimately, I decided to only show the first item ordered by its <em>product_id </em>since the question asked for item instead of items.</p><pre>SELECT DISTINCT ON (s.customer_id)<br> s.customer_id,<br>    s.product_id,<br>    COUNT(s.order_date) as cnt,<br>    m.product_name<br>FROM dannys_diner.sales as s<br> JOIN dannys_diner.menu as m<br>     ON s.product_id = m.product_id<br>GROUP BY s.customer_id, s.product_id, m.product_name<br>ORDER BY s.customer_id, cnt DESC, s.product_id;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NPYOcOcwS2F3KcP_wX6q5g.png" /><figcaption>Query Result #5</figcaption></figure><blockquote>Question #6: Which item was purchased first by the customer after they became a member?</blockquote><p>The combination of all three tables is needed to answer this question, so all of them were joined. Then, I used the WHERE clause to filter the transactions that is done only after a customer became a member, setting<em> sales </em>table<em> order_date</em> to be bigger than or equals the <em>members </em>table <em>join_date.</em></p><p>Finally, using DISTINCT ON() clause combined with ORDER BY clause again, we get the first product per item per customer. One thing I noticed is with or without the ORDER BY clause, the query result is the same. It may look like we do not need the ORDER BY clause at all, but that is not the case. By not using it, we are making our query result unreliable as we are letting SQL randomly order the query result. The reason I said that the answer would be unreliable is because there is a chance that your query result will differ in between each executions. So, I would advise you to keep the ORDER BY clause.</p><pre>SELECT DISTINCT ON (s.customer_id)<br> s.customer_id,<br> s.order_date,<br>    m.product_name<br>FROM dannys_diner.sales as s<br>    JOIN dannys_diner.members as mb<br>     ON s.customer_id = mb.customer_id<br>    JOIN dannys_diner.menu as m <br>     ON s.product_id = m.product_id<br>WHERE s.order_date &gt;= mb.join_date<br>ORDER BY s.customer_id, s.order_date;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q9vu0v0RVsLM1CXpaBmrxA.png" /><figcaption>Query Result #6</figcaption></figure><blockquote>Question #7: Which item was purchased just before the customer became a member?</blockquote><p>This problem follows the same logic as Question #6, with only differing rule inserted to the WHERE clause. Also, the <em>order_date</em> is descendingly ordered so that the highest order date before a customer becomes a member is placed at the very top and taken by the DISTINCT ON() clause.</p><pre>SELECT DISTINCT ON (s.customer_id)<br> s.customer_id,<br> s.order_date,<br>    m.product_name<br>FROM dannys_diner.sales as s<br>    JOIN dannys_diner.members as mb<br>     ON s.customer_id = mb.customer_id<br>    JOIN dannys_diner.menu as m <br>     ON s.product_id = m.product_id<br>WHERE s.order_date &lt; mb.join_date<br>ORDER BY s.customer_id, s.order_date DESC;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pz1yGnzkh3nKQGlAL9NSeg.png" /><figcaption>Query Result #7</figcaption></figure><blockquote>Question #8: What is the total items and amount spent for each member before they became a member?</blockquote><p>Since both the total items and amount spent are counted per customer, there would be no difference in grouping. Hence why we can use both COUNT() and SUM() aggregate function to get them simultaneously.</p><pre>SELECT <br> s.customer_id,<br>    COUNT(s.product_id),<br>    SUM(m.price)<br>FROM dannys_diner.sales as s<br>    JOIN dannys_diner.members as mb<br>     ON s.customer_id = mb.customer_id<br>    JOIN dannys_diner.menu as m <br>     ON s.product_id = m.product_id<br>WHERE s.order_date &lt; mb.join_date<br>GROUP BY s.customer_id;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9aSBRoUD787-6owPrlPfOA.png" /><figcaption>Query Result #8</figcaption></figure><blockquote>Question #9: If each $1 spent equates to 10 points and sushi has a 2x points multiplier — how many points would each customer have?</blockquote><p>If you are familiar with programming languages, you would be familiar with if then statements. CASE WHEN statement is the if then statement in SQL. We wanted to double the points if the customer buys sushi, so after CASE WHEN we would put the condition, and after THEN we would put the outcome. This CASE WHEN statement would basically count the points for every row of transaction. Then, the SUM() aggregate function combined with GROUP BY clause would sum the points counted per customer.</p><pre>SELECT<br> s.customer_id,<br>    SUM(<br>       CASE WHEN m.product_name = &#39;sushi&#39; THEN m.price*10*2<br>       ELSE m.price*10<br>       END<br>       ) as points<br>FROM dannys_diner.sales as s<br> JOIN dannys_diner.menu as m<br>     ON s.product_id = m.product_id<br>GROUP BY s.customer_id;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rhfMVFyNly-jUJl-8D3rDA.png" /><figcaption>Query Result #9</figcaption></figure><blockquote>Question #10: In the first week after a customer joins the program (including their join date) they earn 2x points on all items, not just sushi — how many points do customer A and B have at the end of January?</blockquote><p>Similar to Question #9, we would have to use CASE WHEN statement to count the points. With some tweaking on the CASE WHEN to fulfill the first week condition. Also, we only want to count the points for January, so we have to user EXTRACT(MONTH from date) function in the WHERE clause.</p><pre>SELECT<br> s.customer_id,<br>    SUM(<br>       CASE WHEN (s.order_date BETWEEN mb.join_date AND mb.join_date + 7) THEN m.price*10*2<br>       WHEN (s.order_date NOT BETWEEN mb.join_date AND mb.join_date + 7) AND m.product_name = &#39;sushi&#39; THEN m.price*10*2<br>       ELSE m.price*10<br>       END<br>       ) as points<br>FROM dannys_diner.sales as s<br> JOIN dannys_diner.menu as m<br>     ON s.product_id = m.product_id<br> JOIN dannys_diner.members as mb<br>     ON s.customer_id = mb.customer_id<br>WHERE EXTRACT(MONTH from s.order_date) = 1<br>GROUP BY s.customer_id;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nehHxeuEBl1WVWLjr29YjQ.png" /><figcaption>Query Result #10</figcaption></figure><blockquote>Bonus Question #1: Join All The Things</blockquote><p>For the bonus questions, I decided to create views because the description given made it seem like Danny’s Diner team wouldn’t want to join the tables every time. Views are like virtual table, its contents are not stored in the database, but rather presents the result from the defined query stated when it was created.</p><p>The columns wanted by Danny’s Diner team details the purchase transaction, when it was done, the name of the product and its price, and also if the customer is a member or not.</p><p>Those columns come from all three tables, so I joined them all. Since not all customers are members, I decided to use LEFT JOIN when joining <em>sales </em>table and <em>members </em>table, so that the non-member customer data are still shown.</p><p>I also used CASE WHEN statements to custom the values shown on the <em>member </em>column, based on whether the customer has become a member or not on the date of the transaction.</p><pre>CREATE VIEW All_data AS<br>SELECT<br> s.customer_id,<br>    s.order_date,<br>    m.product_name,<br>    m.price,<br>    CASE WHEN s.order_date &gt;= mb.join_date THEN &#39;Y&#39;<br>     ELSE &#39;N&#39;<br>    END as member<br><br>FROM dannys_diner.sales as s<br> JOIN dannys_diner.menu as m<br>     ON s.product_id = m.product_id<br> LEFT JOIN dannys_diner.members as mb<br>     ON s.customer_id = mb.customer_id<br>ORDER BY s.customer_id, s.order_date;<br><br>Select * from All_data;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UfYDw4qTPZN5me0W9st_Rw.png" /><figcaption>Query Result #11 (Preview)</figcaption></figure><blockquote>Bonus Question #2: Rank All The Things</blockquote><p>This is a tricky one.</p><p>First of all, I created a subquery I named as <em>r </em>to get the product rankings after the customers become members. This subquery uses the RANK() window function to assign the product rank grouped by each customer and ordered by the order date. The reason we need to create this subquery is because we still want to show the non-members transaction albeit without the ranking, so we have to separate the product ranking process.</p><p>I also used COALESCE() function to assign a custom value to show in <em>ranking </em>column if the ranking is not found on the <em>r </em>subquery. Aside from that, the rest of the query is similar to Bonus Question #1.</p><pre>CREATE VIEW Member_rank AS<br>SELECT<br> s.customer_id,<br>    s.order_date,<br>    m.product_name,<br>    m.price,<br>    CASE WHEN s.order_date &gt;= mb.join_date THEN &#39;Y&#39;<br>     ELSE &#39;N&#39;<br>    END as member,<br> COALESCE(r.ranking, null) as ranking<br>FROM dannys_diner.sales as s<br> JOIN dannys_diner.menu as m<br>     ON s.product_id = m.product_id<br> LEFT JOIN dannys_diner.members as mb<br>     ON s.customer_id = mb.customer_id<br> LEFT JOIN (SELECT DISTINCT<br>                s.customer_id,<br>                s.order_date,<br>                RANK()OVER(PARTITION BY s.customer_id ORDER BY s.order_date) as ranking<br>            FROM dannys_diner.sales as s<br>                 JOIN dannys_diner.members as mb<br>                    ON s.customer_id = mb.customer_id<br>            WHERE s.order_date &gt;= mb.join_date) as r<br>         ON s.customer_id = r.customer_id<br>         AND s.order_date = r.order_date<br>ORDER BY s.customer_id, s.order_date;<br><br>Select * from Member_rank;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3GiCBVhpxupHaSHQwjvZYg.png" /><figcaption>Query Result #12 (Preview)</figcaption></figure><p>Phew! That wraps up my answer for the Week 1 challenge. I can’t lie I am not used to postgresql (since I usually use OracleSQL), but it was fun learning the difference between each SQL dialects. This is my first time using DISTINCT ON() clause and I find it fascinating since it gave me an opportunity to think and visualize how the query would run in a new way.</p><p>Well, I hope you find this post helpful (or maybe entertaining XD), and I hope you are having a lot of fun learning SQL or doing the challenge like me.</p><p>See ya next time :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9d7edd58a833" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>