<?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 Eliam Abdiel Fonseca Cruz on Medium]]></title>
        <description><![CDATA[Stories by Eliam Abdiel Fonseca Cruz on Medium]]></description>
        <link>https://medium.com/@eliamfc.a?source=rss-013532fb87b0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*9VpwK1dilYZTHkzvOv1-Ug.jpeg</url>
            <title>Stories by Eliam Abdiel Fonseca Cruz on Medium</title>
            <link>https://medium.com/@eliamfc.a?source=rss-013532fb87b0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 14:09:45 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eliamfc.a/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[30 days SQL Challenge/ Day 26]]></title>
            <link>https://medium.com/@eliamfc.a/30-days-sql-challenge-day-26-d3d1fe22519d?source=rss-013532fb87b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3d1fe22519d</guid>
            <category><![CDATA[query]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[datalemur]]></category>
            <category><![CDATA[cte-sql-server]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Eliam Abdiel Fonseca Cruz]]></dc:creator>
            <pubDate>Fri, 09 Jan 2026 02:43:36 GMT</pubDate>
            <atom:updated>2026-01-09T02:43:36.353Z</atom:updated>
            <content:encoded><![CDATA[<p>On day 26 of the 30-day SQL challenge, we will solve a Snapchat SQL interview question from Data Lemur. We need to write a query to break down the time spent sending vs. opening snap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x4TvDYtZP-hbkDXRZAwY0A.png" /></figure><p>Looking deeper into the problem. Is necessary to calculate the specific time depending on the user activity. After we have the total time for each category, it is possible to create the calculations that are requested.</p><p>How to solve the problem? A CTE would help to create the time spent metrics that are requested. Important we are working with data from two source tables: age_breakdown and activities. Means that a join is necessary between the tables. A ‘CASE WHEN’ is the best way to filter the data and create the metrics.</p><p><strong>How the data looks:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*EKPRlOjH3f6KmQ80N5cZUQ.png" /></figure><p><strong>The solution:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QaF_OOFPD8ojnFfgFVmJGw.png" /></figure><p><strong>Result:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pTXou3k1ENkI6wnDKrBTcA.png" /></figure><p><strong>Explanation:</strong></p><p>The first part of the query is the CTE. The aggregate function ‘SUM’ takes the result of the ‘CASE WHEN’ and adds it up. As you can see, I created three ‘CASE WHEN’: send_per, open_perc, and the total_per. That returns the time_spent when the condition is true and 0 when not, ensuring that we only have the right data. <br>Second is the ‘INNER JOIN’, which helps link both tables via the user_id, which is the column that the two tables share in common to connect the user data.<br>Third and last is the final ‘SELECT’ that applies a normal mathematical operation to get each metric. Only multiplying by 100 the result of the division between the send_per or open_per by the time_spent. The ROUND adjusts the output to only show two decimals in the result.</p><p><em>Thanks for reading this post. I hope you learn something new or understand better how CTE work in SQL. This is a gift from me to the world because I have received a lot from the world in this life.</em></p><p>Greetings to all, from Costa Rica, <strong>Abdiel!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3d1fe22519d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[30 days SQL Challenge/ Day 25]]></title>
            <link>https://medium.com/@eliamfc.a/30-days-sql-challenge-day-25-2b85d8b70154?source=rss-013532fb87b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b85d8b70154</guid>
            <category><![CDATA[query]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[window-functions]]></category>
            <category><![CDATA[datalemur]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Eliam Abdiel Fonseca Cruz]]></dc:creator>
            <pubDate>Thu, 08 Jan 2026 00:26:30 GMT</pubDate>
            <atom:updated>2026-01-09T02:06:04.957Z</atom:updated>
            <content:encoded><![CDATA[<p>On day 25 of the 30-day SQL challenge, we will solve an FAANG SQL interview question from Data Lemur. To find the second-highest salary in the company.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*ZJV72bsX2KI87P9E5BAxvQ.png" /></figure><p>Talking about how to solve the problem. The situation is that we are HR data analysts of the company, and the manager is asking us to find the second-highest salary.</p><p>How to solve the problem? In this case, similar to the last SQL challenge, an option is creatyng a query with a <em>‘WINDOW’</em> function. I used three columns: <em>employee_id</em>,<em> salary</em>, and <em>department_id</em>, but the first two are enough. With those columns, applying <em>‘DENSE_RANK’ </em>in SQL, we can create a hierarchy list to see the second-highest.</p><p><strong>How the data looks:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/670/1*izORyM0wx7YQvPuEZCosNg.png" /></figure><p><strong>The solution:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*7z66nFuUVwOnOEMVFPVNcg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*X1XDaUdtNgJdblQAqEZjpQ.png" /></figure><p><strong>My solution:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/639/1*RaW8LvOv-2u-gocF-vyuhg.png" /></figure><p><strong>Result:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*BC2rvFQkQasXa6XZfQwkWA.png" /></figure><p><strong>Explanation:</strong></p><p>In this case, I did two queries because thinking like a real data analyst is better for designing the query in a way that could be easy to recycle and adapt to a similar question in the future.</p><p>First, the query is read in this way. The query starts with the <em>subquery</em> <strong><em>‘sq’</em></strong>, selecting the columns required. This part is important; duplicate salaries can’t be shown in the output result, which is why <strong><em>‘DISTINCT’</em></strong> is used to avoid duplicate results. Then <strong><em>‘DENSE_RANK () OVER (ORDER BY())’ </em></strong>creates a dense hierarchy rank aganist to the salaries. After that, the<strong><em> CTE</em></strong> selects the two columns that will be useful to see the company salaries. Third and last, the <strong><em>CTE </em></strong>selects all the data according to the conditions, filtering in this case the second salary.</p><p>I decided to create a <strong><em>CTE </em></strong>because in a real job situation, this question can be asked differently, like the 8th salary of the company. So it is easier to only change or create a more specific filter, or the data created can be used in another query.</p><p><em>Thanks for reading this post. I hope you learn something new or understand better how WINDONW and CTE work in SQL. This is a gift from me to the world because I have received a lot from the world in this life.</em></p><p>Greetings to all, from Costa Rica, <strong>Abdiel!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b85d8b70154" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[30 days SQL Challenge/ Day 24]]></title>
            <link>https://medium.com/@eliamfc.a/30-days-sql-challenge-day-24-f5384bf7476e?source=rss-013532fb87b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/f5384bf7476e</guid>
            <category><![CDATA[window-functions]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[datalemur]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[query]]></category>
            <dc:creator><![CDATA[Eliam Abdiel Fonseca Cruz]]></dc:creator>
            <pubDate>Tue, 06 Jan 2026 19:27:49 GMT</pubDate>
            <atom:updated>2026-01-07T16:21:29.876Z</atom:updated>
            <content:encoded><![CDATA[<p>Entering the final stretch of this challenge, the questions are thought-provoking. I have completed all the free, easy questions from Data Lemur.</p><p>On day 24 of the 30-day SQL challenge, we will solve an Uber SQL interview question from Data Lemur. To see and track the user’s third transaction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wLPmamBOY1aOvwUsMIhNDA.png" /></figure><p>Furthermore, the problem is more difficult than simply using an aggregate function to count the number of transactions. The real challenge is getting only the third one.</p><p>How to solve the problem? In this case, we need the three columns: user_id, spend, and transaction_date. To get only the third transaction, we need to create a window function. The window function is like a real window that helps calculate something by looking at multiple rows separate into groups (without collapsing). Also, we need to assign a number to each transaction. To do this, we will apply the RANK function.</p><p><strong>How the data looks:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*suhVxRusHnJGC07dEWC6mA.png" /></figure><p><strong>The solution:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/641/1*XBluafUbn3lRfKEtyIOY6w.png" /></figure><p><strong>The result:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/633/1*WyCsjggHuXpMhG3z8uIkKg.png" /></figure><p><strong>Explanation:</strong></p><p>First, the magic happens in the subquery. In this ‘SELECT’ statement, we select all the necessary columns and use the ‘RANK()’ to list the transactions. ‘OVER()’ is telling the query about which rows we need to make the calculation. One of the critical parts is the ‘PARTITION BY’ in this section, where we establish how to group by without collapsing. And we order by transaction_date to have the data in chronological order. The second and final part is the ‘WHERE’ filter; we specify that we only provide the data when the number in the classification list is 3.</p><p><em>Thanks for reading this post. I hope you learn something new or understand better how WINDONW works in SQL. This is a gift from me to the world because I have received a lot from the world in this life.</em></p><p>Greetings to all, from Costa Rica, <strong>Abdiel!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5384bf7476e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[30 days SQL Challenge/ Day 23]]></title>
            <link>https://medium.com/@eliamfc.a/30-days-sql-challenge-day-23-2dc490e39d2e?source=rss-013532fb87b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2dc490e39d2e</guid>
            <category><![CDATA[subquery]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[datalemur]]></category>
            <category><![CDATA[query]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Eliam Abdiel Fonseca Cruz]]></dc:creator>
            <pubDate>Tue, 06 Jan 2026 03:44:13 GMT</pubDate>
            <atom:updated>2026-01-06T03:44:13.807Z</atom:updated>
            <content:encoded><![CDATA[<p>Today is Monday, and now I am close to finishing this challenge. And it’s funny, because I need to think about what they are really asking.</p><p>On day 23 of the 30-day SQL challenge, we will solve a Patient Support Analysis from Data Lemur. To see and track the calls that the UHG policyholders made.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/909/1*cmzBdBbeh5cJiCHRqhXY9w.png" /></figure><p>The problem of today is to count the number of UHG policyholders who made three or more calls. It looks easy, but the trick is that we need to count the policy_holder_id that passes the threshold. So if we apply a where filter, that will count only the policy_holder_id that are greater than or equal to three.</p><p>How to solve the problem? We need two columns:<em> case_id </em>and the <em>policy_holder_id</em>. In this case, we need to create a subquery to calculate the right amount.</p><p><strong>How the data looks:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*x_HDCIMaFxY7Y9Q7hhsqtw.png" /></figure><p><strong>The solution:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*M3cT7z-Ye5FRkWVe5pDWzA.png" /></figure><p><strong>The result:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*N4UKAG1El6d9UCvBP5XbZg.png" /></figure><p><strong>Explanation:</strong></p><p>The first part of the subquery is the ‘SELECT HAVING’. In this part, we first calculate, via the aggregate function ‘COUNT’, the total number of calls, grouping the result by the policy_holder_id. Second, the ‘HAVING’ filter brings us to create an aggregate filter, such as we need in this case, to only count the UHG policyholders who have more than three calls. Third and last, we count the number of policy_holder_id that pass the threshold. In conclusion, is necessary to count the case_id and group by the policy_holder_id, because the case_id is the unique record for each call that was made. And the policy_holder_id is the id number assigned to each UHG policyholder.</p><p><em>Thanks for reading this post. I hope you learn something new or understand better how subqueries work in SQL. This is a gift from me to the world because I have received a lot from the world in this life.</em></p><p>Greetings to all, from Costa Rica, <strong>Abdiel!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2dc490e39d2e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[30 days SQL Challenge/ Day 22]]></title>
            <link>https://medium.com/@eliamfc.a/30-days-sql-challenge-day-22-121769beabd6?source=rss-013532fb87b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/121769beabd6</guid>
            <category><![CDATA[datalemur]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[query]]></category>
            <category><![CDATA[pharmacy]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Eliam Abdiel Fonseca Cruz]]></dc:creator>
            <pubDate>Mon, 05 Jan 2026 04:32:39 GMT</pubDate>
            <atom:updated>2026-01-05T04:32:39.390Z</atom:updated>
            <content:encoded><![CDATA[<p>After a turbulent year, I am returning to the challenge that I left behind because I got a job. To be honest, at that moment, I didn’t feel like finishing the challenge.</p><p><em>But look at me, here were going again</em>.</p><p>On day 22 of the 30-day SQL challenge, we will solve a Pharmacy Analytics sales calculation from Data Lemur. This one has a special format request like a real job task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/504/1*OvbNPkyRKgXmpuUcGuOVcA.png" /></figure><p>Today’s challenge is create a query to calculate the total sales of each manufacturer, and sort them by manufacturer name to avoid duplicate manufacturer names. In addition to the aggregate calculation functions, the ‘<strong><em>CONCAT’</em></strong> function is necessary to aggregate the string part for the output result format.</p><p>How to solve the problem? We need two columns:<em> manufacturer </em>and <em>total_sales</em>. Applying the <strong><em>‘SUM’ </em></strong>aggregate function, we can get the total sales. And use <strong><em>‘ROUND’ </em></strong>with a simple divide logic to get the <em>total_sales</em>.</p><p><strong>How the data looks:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*yESEQxvSm-PV0nXglXOJgw.png" /></figure><p><strong>The solution:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/947/1*eRFV2eZJlzSTs8CGdwwzoA.png" /></figure><p><strong>The result:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*q8xP2T6lUED5nrgRIWZFlA.png" /></figure><p><strong>Explanation:</strong></p><p>First, the query sums the total_sales and divides it by 1000000. Then the result is rounded to the nearest million, thanks to the <strong><em>‘ROUND’</em></strong> function. Second, the <strong><em>‘CONCAT’ </em></strong>function makes the magic, inserting the text part into the output result of each sale. If you pay attention to the query could see that it is using ‘<strong><em>,</em></strong>’ and is used to make the separator between the string and the calculation. Third and last, the <strong><em>‘GROUP BY’ </em></strong>function is necessary because we are working with aggregation functions, and the query needs it to run properly and group the results by each manufacturer. Then the <strong><em>‘ORDER BY’</em></strong> function sorts the result by sales and then by manufacturer name in alphabetical order.</p><p><em>Thanks for reading this post. I hope you learn something new or understand better how mathematical calculation functions and string functions work in SQL. This is a gift from me to the world because I have received a lot from the world in this life.</em></p><p>Greetings to all, from Costa Rica, <strong>Abdiel!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=121769beabd6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[30 days SQL Challenge/ Day 21]]></title>
            <link>https://medium.com/@eliamfc.a/30-days-sql-challenge-day-21-cbcb0bd4823c?source=rss-013532fb87b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbcb0bd4823c</guid>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[alibaba]]></category>
            <category><![CDATA[datalemur]]></category>
            <category><![CDATA[query]]></category>
            <dc:creator><![CDATA[Eliam Abdiel Fonseca Cruz]]></dc:creator>
            <pubDate>Mon, 06 Jan 2025 01:34:00 GMT</pubDate>
            <atom:updated>2026-01-05T04:25:29.888Z</atom:updated>
            <content:encoded><![CDATA[<p>On day 21 of the 30-day SQL challenge, we will solve a simple mean calculation of Alibaba’s order items from Data Lemur.</p><p>Today’s issue concerns the items ordered from Alibaba. We need to find the mean of items per order. To solve this, it is necessary to use the <strong><em>‘SUM’</em></strong> <em>aggregate function</em> and calculate an easy mathematical function to find the mean.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/449/1*1PIUxuMB6m2uBd-69Tb3Nw.png" /></figure><p>How to solve the problem? To solve this problem are necessary the two columns from the <em>items_per_order</em> table: <em>item_count</em> and <em>order_occurrences</em> to calculate the mean. Also is necessary the <strong><em>‘SUM’</em></strong> <em>aggregate function </em>to obtain the total of those columns and the ‘ROUND’ function to return the result to one decimal place.</p><p><strong>How the data looks:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/901/1*sD9GSExXL2BJPiTyy433pA.png" /></figure><p><strong>The solution:</strong></p><pre>SELECT ROUND(CAST(SUM(item_count * order_occurrences) AS numeric) / SUM(order_occurrences), 1) AS items_per_order_mean<br>FROM order_occurences;</pre><p><strong>The result:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*PJivJMN_U0muVGzxsAssNA.png" /></figure><p><strong>Explanation:</strong></p><p>First, the query adds up the columns <em>item_count</em> and <em>order_occurrences</em> before multiplying them to obtain the total items, this is because to get the total items is necessary to multiply by each <em>item_count</em> for the <em>order_occurences</em>. Second, the query via the <strong><em>‘CAST’ </em></strong><em>conversion type function</em> ensures that the result is numeric and handy for the <strong><em>‘ROUND’</em></strong> function because SQL doesn’t accept <strong>double precision types </strong>to round them off. Third, the query adds up the <em>order_occurrences </em>column to obtain the total for the mean, and after the adds up the query divides the total_items between the total orders to calculate the mean result. Finally, the query rounds up the result via the <strong><em>‘ROUND’ </em></strong>function to one decimal place.</p><p><em>Thanks for reading this post. I hope you learn something new or understand better how mathematical calculation functions work in SQL. This is a gift from me to the world because I have received a lot from the world in this life.</em></p><p>Greetings to all, from Costa Rica, <strong>Abdiel!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbcb0bd4823c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[30 days SQL Challenge / Day 20]]></title>
            <link>https://medium.com/@eliamfc.a/30-days-sql-challenge-day-20-3649603df639?source=rss-013532fb87b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3649603df639</guid>
            <category><![CDATA[datalemur]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[query]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[jp-morgan]]></category>
            <dc:creator><![CDATA[Eliam Abdiel Fonseca Cruz]]></dc:creator>
            <pubDate>Sun, 05 Jan 2025 03:09:11 GMT</pubDate>
            <atom:updated>2025-01-05T03:09:11.666Z</atom:updated>
            <content:encoded><![CDATA[<p>On day 20 of the SQL challenge, we will solve a math calculation problem from DataLemur related to JPMorgan credit card problems.</p><p>Today’s problem is identifying the difference between the month with the highest and lowest number of issued cards. The problem’s statement is to create a calculation difference of the issued cards by each card_name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*MEL1Y5o2mD300XZ2qUJciA.png" /></figure><p><strong>How to solve the problem?</strong> To be honest. On day 20 of the 30-day SQL challenge, the corresponding problem is easy. We only need two columns from the <em>monthly_card_issued </em>table: <em>card_name </em>and <em>issued_amount.</em> Two essential aggregate functions to solve the problem are the<strong><em> ‘MAX’ </em></strong>to calculate the highest value, and the <strong><em>‘MIN’ </em></strong>to calculate the lowest value of the issued cards per card.</p><p><strong>The solution:</strong></p><pre>SELECT card_name, MAX(issued_amount) - MIN(issued_amount) AS difference<br>FROM monthly_cards_issued<br>GROUP BY card_name<br>ORDER BY difference DESC;</pre><p><strong>The result:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/394/1*RRSiKRUrRoDoT3_oXRnxKA.png" /></figure><p><strong>Extra analysis:</strong></p><pre>SELECT card_name, MAX(issued_amount) AS Highest_issued_card, MIN(issued_amount) AS Lowest_issued_card,MAX(issued_amount) - MIN(issued_amount) AS difference<br>FROM monthly_cards_issued<br>GROUP BY card_name<br>ORDER BY difference DESC;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/593/1*i1yvSrSjp7t0tJntf-FwMw.png" /></figure><p><strong>Explanation:</strong></p><p>As you can see in the solution query, I am using the <strong><em>‘MAX’ </em></strong><em>aggregate calculation</em> <em>function</em> to calculate the highest amount of issued cards, and the <strong><em>‘MIN’</em></strong> <em>aggregate calculation</em> <em>function</em> to calculate the lowest amount of issued cards. After that the <em>‘-’</em> subtract symbol is the mathematical operator that calculates the range. The most important part is the <strong><em>‘GROUP BY’</em></strong> <em>grouping function</em> which allows the calculation of the <em>maximum</em> and the <em>minimum </em>of the issued cards by cards. Remember that because I am using a column without an aggregate calculate function is necessary to use the <strong><em>‘GROUP BY’</em></strong> <em>grouping function</em> to SQL understand in a base which parameter needs to be calculated.</p><p>Thinking as data analysis it is important to explain or share the information in a simple and accessible way for everyone. So the second query of the <strong><em>extra analysis </em></strong>is effective in showing the difference between the issued cards. Because shows the values of the range calculation before obtaining the difference.</p><p><em>Thanks for reading this post, I hope you learn something new or understand better how aggregate calculation functions work in SQL. This is a gift from me to the world because I have received a lot from the world in this life.</em></p><p>Greetings to all, from Costa Rica <strong>Abdiel!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3649603df639" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[30 days SQL Challenge / Day 19]]></title>
            <link>https://medium.com/@eliamfc.a/30-days-sql-challenge-day-19-9cb8fe056d1e?source=rss-013532fb87b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/9cb8fe056d1e</guid>
            <category><![CDATA[query]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[datalemur]]></category>
            <category><![CDATA[subquery]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Eliam Abdiel Fonseca Cruz]]></dc:creator>
            <pubDate>Sat, 04 Jan 2025 05:52:54 GMT</pubDate>
            <atom:updated>2025-01-04T05:54:22.985Z</atom:updated>
            <content:encoded><![CDATA[<p>On day 19 of the SQL challenge, we will solve a <strong><em>‘Subquery’ </em></strong>calculation problem from DataLemur related to IBM problems.</p><p>Today’s problem is related to understanding how IBM’s employees are utilizing the Db2 by tracking the SQL queries executed by their employees. The problem’s statement is to create a histogram that shows the number of unique queries run by employees during the third quarter of 2023.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/685/1*V_cRWitr1C2z9bjgk3fBmg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*SfXC6ysSvzwAv8JtFcxBXQ.png" /></figure><p><strong>How to solve the problem?</strong> This can be solved through two methods, the first is using a <strong><em>‘subquery’</em></strong>, and the second is via a <strong><em>‘CTE’.</em></strong> Is necessary three columns: <em>employe_id</em>, <em>query_id</em>, and <em>query_starttime</em>. See also, is necessary to use the <strong><em>‘COUNT’ </em></strong><em>aggregate function</em>, the <strong><em>‘LEFT JOIN’ </em></strong>or <strong><em>‘RIGHT JOIN’</em></strong> to merge the tables, and use the <strong><em>‘GROUP BY’</em></strong> <em>grouping function</em>. Also, use the <strong><em>‘AND’</em></strong> function to filtrate the data that will be merged for the third quarter of 2023.</p><p><strong>The solution:</strong></p><pre>SELECT query_count AS unique_queries, COUNT(employee_count) AS employee_count<br>FROM (<br>    SELECT e.employee_id AS employee_count, <br>           COALESCE(COUNT(DISTINCT q.query_id), 0) AS query_count<br>    FROM employees AS e<br>    LEFT JOIN queries AS q ON e.employee_id = q.employee_id<br>    AND q.query_starttime BETWEEN &#39;2023-07-01&#39; AND &#39;2023-09-30&#39;<br>    GROUP BY e.employee_id<br>) AS queries_count<br>GROUP BY unique_queries<br>ORDER BY unique_queries;</pre><p><strong>The result:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*cfghNn1dCOIzvPYD_Jwvag.png" /></figure><p><strong>Explanation:</strong></p><p>First, the query makes the merges between the employees and queries tables via <strong><em>‘LEFT JOIN’</em></strong> because this case is necessary even in the case when the employees didn’t use a query, and the <strong><em>‘LEFT JOIN’ </em></strong>matches the first table with the second deepened of the clause that we establish, in this case when the <em>employee_id</em> of the <em>employees</em> table is equal to the <em>employee_id</em> of the <em>queries</em> table and when didn´t match return null. Then the query filter for the <em>query_starrtime</em> column only returns the<em> employees_id</em> that are from the third quarter of 2023. After that, the query starts with the first <strong><em>‘SELECT’</em></strong> <em>project clause</em>, which returns the <em>employee_id</em> and uses the <strong><em>‘COUNT’</em></strong> <em>aggregate function</em> to count the<strong><em> ‘DISTINCT’ </em></strong>query_id, and the <strong><em>‘COALECSE’</em></strong> function fixes the results when there are null for the <strong><em>‘LEFT JOIN’</em></strong>. Lastly the subquery groups via the <strong><em>‘GROUP BY’</em></strong> function for the <em>employee_id</em>.</p><p>Second, the first <strong><em>‘SELECT’</em></strong><em> project function</em> starts with the magic because selecting the<em> query_count </em>gives an alias via the<strong><em> ‘AS’</em></strong> function, and counts the <em>employee_id </em>to have what we need, that is the count of employees that use queries. But the important part is the <strong><em>‘GROUP BY’</em></strong> <em>grouping function</em> that makes the magic because groups the results for the <em>unique_queries </em>so that way the count will be through this column. Last but not least, the query sorts the result using the ‘ORDER BY’ function by <em>unique_queries</em>.</p><p><em>Thanks for reading this post, I hope you learn something new or understand better how subqueries work in SQL. This is a gift from me to the world because I have received a lot from the world in this life.</em></p><p>Greetings to all, from Costa Rica <strong>Abdiel!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cb8fe056d1e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[30 days SQL Challenge / Day 18]]></title>
            <link>https://medium.com/@eliamfc.a/30-days-sql-challenge-day-18-0f2f26e38f6f?source=rss-013532fb87b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/0f2f26e38f6f</guid>
            <category><![CDATA[query]]></category>
            <category><![CDATA[datalemur]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[ti̇ktok]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Eliam Abdiel Fonseca Cruz]]></dc:creator>
            <pubDate>Fri, 03 Jan 2025 08:15:49 GMT</pubDate>
            <atom:updated>2025-01-03T08:20:40.434Z</atom:updated>
            <content:encoded><![CDATA[<p>On day 18 of the SQL challenge, we will solve a <strong><em>‘WHERE’ </em></strong>and <strong><em>‘DATE’ </em></strong>calculation problem from DataLemur related to TikTok problems.</p><p>Today’s problems are related to the TikTok app. We need to find the users who confirm their account via text link on the second day after creating their account. The tricky part is calculating the second day after the account’s creation. To archive this, I used the <strong><em>‘INTERVAL’ </em></strong>date<strong><em> </em></strong>calculation function</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*ZgTTiInFn5EKHGoYZe0Zsw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/431/1*pscPlfKTiDBBoNyxcoKzhg.png" /></figure><p><strong>How to solve the problem?</strong> To solve the problem we need the tables<em> texts</em> and <em>emails</em>, and from those table five columnsare necessary: <em>user_id</em>, <em>email_id</em>, a<em>ction_date,</em> <em>signup_date</em> and <em>signup_action</em>. First, is necessary to merge both tables to obtain the columns that match between both tables. Second, aply the filters conditions to obtain the data according to our needs.</p><p><strong>The solution:</strong></p><pre>SELECT e.user_id<br>FROM emails AS e<br>INNER JOIN texts AS t ON e.email_id = t.email_id<br>WHERE DATE(t.action_date) = DATE(e.signup_date + INTERVAL &#39;1 DAY&#39;)<br>AND t.signup_action = &#39;Confirmed&#39;;</pre><p><strong>The result:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/446/1*0AFTGSO0d-x6T_lDnCHVow.png" /></figure><p><strong>Explanation:</strong></p><p>First, the query filters the data based on when the action_date is equal to the<em> signup_date</em> + <strong><em>‘INTERVAL ‘1 DAY’’</em></strong>, the <strong><em>’INTERVAL’ </em></strong><em>date calculation function </em>is the operator function that allows the addition or subtraction time periods to a<em> date </em>or <em>timestamp</em>. In this case, allowing to add one day to the <em>signup_date, </em>thus creating a filter for users who confirmed their account one day after created the account. Secondly, the query filters the data when the <em>signup_action</em> is <em>confirmed</em>. Third, the query performs the merge using the &#39;<strong><em>INNER JOIN</em></strong>&#39; <em>merge functio</em>n when the emails table&#39;s <em>email_id</em> matches the texts table&#39;s id. Finally, the <strong><em>‘SELECT’ </em></strong><em>consult projection</em> function returns the <em>user_id from </em>the emails table when all the aforementioned conditions are met.</p><p><em>Thanks for reading this post, I hope you learn something new or understand better how the ‘INTERVAL’ date calculation function works in SQL. This is a gift from me to the world because I have received a lot from the world in this life.</em></p><p>Greetings to all, from Costa Rica <strong>Abdiel!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0f2f26e38f6f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[30 days SQL Challenge / Day 17]]></title>
            <link>https://medium.com/@eliamfc.a/30-days-sql-challenge-day-17-abd1aa7c78f2?source=rss-013532fb87b0------2</link>
            <guid isPermaLink="false">https://medium.com/p/abd1aa7c78f2</guid>
            <category><![CDATA[facebook]]></category>
            <category><![CDATA[query]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[datalemur]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Eliam Abdiel Fonseca Cruz]]></dc:creator>
            <pubDate>Tue, 31 Dec 2024 03:29:54 GMT</pubDate>
            <atom:updated>2024-12-31T03:29:54.247Z</atom:updated>
            <content:encoded><![CDATA[<p>On day 17 of the SQL challenge, we will solve a <strong><em>‘CASE’ </em></strong>calculation problem from DataLemur related to Facebook problems.</p><p>Today’s problem is related to Facebook app analytics, we have to find the click-through rate (CTR) for the year 2022. This is a little bit tricky if think that using only the <strong><em>‘COUNT’ </em></strong>aggregate function will solve the problem, in this case, we have to count two aspects of the even_type column: click and impression. To solve the problem the best method is to create a logic case to assign a 1 when some of these two scenarios happened.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*IxQaydmLBz3524sIBrPCLA.png" /></figure><p><strong>How to solve the problem? </strong>To solve this problem we need the three columns from the events table: <em>app_id</em>, <em>event_typ</em>e, and <em>timestamp</em>. On the other hand, to count the number of clicks and impressions, we have to create a <strong><em>‘CASE’ </em></strong>logic function to return a 1 when one of these cases is true, so that way we can add up and divide by each other. Also, we need to filter the data to only get the data for 2022.</p><p><strong>The solution:</strong></p><pre>SELECT app_id, ROUND(100.0*SUM(CASE WHEN event_type = &#39;click&#39; THEN 1 ELSE 0 END) / SUM(CASE WHEN event_type = &#39;impression&#39; THEN 1 ELSE 0 END),2) AS ctr<br>FROM events<br>WHERE EXTRACT(YEAR FROM timestamp) = 2022<br>GROUP BY app_id;</pre><p><strong>The result:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*3AbIH1HttebcrGZh4ela7g.png" /></figure><p><strong>Explanation:</strong></p><p>First, the query filters the data from the table events via the <strong><em>‘WHERE’</em></strong> filter clause to obtain only the data from 2022 using the ‘EXTRACT(YEAR FROM)’ time clause. The second, and most important part is the <strong><em>‘SELECT’ </em></strong><em>project clause</em>, after selecting the <em>app_id</em>, the mathematical function is processed via the <strong><em>‘CASE’ </em></strong><em>logical expression</em> inside the <strong><em>‘SUM’</em></strong> <em>aggregate function</em>. The <strong><em>‘CASE’</em></strong><em> logical expression</em> returns a value of 1 when the <em>even_type</em> is true in other cases returns a value of 0. Last, but not least the <strong><em>‘GROUP BY’ </em></strong>grouping function takes the results and groups by <em>app_id,</em> and this is important to use because I am using an aggregate function such as <strong><em>‘SUM’</em></strong>, and this is necessary or SQL doesn&#39;t run the query because the <strong><em>‘SELECT’</em></strong> <em>projection clause</em> contains a not <em>aggregate function</em> that is the <em>app_id</em>.</p><p>Clarifying better the mathematical function, we need to dive the number of clicks between the number of prints to multiply the result by 100 to get the rate of the<strong><em> CTR</em></strong>, that&#39;s why I specify in the first <strong><em>‘CASE</em></strong>’ <em>logic clause</em> that only returns a 1 when the event_type is equal to <em>click</em> and 0 for others because I only want to count when is this case, and vice versa for the second <strong><em>´CASE´</em></strong> <em>logical expression</em> for when <em>event_type</em> is equal to <em>print</em>. The<strong><em> ‘ROUND’</em></strong> numeric manipulation function is used to round the number to 2 decimals place.</p><p><em>Thanks for reading this post, I hope you learn something new or understand better how ‘CASE’ logical expression works in SQL. This is a gift from me to the world because I have received a lot from the world in this life.</em></p><p>Greetings to all, from Costa Rica <strong>Abdiel!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=abd1aa7c78f2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>