<?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 Faisal Yudha S on Medium]]></title>
        <description><![CDATA[Stories by Faisal Yudha S on Medium]]></description>
        <link>https://medium.com/@faisalys?source=rss-c705f80ea71f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*R4ISyu5pYK28AnYhzYhaZA.jpeg</url>
            <title>Stories by Faisal Yudha S on Medium</title>
            <link>https://medium.com/@faisalys?source=rss-c705f80ea71f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 05:12:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@faisalys/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[Data Analysis MySkill: SQL Study Case]]></title>
            <link>https://faisalys.medium.com/data-analysis-myskill-sql-study-case-cde50de33cf0?source=rss-c705f80ea71f------2</link>
            <guid isPermaLink="false">https://medium.com/p/cde50de33cf0</guid>
            <category><![CDATA[postgresql]]></category>
            <category><![CDATA[data-analysis]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[pgadmin-4]]></category>
            <category><![CDATA[data-analyst]]></category>
            <dc:creator><![CDATA[Faisal Yudha S]]></dc:creator>
            <pubDate>Tue, 23 May 2023 09:22:49 GMT</pubDate>
            <atom:updated>2023-05-24T07:26:18.813Z</atom:updated>
            <content:encoded><![CDATA[<p>The story begins.<br>Hello, this is my first SQL study case; I’m still studying data analysis. In my opinion, the first skill that must be mastered by a data analyst is SQL. In this SQL study case, we will discuss 10 problems. There may be errors or mistakes in this post, but I still try to share it with you guys. In the process, I use pgAdmin4, which is a management tool for PostgreSQL. Let’s start!</p><p>First, open pgAdmin4, then enter the master password that was previously specified during the pgAdmin4 installation process.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*qBGEqL25JL8bckxsoQs-mA.png" /></figure><p>Next, re-enter the password to be able to connect to the PostgreSQL 15 server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*NAgd2VlmFtvf5-lqsjNUcw.png" /></figure><p>Next, open the database and select “Query Tool”. In the Query tool, this is where we enter the query code and run it (you can press the run icon or the F5 shortcut key).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/130/1*3yU9JTjBdcWNk0jNR7Id8A.png" /><figcaption>Select “Query Tool”</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/437/1*xH3DmToE9iCDb7cWUBTq6w.png" /><figcaption>Enter The Query Code Here</figcaption></figure><p>The following datasets are used:</p><ol><li><a href="https://drive.google.com/file/d/19t5qryTODPM5p9rFD7txBlpYrO1IAOsa/view?usp=sharing"><em>order_detail</em></a></li><li><a href="https://drive.google.com/file/d/1eySaFhdEO0AgscyOk153amR1QiMEQ6Vh/view?usp=sharing"><em>sku_detail</em></a></li><li><a href="https://drive.google.com/file/d/1qCbfWVPfdCZy8y928fngP3g4fUBjouYb/view?usp=sharing"><em>customer_detail</em></a></li><li><a href="https://drive.google.com/file/d/194st8Id3s9VmUP4QsivqD3t9YTUlwzx2/view?usp=sharing"><em>payment_detail</em></a></li></ol><p>The data used is from Kaggle: <a href="https://www.kaggle.com/datasets/zusmani/pakistans-largest-ecommerce-dataset"><em>Pakistan’s Largest E-Commerce Dataset</em></a>, with some changes to make it easier to practice using SQL. The price listed has been converted to 1 rupee, which is equal to Rp. 58. Regarding the explanation of the dataset, it is as follows:</p><p><strong>order_detail</strong>:</p><ol><li>id → unique number of order / id_order</li><li>customer_id → unique number of the customer</li><li>order_date → the date when the transaction was made</li><li>sku_id → unique number of the product (sku is the stock keeping unit)</li><li>price → the price listed on the price tag</li><li>qty_ordered → the number of items purchased by the customer</li><li>before_discount → total price value of the product (price * qty_ordered)</li><li>discount_amount → total product discount value</li><li>after_discount → the value of the total price of the product when it has been deducted by the discount</li><li>is_gross → indicates the customer has not paid for the order</li><li>is_valid → indicates the customer has made a payment</li><li>is_net → indicates the transaction has been completed</li><li>payment_id → the unique number of the payment method</li></ol><p><strong>sku_detail:</strong></p><ol><li>id → a unique number of the product (which can be used as a key when joining)</li><li>sku_name → the name of the product</li><li>base_price → the price of the item listed on the price tag</li><li>cogs → cost of goods sold / total cost to sell 1 product</li><li>category → product category</li></ol><p><strong>customer_detail:</strong></p><ol><li>id → a unique number from the customer</li><li>registered_date → the date the customer started to register as a member</li></ol><p><strong>payment_detail:</strong></p><ol><li>id → the unique number of the payment method</li><li>payment_method → payment method used</li></ol><p>After downloading the file with the .txt extension from the dataset list listed above, please open the .txt file, copy all of its contents, then paste it on the Query Tool page, then run. Furthermore, the process of working on the SQL Study Case can be continued from here, GO GO GO!</p><p><strong>Question</strong><br><strong>Number 1</strong><br>Q: During the transactions that took place in 2021, in which month was the total transaction value (after_discount) the largest? Use <strong>is_valid = 1</strong> to filter transaction data.<br>Source table: <em>order_detail</em></p><p>Solution:</p><ol><li>First of all, to determine the month name, you need to refer to the order_date column, but because the column is in the yyyy-mm-dd format, to generate month names (ex: January, February, etc.,) you need to use to_char and set it to ‘Month’ .</li><li>Then, to produce the total transaction value, it is necessary to add up the after_discount column. The two columns that will be displayed are in the order_detail table.</li><li>In this case, given the condition that transactions are limited to only 2021, it is necessary to extract the year in the order_date column to select the year 2021, in addition to using the condition column is_valid = 1, which means the customer has made a payment.</li><li>Furthermore, because there is aggregation in select, it is necessary to group by 1, which refers to the first column to be displayed in the output, the ‘bulan’ column.</li><li>To get the largest total transaction value, it needs to be sorted in descending order based on the total_value_transaction column.</li></ol><p>The query will look like below:</p><pre>select <br> to_char(order_date, &#39;Month&#39;) as bulan, <br> sum(after_discount) total_nilai_transaksi<br>from <br> order_detail<br>where <br> extract(year from order_date) = 2021 and is_valid = 1 <br>group by 1<br>order by 2 desc<br>limit 3</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*9yCw6ulAlz5mco0pPH9bNg.png" /></figure><p>Explanation:<br>Actually, to get the answer, you only need a limit of 1, but as a habit, so that you can compare the values well, I use a limit of 3. Based on the displayed results, transactions that occurred during 2021, it turns out that the largest total transaction value (after_discount) was in <strong>November 2021</strong>.</p><p><strong>Number 2</strong><br>Q: During the transactions that took place in 2021, in which month was total number of customers (unique), total orders (unique), and the highest total number of product quantities? Use <strong>is_valid = 1</strong> to filter transaction transaction data.<br>Source table: <em>order_detail</em></p><p>Solution:</p><ol><li>Same as above, to determine the month name, you need to refer to the order_date column, but because the column is in the yyyy-mm-dd format, to generate month names (Ex: January, February, etc.,) you need to use to_char and set it to ‘Month’ .</li><li>Then, to get the total number of customers who are unique, do distinct on the customer_id column, then count the number with count.</li><li>Next, to get the total number of unique orders, do distinct on the id column, then use count again to calculate the amount.</li><li>To get the total product quantity, sum the qty_ordered column.</li><li>The selected columns are in the order_detail table.</li><li>In this problem, given the condition that transactions are limited to only 2021, it is necessary to extract the year in the order_date column to select the year 2021, in addition to using the condition column is_valid = 1, which means the customer has made a payment.</li><li>Because there is aggregation, it requires group by; in this question, group by 1 refers to the ‘bulan’ column.</li><li>To get the largest value, use ‘order by column total_customers’ in descending order.</li></ol><p>The query will look like below:</p><pre>select<br> to_char(order_date, &#39;Month&#39;) as bulan,<br> count(DISTINCT customer_id) as total_pelanggan,<br> count(DISTINCT id) as total_pesanan,<br> sum(qty_ordered) as total_kuantitas<br>from <br> order_detail<br>where <br> extract(year from order_date) = 2021 and is_valid = 1<br>group by 1<br>order by 2 desc<br>limit 3;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*8i0cwYrrIlwxJCTsNJyA_g.png" /></figure><p>Explanation:<br>Based on the displayed output, the transactions that occurred during 2021, it turns out that the total number of customers (unique), total orders (unique), and the highest total number of product quantities was in <strong>November 2021</strong>.</p><p><strong>Number 3</strong><br>Q: During the transactions that took place in 2022, which category generated the greatest transaction value? Use <strong>is_valid = 1</strong> to filter transaction data. <br>Source table: <em>order_detail, sku_detail</em></p><p>Solution:</p><ol><li>First select the category column, then sum the after_discount column to get the total transaction value.</li><li>It should be noted that this question uses two tables, so it is necessary to do a join. The sku_detail table is aliased with s, and the order_detail table is aliased with o. The join used is the left join, or simply by typing “join” only. To perform a join, it is necessary to have the same values in both tables; these values are in the sku_id column in the order_detail table and the id column in the sku_detail table.</li><li>In this problem, given the condition that transactions are limited to only 2022, it is necessary to extract the year in the order_date column to select the year 2022. Besides that, the condition in the column is_valid = 1 is used, which means the customer has made a payment.</li><li>Because there is aggregation, it requires group by; in this question, group by 1 refers to the category column.</li><li>To get the largest transaction value, sort by column 2 in descending order, namely column “total_nilai_transaksi”.</li></ol><p>The query will look like below:</p><pre>select<br> category,<br> sum (after_discount) total_nilai_transaksi <br>from sku_detail as s<br>join order_detail as o on o.sku_id = s.id<br>where extract(year from order_date) = 2022 and is_valid = 1<br>group by 1<br>order by 2 Desc;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*MDwHsEyr2jv-mUQJbXsTjw.png" /></figure><p>Explanation:<br>Based on the displayed output, transactions that occurred during 2022, it turns out that the category that generates the most transaction value is <strong>Mobiles &amp; Tablets</strong>.</p><p><strong>Number 4</strong><br>Q: Compare the transaction value of each category in 2021 and 2022. Please state which categories experienced an increase and which categories experienced a decrease in transaction value from 2021 to 2022. Use <strong>is_valid = 1</strong> to filter transaction data. <br>Source table: <em>order_detail, sku_detail</em></p><p>Solution:</p><ol><li>First, select the category column. Because this question compares transaction values between 2021 and 2022, you can use the case when to get results in the form of new columns classified in 2021 and 2022 separately. Then each of them is added up to get the total transaction value.</li><li>In this question, we will use join again, with the sku_detail table aliased with s and the order_detail table aliased with o. These values are similar in the sku_id column in the order_detail table and the id column in the sku_detail table.</li><li>The condition used is the column is_valid = 1, which means the customer has made a payment.</li><li>Then, because there is aggregation, group by 1, which refers to the category column.</li><li>To get the largest transaction value, sort by column 2 in descending order, namely column “total_sales_2021”.</li></ol><p>The query will look like below:</p><pre>select<br> category,<br> sum(case when extract(year from order_date) = 2021 then after_discount end) total_sales_2021,<br> sum(case when extract(year from order_date) = 2022 then after_discount end) total_sales_2022<br>from sku_detail as s<br>join order_detail as o on o.sku_id = s.id <br>where is_valid = 1<br>group by 1<br>order by 2 Desc;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*8efON0cY2FSVJU-h9_a-kQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*AiKb3gBS0lxG2HSqA4LKsA.png" /></figure><p>Explanation:<br>Based on the displayed output, categories that have experienced an increase include: <strong>Mobiles &amp; Tablets, Appliances, Entertainment, Women Fashion, Computing, Beauty &amp; Grooming, Home &amp; Living, Superstore, Health &amp; Sports, Kids &amp; Baby, and School &amp; Education</strong>.</p><p>While the categories that experienced a decrease included: <strong>Men Fashion, Soghaat, Others, </strong>and <strong>Books.</strong></p><p><strong>Number 5</strong><br>Q: Display the Top 10 sku_name (along with their categories) based on the value of transactions that occurred during 2022. Also display the total number of customers (unique), total orders (unique), and total amount of quantity. Use <strong>is_valid = 1</strong> to filter transaction data. <br>Source table: <em>order_detail, sku_detail</em></p><p>Solution:</p><ol><li>First select the column sku_name, category, then sum after_discount to get the total transaction value.</li><li>Because this question is ordered to display the total number of customers uniquely, use distinct in the customer_id column, then count to get the total number of customers.</li><li>This question is also ordered to display the total order uniquely, so use distinct in the id column contained in the sku_detail table, then count to get the total number of orders.</li><li>Sum the qty_ordered column to get the total quantity.</li><li>In this question, it is necessary to do a join table; the order_detail table is aliased with o, and the sku_detail table is aliased with s. These values are in the sku_id column in the order_detail table and the id column in the sku_detail table.</li><li>In this problem, given the condition that transactions are limited to only 2022, it is necessary to extract the year in the order_date column to select the year 2022, in addition to using the condition column is_valid = 1, which means the customer has made a payment.</li><li>Group by columns 1 (sku_name) and 2 (category). Sorted descending based on the 3rd column, “total_nilai_transaksi”.</li><li>Because this question is asked to display the Top 10, use the limit of 10.</li></ol><p>The query will look like below:</p><pre>select<br> sku_name,<br> category,<br> sum(after_discount) as total_nilai_transaksi,<br> count(DISTINCT customer_id) as total_pelanggan,<br> count(DISTINCT s.id) as total_pesanan,<br> sum(qty_ordered) as total_kuantitas<br>from order_detail as o<br>join sku_detail s on o.sku_id = s.id<br>where extract(year from order_date) = 2022 and is_valid = 1<br>group by 1,2 <br>order by 3 desc <br>limit 10</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*1Q11id2-jexCcJFGoO0_wA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/1*dRvvs1maLJrXZVOa2x-Lxg.png" /></figure><p><strong>Number 6</strong><br>Q: Show the top 5 most popular payment methods used in 2022 (based on total unique orders). Use <strong>is_valid = 1</strong> to filter transaction data. <br>Source table: <em>order_detail, payment_detail</em></p><p>Solution:</p><ol><li>First, select payment_method to display the payment_method column.</li><li>Distinct the id column in the order_detail table, then count to display the number of unique orders.</li><li>This problem uses two tables, so you need to join the order_detail table aliased with o and the payment_detail table aliased with “p”. This value similarity is found in the payment_id column in the order_detail table and the id column in the payment_detail table.</li><li>In this problem, given the condition that transactions are limited to only 2022, it is necessary to extract the year in the order_date column to select the year 2022, in addition to using the condition column is_valid = 1, which means the customer has made a payment.</li><li>Group by “payment_method”. Sorted descending based on column total_order.</li><li>Because this question is asked to display the top 5, use limit 5.</li></ol><p>The query will look like below:</p><pre>select <br> payment_method,<br> count(DISTINCT o.id) total_order<br>from order_detail o<br>join payment_detail &quot;p&quot; on o.payment_id = &quot;p&quot;.id<br>where extract(year from order_date) = 2022 and is_valid = 1<br>group by 1 <br>order by 2 desc<br>limit 5;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*Sen9w1u5__HOtsvKIECi2A.png" /></figure><p>Explanation:<br>Based on the output displayed, the top 5 most popular payment methods used in 2022 are: <strong>cod, Payaxis, Easypay, customercredit, </strong>and <strong>Jazzwallet</strong>.</p><p><strong>Number 7</strong><br>Q: Sort these 5 products based on their transaction value.</p><ol><li>Samsung</li><li>Apple</li><li>Sony</li><li>Huawei</li><li>Lenovo</li></ol><p>Use <strong>is_valid = 1</strong> to filter transaction data.<br>Source table: <em>order_detail, sku_detail</em></p><p>Solution:</p><ol><li>In this problem, it is necessary to use a CTE (Common Table Expression). Let’s discuss how to use a CTE, which will be named “a”, first classify it using “case”, to determine the value of the sku_name column, which contains a certain series of characters to enter into the classification desired.</li><li>Sum the after_discount column to get the total transaction value.</li><li>In this problem, it is necessary to use a join; the sku_detail table is aliased with s and the order_detail table is aliased with o. The value similarities are found in the id column in the sku_detail table and the sku_id column in the order_detail table.</li><li>The condition used is the column is_valid = 1, which means the customer has made a payment.</li><li>Group by product brand column.</li><li>After finishing setting the CTE, display everything from table “a” as a CTE, with the condition that the product_brand column is not null. Sorted descendingly based on the column “total_nilai_transaksi” to get the largest value.</li></ol><p>The query will look like below:</p><pre>with a as(<br>select<br> case<br>  when lower(sku_name) like &#39;%samsung%&#39; then &#39;Samsung&#39;<br>  when lower(sku_name) like &#39;%sony%&#39; then &#39;Sony&#39;<br>  when lower(sku_name) like &#39;%huawei%&#39; then &#39;Huawei&#39;<br>  when lower(sku_name) like &#39;%lenovo%&#39; then &#39;Lenovo&#39;<br>  when lower(sku_name) like &#39;%apple%&#39; or lower(sku_name) like &#39;%iphone%&#39; or lower(sku_name) like &#39;%macbook%&#39; then &#39;Apple&#39;<br> end product_brand,<br> sum(after_discount) total_nilai_transaksi<br>from sku_detail s<br>join order_detail o on s.id = o.sku_id<br>where is_valid = 1<br>group by 1<br>)<br>select * from a <br>where product_brand is not null<br>order by 2 Desc;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*S7tNyNsXKNxxXHp8IIpEGQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*Pm8wgxLfP5e5qVkHReATFg.png" /></figure><p>Explanation:<br>Based on the results displayed, the transaction value went from the largest to the smallest, starting with <strong>Samsung, Apple, Huawei, Lenovo, </strong>and <strong>Sony</strong>.</p><p><strong>Number 8</strong><br>Q: Like question no. 3, make a comparison of the profit values for 2021 and 2022 for each category. Then make the difference % profit between 2021 and 2022 (profit = after_discount — (cogs*qty_ordered)).<br>Use <strong>is_valid = 1</strong> to filter transaction data.<br>Source table: <em>order_detail, sku_detail</em></p><p>Solution:</p><ol><li>In this problem, first create a CTE named “b” in which there is a CTE named “a”, Let’s first discuss CTE “a”. Select the id column from the order_detail table, select the category column, and then extract the year from the order_date column. Then, because this question asks to display the profit value, use the formula profit = after_discount — (cogs*qty_ordered).</li><li>CTE “a” requires a join between the order_detail table aliased with o and the sku_detail table aliased with s. The value similarities are in the sku_id column in the order_detail table and the id column in the sku_detail table. Then use the condition is_valid = 1, which means the customer has made a payment.</li><li>Then switch to CTE “b”, select the category column from CTE “a” and then use the when case to get the results in the form of a new column belonging to 2021 and 2022 separately. Then each of them is added up to get the total transaction value. Group by category column.</li><li>Then display everything from CTE “b”. Because this question is asked to display the difference in the percentage of growth, you need to do calculations using the formula ((total_profit_2022 — total_profit_2021)/total_profit_2021). Order descending based on the growth column.</li></ol><p>The query will look like below:</p><pre>with b as(<br> with a as (<br>  select<br>   o.id,<br>   category,<br>   extract(year from order_date) as tahun,<br>   after_discount - (cogs * qty_ordered) as profit<br>  from order_detail as o<br>  join sku_detail as s on o.sku_id = s.id<br>  where is_valid = 1<br> )<br> select<br>  a.category,<br>  sum(case when tahun = 2021 then profit end) as total_profit_2021,<br>  sum(case when tahun = 2022 then profit end) as total_profit_2022<br> from a<br> group by 1<br>)<br>select *,<br> ((total_profit_2022 - total_profit_2021)/total_profit_2021) as growth<br>from b<br>order by 4 desc;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*acaXrpKYd4A8Sx4KoguvCw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*LFyzXXP5idegTr4NHHpQaA.png" /></figure><p>Explanation:<br>In the resulting output, there is a growth column to see which has increased and which has decreased. Those that have increased have a positive growth value; <strong>the greater the value, the better the increase</strong>. While those that experience a decrease have a negative growth value; <strong>the smaller the value, the worse the increase</strong>.</p><p><strong>Number 9</strong><br>Q: Show the top 5 SKU with the highest profit contribution in 2022 based on the categories with the most profit growth from 2021 to 2022 (based on result no. 8).<br>Use <strong>is_valid = 1</strong> to filter transaction data.<br>Source table: <em>order_detail, sku_detail</em></p><p>Solution:</p><ol><li>In this problem, we will use a CTE that will be named “a”, first select the sku_name column, then create a formula for profit (after_discount — (cogs * qty_ordered)). This CTE “a” uses a join; the order_detail table is aliased with o, and the sku_detail table is aliased with s. The value similarities are in the sku_id column in the order_detail table and the id column in the sku_detail table.</li><li>The conditions for CTE “a” are column is_valid = 1, extract the year for the 2022 filter only, and the largest category based on number 8, which is ‘Women Fashion’.</li><li>Then select the sku_name column and the sum of the profit column to get the total profit; the selected column comes from the CTE table “a”.</li><li>Group by 1, sku_name column, sorted in descending order based on the “total_profit” column, to get the largest value.</li><li>Because this question is asked to display the top 5, use limit 5.</li></ol><p>The query will look like below:</p><pre>with a as(<br> select <br>  sku_name,<br>  after_discount - (cogs * qty_ordered) as profit<br> from order_detail as o<br> join sku_detail as s on o.sku_id = s.id<br> where is_valid = 1 and extract(year from order_date) = 2022 and category = &#39;Women Fashion&#39;<br>)<br>select <br> sku_name,<br> sum(profit) as total_profit<br>from a<br>group by 1<br>order by 2 desc<br>limit 5;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*F_xR_KtINzZaLWc9xDt3DA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*k--Sj35ru4P5LTVylgpN5Q.png" /></figure><p><strong>Number 10</strong><br>Q: Show the number of unique orders using the top 5 payment methods (question no. 6) by product category in 2022.<br>Use <strong>is_valid = 1</strong> to filter transaction data.<br>Source table: <em>order_detail, sku_detail, payment_detail</em></p><p>Solution:</p><ol><li>Select the category column. Based on the answer to question no. 6, the top 5 payment methods (cod, Payaxis, Easypay, customercredit, jazzwallet), use a distinct count to calculate the number of payment_methods used.</li><li>In this problem, we will use two joins: the first is the payment_id column in the order_detail table (aliased by o) joined with the id column in the payment_detail table (aliased by “p”). Then the sku_id column in the order_detail table is combined with the id column in sku_detail (aliased by s).</li><li>In this problem, given the condition that transactions are limited to only 2022, it is necessary to extract the year in the order_date column to select the year 2022, in addition to using the condition column is_valid = 1, which means the customer has made a payment.</li><li>Group by 1 column category. Sorted descending based on the second column to be displayed in the output.</li><li>Limit output to only the top 5 categories.</li></ol><p>The query will look like below:</p><pre>select <br> category,<br> count(DISTINCT case when payment_method = &#39;cod&#39; then o.id end) cod,<br> count(DISTINCT case when payment_method = &#39;Payaxis&#39; then o.id end) Payaxis,<br> count(DISTINCT case when payment_method = &#39;Easypay&#39; then o.id end) Easypay,<br> count(DISTINCT case when payment_method = &#39;customercredit&#39; then o.id end) customercredit,<br> count(DISTINCT case when payment_method = &#39;jazzwallet&#39; then o.id end) jazzwallet<br>from order_detail o <br>join payment_detail &quot;p&quot; on o.payment_id = &quot;p&quot;.id<br>join sku_detail s on o.sku_id = s.id<br>where extract(year from order_date) = 2022 and is_valid = 1 <br>group by 1<br>order by 2 desc<br>limit 5;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*yzwTY3mJbVmP00rd0okSgA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*X4rmbDZ47WtS6ZjaXfr2ZQ.png" /></figure><p>Thank you for reading to the end. I hope this post is useful. Don’t hesitate to give criticism or suggestions; feedback from readers is also very useful for me. I hope you guys are always healthy, wherever you are. See you again in the next medium post!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cde50de33cf0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>