<?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 Najwa Eliva on Medium]]></title>
        <description><![CDATA[Stories by Najwa Eliva on Medium]]></description>
        <link>https://medium.com/@najwaelivaa?source=rss-65481923b45b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Najwa Eliva on Medium</title>
            <link>https://medium.com/@najwaelivaa?source=rss-65481923b45b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 06:35:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@najwaelivaa/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[Google Data Studio for Data Visualization]]></title>
            <link>https://medium.com/@najwaelivaa/google-data-studio-for-data-visualization-7a5d1cd93041?source=rss-65481923b45b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7a5d1cd93041</guid>
            <category><![CDATA[google-data-studio]]></category>
            <category><![CDATA[myskill]]></category>
            <category><![CDATA[data-visualization]]></category>
            <dc:creator><![CDATA[Najwa Eliva]]></dc:creator>
            <pubDate>Wed, 23 Nov 2022 08:36:24 GMT</pubDate>
            <atom:updated>2022-11-23T08:39:05.228Z</atom:updated>
            <content:encoded><![CDATA[<p>After we done extracting, processing, and do something that we desired about our data, the next important step is to bring the information gained from the data through visualization. Data visualization offers a huge advantage for our work that involves data, especially if we are handling big data with lots of information. By doing data visualization, the information will be more accessible, presentable and easy to understand prior making data-driven decisions.</p><p>The data can be visualized in many form of visualizations:</p><ul><li>Chart</li><li>Table</li><li>Graph</li><li>Geospatial</li><li>Infographic</li><li>Dashboard</li><li>Area Map</li><li>Box-and-whisker Plot</li><li>Histogram</li><li>Heat Map</li><li>and many other form.</li></ul><p>To create such visualization, there are many tools that we can use, for example: Microsoft Power BI, Tableau, Google Data Studio, Qlik Sense, Plotly. Google Data Studio or Looker Studio is a free visualization tools provided by Google. Google Data Studio is easy to use, connected to many data sources, and it flexibility allows the users to create interactive dashboards and beautiful customized reporting.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*E8dwgHtJhWUREEgX.png" /></figure><p>In this post, I am going to demonstrate a how to do an exercise given by MySkill as one of the exercise for Data Analysis Bootcamp program. The goals of the exercise is to give a proper and good understanding about data analysis dan visualization for the participants of the bootcamp. In this exercise, we are given a dataset called <a href="https://docs.google.com/spreadsheets/d/1jRSlkSTGuS88H4tQUSeEfJQKfSsUrMthuRR8F-kOx58/edit#gid=0">Hotel Booking Demand</a>, and we have to give a visualization on problems given to understand the data better.</p><p><strong>NUMBER 1:</strong> How many total bookings were made in each type of hotels? What type of hotels is booked more? Explain what insight is obtained from the visualization!</p><p>To compare total bookings made in each type of hotels, we can use bar chart, either horizontal or vertical bar. I am using horizontal bar chart with number of bookings as y-axis. To calculate the number of bookings, I use customer’s data and apply <strong>COUNT DISTINCT </strong>aggregation in the option of Google Data Studio. The visualization for question number 1 can be seen on Figure 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/569/1*9YJtQVa-MDUJlswjYTV2KA.png" /><figcaption><strong>Figure 1. </strong>Number of Bookings in Each Hotel</figcaption></figure><p>From the figure above, it can be be seen that there are 79,330 bookings for City Hotel and 40,060 bookings for Resort Hotel. Therefore, it can be concluded that City Hotel is the type of hotels that have the biggest number of bookings compared to Resort Hotel.</p><p><strong>NUMBER 2: </strong>Show a visualization that compares the number of bookings made by local tourists (customer from Portugal) and bookings made by inbound tourists (customer outside Portugal)! Explain what insight is obtained from the visualization!</p><p>Same as the previous number, to compare the number of bookings made by local tourists and inbound tourists, we will use horizontal bar chart with number of bookings as y-axis. The visualization for question number 2 can be seen on Figure 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/575/1*KNoOvvgjyXjY1CdSL4gGOw.png" /><figcaption><strong>Figure 2. </strong>Number of Bookings by Tourism Category</figcaption></figure><p>From the figure above, it can be be seen that there are 70,800 bookings made by inbound tourists and 48,590 bookings made by local tourists. Therefore, it can be concluded that most visitors of the hotel came from outside Portugal. From the dataset, it is stated that the origin country of inbound tourists varied, such as: United States, United Kingdom, Spain, France, and many more.</p><p><strong>NUMBER 3:</strong> Average Daily Rate or ADR is one of the most important metrics in hotel industry. How is the weekly average ADR pattern for each type of hotels? Does the average ADR rise and fall in both types of hotels at the same period? Explain what insight is obtained from the visualization!</p><p>To figure out ADR pattern on a given time set, we will use time series chart with arrival date as the x-axis and ADR as the y-axis. The question instructed us to present the time set as weekly. Therefore in the setup option for the chart, we change the setting from “Date” into “Yearly Week”. Then at the metric option, we change the setting for cancellation rate from “SUM” to “AVG”. The visualization for question number 3 can be seen on Figure 3.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IilWZEC_Rz1Gpcy2RCC44A.png" /><figcaption><strong>Figure 3. </strong>Weekly Average ADR for Both Hotels</figcaption></figure><p>From the figure above, it can be seen that the pattern for weekly average ADR trend for both type of hotels is different. In the first few weeks, Resort Hotel has higher average ADR compared to City Hotel. However later on, the declined in the average ADR for Resort Hotel can be observed, making City Hotel has higher average ADR compared to Resort Hotel. The average ADR of Resort Hotel seems to rise at certain period. For example, at the middle of August and during year end.</p><p><strong>NUMBER 4:</strong> How is the cancellation rate for each type of hotels each month? Which type of hotels has the highest cancellation rate? Explain what insight is obtained from the visualization!</p><p>Same as question number 3, for question number 4 we will use time series chart with arrival date as the x-axis and cancellation rate as the y-axis. The question instructed us to present the time set as monthly. Therefore in the setup option for the chart, we change the setting from “Date” into “Yearly Month”. Then at the metric option, we change the setting for cancellation rate from “SUM” to “AVG”. The visualization for question number 4 can be seen on Figure 4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PUaO9F0yLnDfv4cm_N4tIQ.png" /><figcaption><strong>Figure 4.</strong> Monthly Average Cancellation Rate for Both Hotels</figcaption></figure><p>From the figure above, the cancellation rate for both type of hotels each month differs with each other. Overall, it can be concluded that the cancellation rate for Resort Hotel is lower compared Resort Hotel. The overall highest cancellation rate occurred in July 2015 for City Hotel. Meanwhile, the overall lowest cancellation rate occurred in January 2015 for Resort Hotel.</p><p><strong>NUMBER 5:</strong> How many cancelled bookings for each type of market segments? Which market segments has the highest cancellation rate? Explain what insight is obtained from the visualization!</p><p>For question number 5, we are instructed to figure out the number of cancelled bookings and cancellation rate for each type of market segments. Hence, we will use bar chart with line to visualize the data. For the first metrics, that is Number of Cancelled Booking, the metrics is defined as “SUM”. For the second metrics, that is Cancellation Rate, the metrics is defined as “AVG” and percentage option is applied. The visualization for question number 4 can be seen on Figure 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/663/1*tEIZd6stXKNMjWriV5fhAg.png" /><figcaption><strong>Figure 5. </strong>Cancelled Bookings by Market Segments</figcaption></figure><p>From the figure above, we can see that the market segments with the highest cancellation rate is Groups with 60.93% cancellation rate, followed by Online TA with 36.83% cancellation rate and Offline TA/TO with 34.25% cancellation rate. However, we can see that the market segments with highest number of cancelled bookings is Online TA, followed by Groups and Offline TA/TO. Therefore, it can be concluded that having the highest number of cancelled bookings doesn’t always means that the cancellation rate is also the highest. For example in the case of Online TA, even though the number of cancelled bookings is the highest, the number of successful booking is also high, making it has lower cancellation rate compared to Groups.</p><p><strong>NUMBER 6: </strong>From the same data, make an additional visualization that is different from 5 questions above! Explain what insight is obtained from the visualization!</p><p>For question number 6, I will figure out the number of guest that comes back to stay at the hotel and the customer retention rate. In my opinion, this parameter is important because every business must have goals to not only wanting new customers, but also wants their previous customers to revisit their hotel to enjoy their services. In this case, the previous customers can coming back to stay at the hotel again. Same as question number 5, I am using bar chart with line to visualize the data. The visualization for question number 4 can be seen on Figure 6.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/622/1*fz9AQanLGeRqfArL6EIsJA.png" /><figcaption><strong>Figure 6. </strong>Repeated Customer for Both Hotels</figcaption></figure><p>From the figure above, we can see for City Hotel, the number of repeated guests is 2,034 with 2.54% customer retention rate. In the other side for Resort Hotel, the number of repeated guests is 1,780 with 4.42% customer retention rate. Therefore, we can conclude that the customer retention rate for Resort Hotel is higher compared to City Hotel.</p><p>Question number 6 marks the end of my explanation on how to perform data visualization using Google Data Studio to do an exercise given by MySkill for the participants of their Data Analysis Bootcamp. My full work on Google Data Studio can be accessed in <a href="https://datastudio.google.com/reporting/444ca91a-4aec-4bd1-aad2-50e368aaedfe">this link</a>. See you on my other post about <a href="https://medium.com/@najwaelivaa/python-for-data-analysis-8358609c3506">Python for Data Analysis</a> and <a href="https://medium.com/@najwaelivaa/sql-for-data-analysis-8c57dd5932b3">SQL for Data Analysis</a>!☺️</p><p>References:</p><p>Ahmeti, E. (2022). What is Looker Studio and what are its benefits. [online] Retrieved from <a href="https://windsor.ai/what-is-google-data-studio-and-what-are-its-benefits/">https://windsor.ai/what-is-google-data-studio-and-what-are-its-benefits/</a> (Accessed on 21/11/22)</p><p>Tableau. Data Visualization. [online] Retrieved from <a href="https://www.tableau.com/learn/articles/data-visualization">https://www.tableau.com/learn/articles/data-visualization</a> (Accessed on 21/11/22)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7a5d1cd93041" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Python for Data Analysis]]></title>
            <link>https://medium.com/@najwaelivaa/python-for-data-analysis-8358609c3506?source=rss-65481923b45b------2</link>
            <guid isPermaLink="false">https://medium.com/p/8358609c3506</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[myskill]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Najwa Eliva]]></dc:creator>
            <pubDate>Wed, 23 Nov 2022 08:35:25 GMT</pubDate>
            <atom:updated>2022-11-23T08:40:42.485Z</atom:updated>
            <content:encoded><![CDATA[<p>Python is a programming language created by Guido van Rossum in 1991 and developed further by the Python Software Foundation. As a programming language, it can be used for: web development, software development, mathematic purposes, and system scripting. Up to this day, Python has become a widely used general-purpose, high-level programming language because of its versatility and easy to read language.</p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/0*LthQD8emAmheEnxT.png" /></figure><p>In Python, includes several useful libraries that we can use to perform data analysis. Two of them is <strong>Pandas</strong> and <strong>NumPy</strong>, which I will used in to solve the questions in this post. Pandas, that is derived from the word Panel Data, is an open-source library that provides high-performance data manipulation in Python. Pandas is built on top of the NumPy package, therefore NumPy is required to operate Pandas. A Pandas DataFrame, which structured like a table or spreadsheet, can be easily changed and manipulated. Pandas offers an advance support for data analysis by handling missing data, performing operations on columns and rows, also transforming data.</p><p>NumPy, which derived from the word Numerical Python, is an open-source Python library that facilitates numerical operations on large quantities of data. It is used to perform various numerical computations and processing of the single-dimensional and multidimensional array elements. NumPy offers a faster calculations compared to using normal Python arrays. The most important part about NumPy is that Pandas is built on top of it. So, Pandas is dependent on NumPy.</p><p>In this post, I am going to demonstrate and discuss on how to use Python for data analysis for solving the exercise given by MySkill for their Data Analysis Bootcamp. The goal of the exercise is to develop and practice the participant’s knowledge on analyzing data with Python. Therefore, the participant will be familiar and proficient in working in Python workspace. In this exercise, I will use Google Collab as a tool for Python.</p><p>The given data is Hotel Booking Dataset that can be downloaded in this <a href="https://github.com/rfordatascience/tidytuesday/tree/master/data/2020/2020-02-11">link</a>.</p><p>The very first things we need to do is to import Pandas and NumPy to our Python workspace. The code for importing Pandas and NumPy is written below:</p><pre>import pandas as pd<br>import numpy as np<br>import matplotlib.pyplot as plt<br>import seaborn as sns<br>import re<br>sns.set()</pre><p>Then, we need to import the data by copying the link of the data in our script. The code to import the data is written below:</p><pre>df_hotels = pd.read_csv(&#39;https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-11/hotels.csv&#39;)<br>df_hotels.head()</pre><p>After we run the code, the data will be automatically imported to the system. We can check the columns and the first rows of our data frame by running the syntax <strong>df_hotels.head()</strong>.</p><p>The data that we imported before lacks a unique identifier. Therefore, a column that contains number will be added as ‘id’. The code to add ‘id’ as the unique identifier is written below:</p><pre>df_hotels = df_hotels.reset_index().rename(columns={&#39;index&#39;:&#39;id&#39;})<br>df_hotels.head()</pre><p>We can check the data types for each column with the code below:</p><pre>df_hotels.dtypes</pre><p>Then, we are ready to go!</p><p><strong>NUMBER 1: </strong>Make functions with one argument in the form of data frame to check data type, number of null value, null value percentage, and number of unique value in each columns!</p><p>To solve question number 1, there are several steps we need to do:</p><ol><li>Make empty lists for each variables.</li><li>Make empty data frames (saved as <strong>summary_check_value</strong>).</li><li>Make looping to fill the data lists (using append method).</li><li>Add the columns and values.</li></ol><p>The code for question number 1 is written below:</p><pre>def check_values(df):<br>    #list untuk datatype<br>    tipe_data = []<br>    #list untuk null<br>    sum_null = []<br>    persentase_null = []<br>    jumlah_unique = []<br>    #summary check value varible<br>    summary_check_value = pd.DataFrame()<br>    for i in df:<br>      tipe_data.append(df[i].dtype)<br>      sum_null.append(df[i].isna().sum())<br>      persentase_null.append(df[i].isna().sum()/len(df))<br>      jumlah_unique.append(df[i].nunique())<br>    summary_check_value[&#39;kolom&#39;] = df.columns.tolist()<br>    summary_check_value[&#39;tipe_data&#39;] = tipe_data<br>    summary_check_value[&#39;sum_null&#39;] = sum_null<br>    summary_check_value[&#39;persentase_null&#39;] = persentase_null<br>    summary_check_value[&#39;jumlah_unique&#39;] = jumlah_unique<br><br>    return(summary_check_value)</pre><p>To check the data frame, write the code below:</p><pre>check_values(df_hotels)</pre><p>The table that corresponds to the code will be shown in Figure 1.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/789/1*ZvxVkuoxLv5I80-eAqnhSg.png" /><figcaption><strong>Figure 1. </strong>summary_check_value Data Frame</figcaption></figure><p><strong>NUMBER 2: </strong>How many visitors that have cancelled the reservation and those who have not? From that answer, make a conclusion about each proportion!</p><p>To figure out on the numbers of successful reservations, we will use the syntax <strong>value_counts()[0]</strong> because the number ‘0’ represents the reservation that didn’t get cancelled. The code is written below:</p><pre> not_canceled = df_hotels[&#39;is_canceled&#39;].value_counts()[0]<br> print(&#39;Jumlah reservasi yang berlangsung&#39;, not_canceled)</pre><p>After we run the code, it is known that the numbers of successful reservations is 75,166.</p><p>Meanwhile, to figure out on the numbers of cancelled reservations, we will use the syntax <strong>value_counts()[1]</strong> because the number ‘1’ represents the reservation that get cancelled. The code is written below:</p><pre> is_canceled = df_hotels[&#39;is_canceled&#39;].value_counts()[1]<br> print(&#39;Jumlah reservasi yang dibatalkan&#39;, is_canceled)</pre><p>After we run the code, it is known that the numbers of cancelled reservations is 44,224.</p><p>To know the proportion for each conditions, we will use the syntax <strong>value_counts(normalize=True)</strong>. This syntax will divide a certain value by the sum of all value, creating proportion. The code is written below:</p><pre> pct_is_canceled = round(df_hotels[&#39;is_canceled&#39;].value_counts(normalize=True),2)<br> print(pct_is_canceled)</pre><p>After we run the code, it is known that the proportion of successful reservation is 63% and the proportion of cancelled reservation is 37%. To create visualization for comparing the number of successful reservations and cancelled reservations, we can use the syntax below:</p><pre>sns.countplot(data=df_hotels, x=&#39;is_canceled&#39;)<br>plt.title(&#39;Proportion&#39;)<br>plt.show()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/1*wxLfCgxpqMpTzf44VXCeiA.png" /><figcaption><strong>Figure 2. </strong>Number of Successful and Cancelled Reservations</figcaption></figure><p>From the figure above, we can see that the number of successful reservations is higher than the number of cancelled reservations. Number 0 means successful reservations, and number 1 means cancelled reservations.</p><p><strong>NUMBER 3: </strong>For both City Hotel and Resort Hotel, how much is the cancelled reservation percentage? Which type of hotels have the biggest cancellation proportion?</p><p>For question number 3, we were instructed to calculate the cancellation proportion for both type of hotels. Same as the previos number, we will use the syntax v<strong>alue_counts(normalize=True)</strong> to calculate proportion. To calculate the cancellation proportion for City Hotel, we added filter <strong>df_hotels.hotel==’City Hotel’</strong> so that only data corresponding to City Hotel that will be included. The code is written below:</p><pre>canceled_ch = round(df_hotels[df_hotels.hotel==&#39;City Hotel&#39;].is_canceled.value_counts(normalize=True)[1],2)<br>print(&#39;Persentase reservasi City Hotel yang dibatalkan&#39;, canceled_ch)</pre><p>After we run the code, it is known that the proportion of cancelled reservations in City Hotel is 42%.</p><p>Similar to City Hotel, we added filter <strong>df_hotels.hotel==’Resort Hotel’</strong> for Resort Hotel so that only data corresponding to Resort Hotel that will be included. The code is written below:</p><pre>canceled_rh = round(df_hotels[df_hotels.hotel==&#39;Resort Hotel&#39;].is_canceled.value_counts(normalize=True)[1],2)<br>print(&#39;Persentase reservasi Resort Hotel yang dibatalkan&#39;, canceled_rh)</pre><p>After we run the code, it is known that the proportion of cancelled reservations in City Hotel is 28%. Therefore, it is known that the proportion of cancelled reservations in City Hotel is higher compared to Resort Hotel.</p><p><strong>NUMBER 4: </strong>Do filtering to show only the data of the visitors who didn’t cancelled the reservation! Save the result in a variable called df_checkout!</p><p>To only show the data with successful reservation, we will add the syntax <strong>df_hotels[‘is_canceled’] == 0</strong> to remove every data that indicates cancelled reservations. The code is written below:</p><pre>df_checkout = df_hotels[df_hotels[&#39;is_canceled&#39;] == 0]</pre><p>To check the number of columns and rows of the data, we write the code below:</p><pre>df_checkout.shape</pre><p>Turns out that the table has 75,166 rows and 33 columns.</p><p><strong>NUMBER 5: </strong>Show the numbers of monthly reservation for each type of hotels! For both type of hotels, in which month the most number reservations occurred? Make plots and map the name of the month to month in numbers!</p><p>To present the data based on the type of hotels and the month the reservation is made, we will do grouping to group-by the data using the syntax <strong>df_checkout.groupby([‘hotel’,’arrival_date_month’])</strong>. The syntax <strong>size()</strong> is added so the system will count the amount of reservations made in a certain type of hotels in a certain month. The code is written below:</p><pre>df_checkout_gb = df_checkout.groupby([&#39;hotel&#39;,&#39;arrival_date_month&#39;]).size().reset_index() </pre><p>We will rename the newly added column to jumlah_reservasi with the syntax below:</p><pre>df_checkout_gb.columns = [&#39;hotel&#39;,&#39;arrival_date_month&#39;,&#39;jumlah_reservasi&#39;]<br>df_checkout_gb</pre><p>After we run the code, the table shown in Figure 3 that shows the number of reservation made in each type of hotels every month will appear in our Python workspace.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*Z1C9SvJr-5pfLpWDFjascA.png" /><figcaption><strong>Figure 3. </strong>The number of reservations made in each type of hotels every month</figcaption></figure><p>To find out the month with the highest number of reservations in each type of hotels, we write the code below:</p><pre>max_ch = df_checkout_gb[df_checkout_gb[&#39;hotel&#39;] == &#39;City Hotel&#39;][&#39;jumlah_reservasi&#39;].max()<br>max_rh = df_checkout_gb[df_checkout_gb[&#39;hotel&#39;] == &#39;Resort Hotel&#39;][&#39;jumlah_reservasi&#39;].max()<br><br>print(&#39;summary max reservasi City Hotel&#39;)<br>display(df_checkout_gb[df_checkout_gb[&#39;jumlah_reservasi&#39;] == max_ch])<br>print(&#39;\n&#39;,&#39;*&#39;*50)<br>print(&#39;summary max reservasi Resort Hotel&#39;)<br>display(df_checkout_gb[df_checkout_gb[&#39;jumlah_reservasi&#39;] == max_rh])</pre><p>The code above will show the table that can be seen in Figure 4.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/333/1*NzJWnhdvIlYW3gXNF9ixfw.png" /><figcaption><strong>Figure 4.</strong> Month with the highest number of reservations</figcaption></figure><p>From the figure above, it can be concluded that for both type of hotels, the reservations were most made in August.</p><p>To map the month to the form of month in numbers, for example January is 1, February is 2, and so on, the first thing that should be done is importing calendar as shown in the code below:</p><pre>import calendar<br><br>for i in calendar.month_name:<br>  print(i)</pre><p>Then, we will map the month to numbers and grouping the data based on type of hotels and months using the syntax below:</p><pre>import calendar<br>month_dict = {month: index for index, month in enumerate(calendar.month_name) if month}<br>df_checkout[&#39;arrival_date_month_num&#39;] = df_checkout[&#39;arrival_date_month&#39;].map(month_dict)<br>df_checkout.groupby([&#39;hotel&#39;,&#39;arrival_date_month_num&#39;]).size()</pre><p>After we run the code, the system will show the table shown in Figure 5. By then, we are succeeded in mapping the month into numbers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/272/1*tTN4tV6c9GF_S3kTEKpaFA.png" /><figcaption><strong>Figure 5. </strong>The number of reservations after mapping the month to numbers</figcaption></figure><p>To compare and visualize the number of reservations in each type of hotels on each months, we will use the syntax below:</p><pre>sns.countplot(data=df_checkout, x=&#39;arrival_date_month_num&#39;,hue=&#39;hotel&#39;)<br>plt.xlabel(&#39;Bulan Kedatangan&#39;)<br>plt.ylabel(&#39;Jumlah Reservasi&#39;)<br>plt.show()</pre><p>The resulted graph are shown in Figure 6 below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/311/1*8YSkFHQAZeM6oDtFlzcbng.png" /><figcaption><strong>Figure 6. </strong>Visualization of the number of reservations data</figcaption></figure><p>From the figure above, it is clearly seen that the month with the highest number of reservations is August.</p><p><strong>NUMBER 6: </strong>Make a new column called arrival_date that includes information about the arrival date, month, and year! Change the type of column to date time!</p><p>For question number 6, the first step is to change the data type of the column arrival_date to string by using the syntax below:</p><pre>df_checkout.arrival_date_month_num.astype(&#39;str&#39;)</pre><p>Next, we will use the syntax below to add zero padding. Therefore the number ‘1’ will be change to ‘01’.</p><pre>df_checkout.arrival_date_month_num.astype(&#39;str&#39;).str.pad(2,fillchar=&#39;0&#39;)</pre><p>After that, we will add the year and the date to the column arrival_date that previously only consists of month.</p><pre> df_checkout[&#39;arrival_date_year&#39;].astype(&#39;str&#39;) + &#39;-&#39; +\<br> df_checkout.arrival_date_month_num.astype(&#39;str&#39;).str.pad(2,fillchar=&#39;0&#39;) + &#39;-&#39; +\<br> df_checkout.arrival_date_day_of_month.astype(&#39;str&#39;).str.pad(2,fillchar=&#39;0&#39;)</pre><p>The next step is creating a new column will the syntax below:</p><pre>df_checkout[&#39;arrival_date&#39;] = \<br>    df_checkout[&#39;arrival_date_year&#39;].astype(&#39;str&#39;) + &#39;-&#39; +\<br>    df_checkout.arrival_date_month_num.astype(&#39;str&#39;).str.pad(2,fillchar=&#39;0&#39;) + &#39;-&#39; +\<br>    df_checkout.arrival_date_day_of_month.astype(&#39;str&#39;).str.pad(2,fillchar=&#39;0&#39;)<br><br>df_checkout[&#39;arrival_date&#39;]</pre><p>After we run the code, a table shown in Figure 7 will appear in our Python workspace.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*yveh03NMpU4hg16Ulew4Ow.png" /><figcaption><strong>Figure 7. </strong>The column arrival_date that shows the arrival date, month, and year</figcaption></figure><p>Then, the question instructed us to change the data type of the column from string into date time. The code to change the data type is written below:</p><pre>df_checkout[&#39;arrival_date&#39;] = pd.to_datetime(df_checkout.arrival_date)<br>df_checkout[&#39;arrival_date&#39;]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/455/1*pP7nzIdU3dwiKYP7VO_pHw.png" /><figcaption><strong>Figure 8. </strong>The column arrival_date that shows the arrival date, month, and year with data time as the data type</figcaption></figure><p>The data type is successfully converted into data time.</p><p><strong>NUMBER 7: </strong>Make two data frames that show (1) Daily total reservation (saved as df_reservasi_perhari), and (2) Average daily reservation in each week (saved as df_avg_reservasi_harian)!</p><p>For question number 7, to make data frames that show daily total reservation, we will use the syntax <strong>df_checkout.resample(‘D’,on=’arrival_date’)</strong> because the syntax <strong>dataframe.resample()</strong> is primarily used for time series data (in this case, daily). The code is written below:</p><pre>df_reservasi_perhari = df_checkout.resample(&#39;D&#39;,on=&#39;arrival_date&#39;).size().reset_index().rename(columns={0:&#39;total_reservasi&#39;})<br>df_reservasi_perhari</pre><p>The table that corresponds with the code is shown in Figure 9 below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*ru_gcH2jP1mCOL3KtKEO0A.png" /><figcaption><strong>Figure 9. </strong>Daily total reservation</figcaption></figure><p>To make data frames that show average daily reservation in each week, we will use the syntax <strong>df_checkout.resample(‘D’,on=’arrival_date’)</strong> and <strong>resample(‘W’,on=’arrival_date’)</strong>. In addition, the syntax <strong>mean()</strong> is added to calculate the average daily reservation in each week. The code is written below:</p><pre>df_avg_reservasi_harian = df_checkout.resample(&#39;D&#39;,on=&#39;arrival_date&#39;).size().reset_index().rename(columns={0:&#39;total_reservasi&#39;}).\<br>resample(&#39;W&#39;,on=&#39;arrival_date&#39;)[&#39;total_reservasi&#39;].mean().reset_index()<br>df_avg_reservasi_harian</pre><p>The table that corresponds with the code is shown in Figure 10 below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/282/1*kYzrCcsQmYoBG0OFF_sxTQ.png" /><figcaption><strong>Figure 10. </strong>The average daily reservation in each week</figcaption></figure><p><strong>NUMBER 8: </strong>What is the average ADR based on type of hotels and customer type? Which type of customers that have the biggest ADR in each type of hotels?</p><p>To calculate the average ADR based on type of hotels and customer type, we will do grouping by hotel and customer_type, and then adding the syntax <strong>mean()</strong> to calculate the average value. The code is written below:</p><pre>summary_custype = df_checkout.groupby([&#39;hotel&#39;,&#39;customer_type&#39;])[&#39;adr&#39;].mean().reset_index()<br>summary_custype.columns= [&#39;hotel&#39;,&#39;customer_type&#39;,&#39;mean_adr&#39;]<br>summary_custype</pre><p>The table that corresponds with the code is shown in Figure 11 below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*rsgIQGcn-Hg87XeYuP7q8g.png" /><figcaption><strong>Figure 11. </strong>Average ADR based on type of hotels and customer type</figcaption></figure><p>To figure out which customer type that has the biggest ADR in each type hotels, we will use the same syntax used in question number 5 to find the month with the highest number of reservations. The code is written below:</p><pre>max_chs = summary_custype[summary_custype[&#39;hotel&#39;] == &#39;City Hotel&#39;][&#39;mean_adr&#39;].max()<br>max_rhs = summary_custype[summary_custype[&#39;hotel&#39;] == &#39;Resort Hotel&#39;][&#39;mean_adr&#39;].max()<br><br>print(&#39;summary max reservasi City Hotel&#39;)<br>display(summary_custype[summary_custype[&#39;mean_adr&#39;] == max_chs])<br>print(&#39;\n&#39;,&#39;*&#39;*50)<br>print(&#39;summary max reservasi Resort Hotel&#39;)<br>display(summary_custype[summary_custype[&#39;mean_adr&#39;] == max_rhs])</pre><p>The table that corresponds with the code is shown in Figure 12 below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/411/1*zSfYTMnaeQNIougL_jhlFw.png" /><figcaption><strong>Figure 12. </strong>Customer type with the biggest ADR in each type of hotels</figcaption></figure><p>From the figure above, it can be concluded that the customer type with the biggest ADR in both City Hotel and Resort Hotel is transient.</p><p>Question number 8 marks the end of my explanation on how to perform data analysis using Python to do an exercise given by MySkill for the participants of their Data Analysis Bootcamp. See you on my other post about <a href="https://medium.com/@najwaelivaa/sql-for-data-analysis-8c57dd5932b3">SQL for Data Analysis</a> and <a href="https://medium.com/@najwaelivaa/google-data-studio-for-data-visualization-7a5d1cd93041">Google Data Studio for Data Visualization</a>!☺️</p><p>Reference:</p><p>Codecademy Team. Introduction to Pandas and NumPy. [online] Retrieved from <a href="https://www.javatpoint.com/pandas-vs-numpy">https://www.javatpoint.com/pandas-vs-numpy</a> (Accessed on 23/11/22)</p><p>Geeks for Geeks. (2022). Introduction to PYTHON. [online] Retrieved from <a href="https://www.geeksforgeeks.org/introduction-to-python/">https://www.geeksforgeeks.org/introduction-to-python/</a> (Accessed on 22/11/22)</p><p>Java T Point. Pandas vs NumPy. [online] Retrieved from <a href="https://www.javatpoint.com/pandas-vs-numpy">https://www.javatpoint.com/pandas-vs-numpy</a> (Accessed on 23/11/22)</p><p>W3 Schools. Python Introduction. [online] Retrieved from <a href="https://www.w3schools.com/python/python_intro.asp">https://www.w3schools.com/python/python_intro.asp</a> (Accessed on 22/11/22)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8358609c3506" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL for Data Analysis]]></title>
            <link>https://medium.com/@najwaelivaa/sql-for-data-analysis-8c57dd5932b3?source=rss-65481923b45b------2</link>
            <guid isPermaLink="false">https://medium.com/p/8c57dd5932b3</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data-analysis]]></category>
            <dc:creator><![CDATA[Najwa Eliva]]></dc:creator>
            <pubDate>Tue, 22 Nov 2022 14:23:51 GMT</pubDate>
            <atom:updated>2022-11-23T08:39:58.135Z</atom:updated>
            <content:encoded><![CDATA[<p>This day, data is considered as the “new oil” in the market, and jobs related to data is currently one of the most promising career. Data analysis is the process of cleaning, manipulating, processing, and combining raw data to gain and present information that will help in decision making process. The information extracted from data analysis is important for business continuity, as the data shows the exact condition of a business. By performing analysis on data, we can have a good understanding on how well the business run, the business sales, the targeted customer of the business. We might also get solution on problems happened in the business.</p><p>To perform data analysis and interpretation, there are several software and tools that can be used. That includes Microsoft Excel, Python, Jupyter Notebook, Microsoft Power BI, Tableau, and many more. In this post, I am going to discuss on how to use SQL server as a tools to perform data analysis. SQL (Structured Query Language) is a command language used in storing, manipulating, and retrieving data in databases. SQL is one of the most commonly used tools because it offers a fast-performed server that used a simple and flexible language that is capable to handle complex databases.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/900/0*HDHtwPQIwbDHfQ2p.jpg" /></figure><p>I am going to demonstrate and discuss on how to do an exercise given by MySkill for data analysis bootcamp. The goal of the exercise is to develop and practice the participant’s knowledge on analyzing data with SQL. Therefore, the participant will be familiar and proficient in working in SQL workspace. In this exercise, I will use PostgreSQL as a tool.</p><p>The cases are about unicorn companies. Unicorn companies are private startup companies with more than 1 billion valuation. We are given 9 questions related to unicorn companies that we need to solved. To do this exercise, we are given four sets of data, that is:</p><ol><li><a href="https://drive.google.com/file/d/1RQucUgaZOq5FmsLfNNWQYsBYYE8CpA7E/view"><strong>unicorn_companies</strong></a>. This data consists of company id, company name, origin city, country, and continent of the company).</li><li><a href="https://drive.google.com/file/d/1iuVtsF2H6hwN8aQVEOUidEcoizKav8jP/view"><strong>unicorn_industries</strong></a>. This data consists of company id and industry type of the company.</li><li><a href="https://drive.google.com/file/d/1el8rqPfBx0Hv594ea3SAK5PQRgkWfYpz/view"><strong>unicorn_funding</strong></a>. This data consists of company id, investor list, company valuation and funding.</li><li><a href="https://drive.google.com/file/d/1M3iDDKPBnJiy473XUxY5Lqhz_VEMxMjC/view"><strong>unicorn_dates</strong></a>. This data consists of company id, company founding year, and the date the company joined as unicorn company.</li></ol><p>Before we proceed further, we have to import the data by running the given code above in PostgreSQL workspace. We are ready to go once we can see the tables are included in the database.</p><p><strong>NUMBER 1: </strong>Sort the continents based on the largest number of unicorn companies? What continents have the most unicorn companies?</p><p>For question number 1, the data that will be used is <strong>unicorn_companies</strong>. The question ask us to display the continents with the largest number of unicorn companies. Therefore, we need to <strong>SELECT </strong>the column <strong>continent</strong>. Another columns that are needed is the amount of unicorn companies in each continent. Therefore, we need to use <strong>COUNT </strong>function with <strong>DISTINCT</strong>. <strong>COUNT </strong>function with <strong>DISTINCT </strong>is used to remove repetitive data. The next step is applying <strong>GROUP BY</strong> statement to group the data based on the continent and <strong>ORDER BY DESC</strong> function to sort the data from the largest number to the smallest number.</p><p>The code is written below:</p><pre>SELECT  continent,<br>        COUNT(DISTINCT company_id) AS company_number<br>FROM unicorn_companies<br>GROUP BY continent<br>ORDER BY company_number DESC</pre><p>The table that will be shown by PostgreSQL after that code is running is presented on Figure 1. From the figure below, we can conclude that North America is the continent with the largest amount of unicorn companies, with 589 companies, followed by Asia with 310 companies, and Europe with 143 companies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*Svta9sr3rY-pvGLRQ3tmQw.png" /><figcaption><strong>Figure 1. </strong>Continents with the largest number of unicorn companies</figcaption></figure><p><strong>Number 2: </strong>Which countries have more than 100 unicorn companies?</p><p>For question number 2, the data that will be used is <strong>unicorn_companies</strong>. The question ask us to figure out countries with more than 100 unicorn companies. Therefore, we will <strong>SELECT </strong>the column <strong>country </strong>and count how many unicorn companies in each country with <strong>COUNT </strong>function. In <strong>COUNT </strong>function, the column <strong>company_id</strong> will be used to determine how many unicorn companies because the id of the company differs from each other, thus making <strong>company_id</strong> become a unique data. Next, <strong>GROUP BY</strong> statement is used to group the data based on the country. <strong>HAVING COUNT</strong> function is used to filter the data, so that only countries with more than 100 unicorn companies are shown. <strong>ORDER BY DESC</strong> function is used to sort the data from the largest to smallest.</p><p>The code is written below:</p><pre>SELECT  country,<br>  COUNT (company_id) AS company_number<br>FROM unicorn_companies<br>GROUP BY country<br>HAVING COUNT (company_id) &gt;= 100<br>ORDER BY company_number DESC</pre><p>The table that will be shown by PostgreSQL after the code is running is presented by Figure 2. From the figure below, we can conclude that there are only two countries with more than 100 unicorn companies, that is United States with 562 companies, followed by China with 173 companies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/534/1*Ok-VLY-vtAtES-P-l41Pfw.png" /><figcaption><strong>Figure 2. </strong>Countries with more than 100 unicorn companies</figcaption></figure><p><strong>NUMBER 3:</strong> What industry is the largest based on the total funding? How much is the average valuation?</p><p>For question number 3, the data that will be used in <strong>unicorn_industries</strong> and <strong>unicorn_funding</strong>. We used two data because the required data to be presented exists in two different table, therefore we will joined both data to present them in the same table with <strong>LEFT JOIN</strong> function. We will <strong>SELECT </strong>the column <strong>ui.industry</strong>, <strong>SUM </strong>function for the column <strong>uf.funding</strong> to sum the funding in each industry, and <strong>AVG </strong>function for the column <strong>uf.valuation</strong> to average the valuation in each industry. The prefix “ui” in ui.industry or “uf” from uf.funding shows that the data belong to the data unicorn_industries and unicorn_funding respectively. To join two dataset, the join key is <strong>company_id</strong> because both data contains the column <strong>company_id</strong>. Next, <strong>GROUP BY</strong> statement is used to group the data based on the industry, and <strong>ORDER BY DESC</strong> function is used to sort the data based on the total funding and average valuation in each industry from the largest to the smallest amount.</p><p>The code is written below:</p><pre>SELECT  ui.industry,<br>        SUM(uf.funding) AS total_funding,<br>        AVG(uf.valuation) AS avg_valuation<br>FROM unicorn_industries AS ui<br>LEFT JOIN unicorn_funding AS uf<br>ON ui.company_id = uf.company_id<br>GROUP BY industry<br>ORDER BY total_funding DESC, avg_valuation DESC</pre><p>The table that will be shown by PostgreSQL after the code is running is presented by Figure 3. From the figure below, we can conclude that Fintech is the largest industries based on the total funding, followed by E-commerce &amp; direct-to-consumer, and Internet software &amp; services.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/688/1*_ZTSAEGCSzbE3GewN3V-wQ.png" /><figcaption><strong>Figure 3.</strong> Largest industries based on the total funding</figcaption></figure><p><strong>NUMBER 4:</strong> Based on the industry we decided on number 3, how many companies joined as unicorn companies annually in the period of 2016 until 2022?</p><p>For question number 4, the data that will be used is <strong>unicorn_companies</strong>, <strong>unicorn_industries</strong>, and <strong>unicorn_dates</strong>. We are going to join all data using <strong>LEFT JOIN</strong> function. Because there are three data, <strong>LEFT JOIN</strong> function will be used twice. We will <strong>SELECT</strong> year joined column by using <strong>EXTRACT</strong> function to extract the year from the column date_joined because the format of the column is yyyy-mm-dd. Next, we will also use <strong>COUNT</strong> function to count the number of the companies, similar to what we did previously on number 2 using the unique column <strong>company_id</strong>. For joining, the join key is still the same, <strong>company_id</strong>. <strong>WHERE</strong> clause is used to filter the data so only data related to Fintech industry (based on the answer on number 3) and the companies that considered unicorn in the year between 2016 and 2022 will be shown after we run the code. For string data type, do not forget to include quotation mark (‘ ’). Grouping will be done based on <strong>year_joined</strong>.</p><p>The code is written below:</p><pre>SELECT  EXTRACT (YEAR FROM ud.date_joined) AS year_joined,<br>        COUNT (DISTINCT uc.company_id) AS company_number<br>FROM unicorn_companies AS uc<br>LEFT JOIN unicorn_dates AS ud <br>     ON uc.company_id = ud.company_id<br>LEFT JOIN unicorn_industries AS ui <br>     ON uc.company_id = ui.company_id<br>WHERE ui.industry = &#39;Fintech&#39;<br>AND (EXTRACT (YEAR FROM ud.date_joined) BETWEEN &#39;2016&#39; AND &#39;2022&#39;)<br>GROUP BY EXTRACT (YEAR FROM ud.date_joined)</pre><p>The table that will be shown by PostgreSQL after the code is running is presented by Figure 4. From the figure below, we can conclude that there are no companies that newly joined as unicorn companies in 2016, there are 6 companies that joined as unicorn companies in 2017, 10 companies in 2018, 20 companies in 2019, 15 companies in 2020, 138 companies in 2021, and 31 companies in 2022.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*EHJHdF_hLwY8XPkXkXUeUw.png" /><figcaption><strong>Figure 4. </strong>Number of companies considered unicorn in 2016-2022</figcaption></figure><p><strong>NUMBER 5:</strong> (a) Show a detailed data that gives the information about company id, origin city, country, and continent, company industry, and company valuation. What is the origin country of the industry with the largest valuation? (b) How about Indonesia? What company holds the largest valuation?</p><p>For question number 5, the data that will be used are <strong>unicorn_companies</strong>, <strong>unicorn_industries</strong>, and <strong>unicorn_funding</strong>. We will <strong>SELECT </strong>the column <strong>uc.company</strong>, <strong>uc.city</strong>, <strong>uc.country</strong>, <strong>uc.continent</strong>, <strong>ui.industry</strong>, and <strong>uf.valuation</strong>. The prefix ui, uc, and uf indicate the source of the column used. Same as previous number, <strong>LEFT JOIN</strong> function will be used twice with <strong>company_id</strong> as the join key. Next, we will use <strong>ORDER BY DESC</strong> function to sort the data from the largest valuation to the smallest valuation.</p><p>The code is written below:</p><pre>SELECT  uc.company,<br>        uc.city,<br>        uc.country,<br>        uc.continent,<br>        ui.industry,<br>        uf.valuation<br>FROM unicorn_companies AS uc<br>LEFT JOIN unicorn_industries AS ui <br>     ON uc.company_id = ui.company_id<br>LEFT JOIN unicorn_funding AS uf <br>     ON uc.company_id = uf.company_id<br>ORDER by uf.valuation DESC</pre><p>The table that will be shown by PostgreSQL after the code is running is presented by Figure 5. From the figure below, we can conclude that there are the company with the largest valuation is Bytedance, an artificial intelligence company from China, followed by SpaceX from United States and SHEIN from China.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XBrqiJs8sZ9wCS0EiG7Hhg.png" /><figcaption>Figure 5. Companies with the largest valuation</figcaption></figure><p>To answer question 5b, WHERE clause is added to filter the data so only companies from Indonesia are shown in the final table. The code is written below:</p><pre>SELECT  uc.company,<br>        uc.city,<br>        uc.country,<br>        uc.continent,<br>        ui.industry,<br>        uf.valuation<br>FROM unicorn_companies AS uc<br>LEFT JOIN unicorn_industries AS ui <br>     ON uc.company_id = ui.company_id<br>LEFT JOIN unicorn_funding AS uf <br>     ON uc.company_id = uf.company_id<br>WHERE uc.country = &#39;Indonesia&#39;<br>ORDER by uf.valuation DESC</pre><p>The table is shown in Figure 6. From the figure below, we can conclude that in Indonesia, the company with the largest valuation is J&amp;T Express, followed by Traveloka and Akulaku.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NkhLVjVNPKu3p6RSIrP-sw.png" /><figcaption><strong>Figure 6. </strong>Companies with the largest valuation in Indonesia</figcaption></figure><p><strong>NUMBER 6:</strong> What is the oldest company based on the age when the company joined unicorn companies? Which country is the company from?</p><p>For question number 6, the data that will be used are <strong>unicorn_companies </strong>and <strong>unicorn_dates</strong>. We will <strong>SELECT </strong>the column <strong>ud.year_founded</strong>, <strong>ud.date_joined</strong>, all the column from unicorn_companies (written as <strong>uc.*</strong>), and the age of each companies (obtained by substracting the date joined with the founded year of the company). <strong>LEFT JOIN</strong> is used with <strong>company_id</strong> as the join key. <strong>ORDER BY DESC</strong> is used to sort the company age from the oldest to the youngest.</p><p>The code is written below:</p><pre>SELECT  ud.year_founded,<br>        ud.date_joined,<br>        uc.*,<br>        EXTRACT (YEAR FROM ud.date_joined) - year_founded AS company_age<br>FROM unicorn_companies as uc<br>LEFT JOIN unicorn_dates as ud<br>     ON uc.company_id = ud.company_id<br>ORDER by company_age DESC</pre><p>The table that will be shown by PostgreSQL after the code is running is presented by Figure 7. From the figure below, we can conclude that oldest company when they considered unicorn company for the first time is Otto Bock Healthcare from Germany, followed by Five Star Business Finance from India, and Promasidor Holdings from South Africa.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FXICHAu0DK0UyYkTo8JCIA.png" /><figcaption><strong>Figure 7.</strong> The oldest company considered unicorn company</figcaption></figure><p><strong>Number 7:</strong> For the company founded between 1960 and 2000, what is the oldest company based on the age when the company joined unicorn companies? Which country is the company from?</p><p>For question number 7, the data that will be used are the same from number 6, that is <strong>unicorn_companies </strong>and <strong>unicorn_dates</strong>. The working steps and code are also similar with number 6, but with the addition of <strong>WHERE</strong> clause before <strong>ORDER BY DESC </strong>function to filter the data so only the companies founded between 1960 and 2000 are included.</p><p>The code is written below:</p><pre>SELECT  ud.year_founded,<br>        ud.date_joined,<br>        uc.*,<br>  EXTRACT (YEAR FROM ud.date_joined) - year_founded AS company_age<br>FROM unicorn_companies as uc<br>LEFT JOIN unicorn_dates as ud<br>ON uc.company_id = ud.company_id<br>WHERE ud.year_founded BETWEEN 1960 AND 2000<br>ORDER by company_age DESC</pre><p>The table that will be shown by PostgreSQL after the code is running is presented by Figure 8. From the figure below, we can conclude that from the company founded between 1960 and 2000, the oldest company when they considered unicorn company for the first time is Five Star Business Finance from India, followed by Promasidor Holdings from South Africa and Thirty Madison from United States.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hVMWP6LDADV1-dit-c8blw.png" /><figcaption><strong>Figure 8. </strong>The oldest company founded between 1960 and 2000 considered unicorn company</figcaption></figure><p><strong>Number 8: </strong>(a) How many companies that are funded by at least one investor with the name ‘venture’? (b) How many companies that are funded by at least one investor with the name ‘venture’, ‘capital’, and ‘partner’?</p><p>For question number 8, the data that will be used is <strong>unicorn_funding</strong>. The question asks us to figure out how many companies that are funded investor with the name ‘venture’. Therefore, <strong>LIKE</strong> command will be used. <strong>LIKE</strong> command is used in a <strong>WHERE</strong> clause to search for a specified pattern in a column. To find investors that contain ‘venture’ in their name, we will add percent sign (%) at the beginning and the end of the sentences. The <strong>WHERE </strong>clause is based on the column select_investors.</p><p>The code is written below:</p><pre>SELECT COUNT(DISTINCT company_id) AS total_company<br>FROM unicorn_funding<br>WHERE LOWER(select_investors) LIKE &#39;%venture%&#39;</pre><p>The table that will be shown by PostgreSQL after the code is running is presented by Figure 9. From the figure below, we can conclude that there are 603 companies that are funded by investors with the name ‘venture’</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*-qu9kQ2GO_dlbLTkN4MdGA.png" /><figcaption><strong>Figure 9. </strong>Number of companies funded by investors with the name ‘venture’</figcaption></figure><p>For question 8b, the question asks us to figure out how many companies that are funded investor with the name ‘venture’, ‘capital’, and ‘partner’ each. Same as question 8a, LIKE command will be used. Different from before, there are three conditions. Therefore, we will used CASE WHEN clause for each condition and WHERE clause is not needed.</p><p>The code is written below:</p><pre>SELECT  COUNT(DISTINCT CASE WHEN LOWER (select_investors) LIKE &#39;%venture%&#39; THEN company_id END) AS investor_venture,<br>        COUNT(DISTINCT CASE WHEN LOWER (select_investors) LIKE &#39;%capital%&#39; THEN company_id END) AS investor_capital,<br>        COUNT(DISTINCT CASE WHEN LOWER (select_investors) LIKE &#39;%partner%&#39; THEN company_id END) AS investor_partner<br>FROM unicorn_funding</pre><p>The table that will be shown by PostgreSQL after the code is running is presented by Figure 10. From the figure below, we can conclude that there are 603 companies that are funded by investors with the name ‘venture’, 611 companies that are funded by investors with the name ‘capital’, and 398 companies that are funded by investors with the name ‘partner’.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*DKHfnLXe0vt6LtOsgXQtig.png" /><figcaption><strong>Figure 10. </strong>Number of companies funded by investor with the name ‘venture’, ‘capital’, and ‘partner’</figcaption></figure><p><strong>Number 9: </strong>How many logistic startup considered unicorn companies in Asia? How many logistic startup considered unicorn companies in Indonesia?</p><p>For question number 9, the data that will be used is <strong>unicorn_companies </strong>and <strong>unicorn_industries</strong>. Both data will be joined with <strong>LEFT JOIN</strong> function just like we previously did before. We will use <strong>SELECT COUNT DISTINCT</strong> to count the number of unicorn companies in Asia and <strong>SELECT COUNT DISTINCT</strong> to count the number of logistic unicorn companies in Indonesia. <strong>WHERE</strong> clause is used on two conditions, the first condition is where continent is Asia, so that only companies in Asia will be included. The second condition is where the industry is supply chain, logistic, and delivery, so that only those industries will be included.</p><p>The code is written below:</p><pre>SELECT  COUNT (DISTINCT uc.company_id) AS unicorn_Asia,<br>        COUNT (DISTINCT CASE WHEN uc.country = &#39;Indonesia&#39; THEN uc.company_id END) AS unicorn_Indonesia<br>FROM unicorn_companies AS uc<br>LEFT JOIN unicorn_industries AS ui<br>     ON uc.company_id = ui.company_id<br>WHERE uc.continent = &#39;Asia&#39; AND <br>      ui.industry =&#39;&quot;Supply chain, logistics, &amp; delivery&quot;&#39;</pre><p>The table that will be shown by PostgreSQL after the code is running is presented by Figure 11. From the figure below, we can conclude that there are 26 logistic unicorn companies in Asia and 1 logistic unicorn companies in Indonesia.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/421/1*mDoPg6wE6rya7f5kmgTXCA.png" /><figcaption><strong>Figure 11. </strong>The amount of logistic unicorn companies in Asia and Indonesia</figcaption></figure><p>Question number 9 marks the end of my explanation on how to use SQL to do an exercise given by MySkill for the participants of their Data Analysis Bootcamp. See you on my other post about <a href="https://medium.com/@najwaelivaa/python-for-data-analysis-8358609c3506">Python for Data Analysis</a> and <a href="https://medium.com/@najwaelivaa/google-data-studio-for-data-visualization-7a5d1cd93041">Google Data Studio for Data Visualization</a>!☺️</p><p>References:</p><p>Kelley, K. (2020). What is Data Analysis? Methods, Process and Types Explained. [online] Retrieved from <a href="https://www.simplilearn.com/data-analysis-methods-process-types-article">https://www.simplilearn.com/data-analysis-methods-process-types-article</a> (Accessed on 18/11/22).</p><p>W3 Schools. [online] Retrieved from <a href="https://www.w3schools.com/sql/">https://www.w3schools.com/sql/</a> (Accessed on 18/11/22).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8c57dd5932b3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>