<?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 Duncan Anderson on Medium]]></title>
        <description><![CDATA[Stories by Duncan Anderson on Medium]]></description>
        <link>https://medium.com/@duncan-kg-anderson?source=rss-13c8859ee168------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*smJIxRDSBzdnhoiD.</url>
            <title>Stories by Duncan Anderson on Medium</title>
            <link>https://medium.com/@duncan-kg-anderson?source=rss-13c8859ee168------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 14 Nov 2026 16:27:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@duncan-kg-anderson/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[Sentiment Analysis on bagel shop reviews — comparing New York City & Montréal]]></title>
            <link>https://duncan-kg-anderson.medium.com/sentiment-analysis-on-bagel-shop-reviews-comparing-new-york-city-montr%C3%A9al-ffc4ef65a76f?source=rss-13c8859ee168------2</link>
            <guid isPermaLink="false">https://medium.com/p/ffc4ef65a76f</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-journalism]]></category>
            <category><![CDATA[brainstation]]></category>
            <category><![CDATA[bagels]]></category>
            <category><![CDATA[sentiment-analysis]]></category>
            <dc:creator><![CDATA[Duncan Anderson]]></dc:creator>
            <pubDate>Sat, 24 Jul 2021 15:11:12 GMT</pubDate>
            <atom:updated>2021-07-25T02:53:18.721Z</atom:updated>
            <content:encoded><![CDATA[<h3>Sentiment Analysis on bagel shop reviews — comparing New York City &amp; Montréal</h3><p>It’s safe to say that most people who have been to New York City or Montreal have probably had a bagel — the reliable, unabating food option as ubiquitous as the 24-hour restaurants and corner bodegas (or depanneurs in Montreal). Both cities are famous for their bagels, and each tend to claim theirs is <em>better </em>than the other’s. If you’re unaware of how contentious this debate is, just ask someone who has been to or lives in either city.</p><p>In 2008, when NASA officials asked Canadian astronaut Gregory Chamitoff what food item he wanted to bring aboard Discovery for a two-week flight into space, he requested fresh bagels from his cousin’s Montreal bagel shop, <em>Fairmount Bagel.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*69aGQ2tMyGy3azmOPC2UHw.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@lulizler?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Lucie Liz</a> from <a href="https://www.pexels.com/photo/bagel-with-salmon-3957499/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><p>For locals and tourists alike, Yelp may sway you to visit one shop over the other due to their business page attributes, namely the star rating and reviews left by visitors.</p><p>As an experiment, I wanted to see if I could determine which city bagel lovers prefer, and in order to answer this, I started an analysis and posed this question: Can one compare the same types of restaurants on Yelp (in my case, restaurants that serve bagels) between two places (in my case, New York City and Montreal) to determine which has the better product or service?</p><p>As someone who lives in Montreal and has visited New York and tried both types of bagels, I felt like a suitable candidate to take a deep dive into the analysis. To clarify the distinction, Montreal-style bagels are handmade and wood-fire baked, and in contrast to the New York-style bagel, are smaller, thinner, leaving a larger hole, and also sweeter with a denser texture. New York-style bagels tend to be much larger (like everything in America) with a softer, chewy interior.</p><blockquote>Despite not being able to say which city has the better bagel after my investigation, I believe that with this new information, instead of relying on numeric attributes already existing on Yelp like star rating and number of reviews, my findings offers a look into the actual text data which has been converted into meaningful insights about the bagel experience at the given restaurant.</blockquote><p>As far as I know, there haven’t been any data-driven approaches to answering the question I posed, however, many data scientists have used review data from Yelp to predict ratings of restaurants and to evaluate future development of businesses with various Natural Language Processing (NLP) techniques, which were helpful for me when looking at angles for how to approach my research question.</p><p>In order to start my investigation, I needed data. I decided to scrape the following attributes from Yelp to form my dataset:</p><ul><li>Restaurant name</li><li>Customer name</li><li>Customer location</li><li>Review text</li><li>Review rating (customer)</li><li>Review date</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/48212eb852b5b42ea12248f5881296ac/href">https://medium.com/media/48212eb852b5b42ea12248f5881296ac/href</a></iframe><p>I kept getting blocked from Yelp’s website when using Python’s web scraping libraries — Selenium and BeautifulSoup — and in the end I discovered a free Google Chrome extension called <a href="https://chrome.google.com/webstore/detail/web-scraper-free-web-scra/jnhgnonknehpejjnehehllkliplmbmhn?hl=en">‘Web Scraper’</a>. I never once got blocked when using their service, and would recommend it.</p><p>I wanted to get a general idea of review and rating behaviour in both cities, so to begin my analysis I simply scraped a combined ~10,000 reviews &amp; ratings from Yelp consisting of a variety of restaurant types. This wasn’t proving to be very helpful in terms of insights, so I dove right in to the bagel datasets, consisting of a combined ~7,300 reviews and ratings for New York and Montreal shops that sold bagels.</p><iframe src="https://flo.uri.sh/visualisation/6124611/embed" width="700" height="575" frameborder="0" scrolling="no"><a href="https://medium.com/media/807ce6e845dee498b05d31e6bcf39eeb/href">https://medium.com/media/807ce6e845dee498b05d31e6bcf39eeb/href</a></iframe><iframe src="https://flo.uri.sh/visualisation/6094856/embed" width="700" height="575" frameborder="0" scrolling="no"><a href="https://medium.com/media/2927d47c694211e5de50313adf55ed79/href">https://medium.com/media/2927d47c694211e5de50313adf55ed79/href</a></iframe><p>Once I read my CSV files containing the Yelp data into Python, I converted each of them into a Pandas DataFrame. Before my investigation, I made sure that each CSV consisted of the same columns as to make my cleaning process more streamlined.</p><p>I dropped unnecessary columns that were generated by the web scraper and renamed some columns for interpretability. There weren’t any null values or duplicates to take care of, however, I had to convert the ‘rating’ column since it was read in as a string. ‘5 star rating’ turned into a simple integer ‘5’.</p><p>I had a hunch during the scraping process that the star rating distribution would be very imbalanced, as I primarily only saw 5 and 4 star ratings. In the future, I would like to import more review data into an ML pipeline to balance out the dataset, as I believe this skewed results in the end. Moving forward, I binarized the rating column so that reviews with a rating of 4 and 5 become a 1 (positive) and every rating under 4 to become a 0 (negative). The class imbalance for Montreal was 25% negative and 75% positive, and for New York, 24% negative and 76% positive.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/765/1*NpDVsNvifU8I0vBSe1Wb_A.png" /></figure><p>Looking at the review length distribution, there is a very similar distribution across the board for each star in each city, which gave me confidence that the model would be less biased.</p><p>Once I binarized the positive and negative reviews, I divided them into their own data frames with just the text column. Each review was then tokenized using TFIDF (short for term frequency-inverse document frequency) and 1-grams which were giving the most consistent results and clear topics. For topic modelling, I tried Latent Semantic Analysis (LSI), Latent Dirichlet Allocation (LDA) and Non-Negative Matrix Factorization (NMF). Who comes up with these names?!</p><p>The word weights for the negative review tokens where not very interpretable for LSI, however, I was able to determine the number of topics to use for my modelling based off of the topic strengths.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/894/1*7DGWo3Yg8d2vAv0n7qYN0w.png" /></figure><p>I was hopeful about LDA, but in the end due to my class imbalance and not enough negative review data, the overlap in meaning between positive and negative reviews made it difficult to make sense of the topics, so in the end I decided to go with NMF, which gave the most interpretable results and distinct topics.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/deb99e5fd937670520fe4d3167cc917e/href">https://medium.com/media/deb99e5fd937670520fe4d3167cc917e/href</a></iframe><p>I moved forward with two different sets of topics for negative and positive reviews. This differed between Montreal and New York, but not by much.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/250b0e05f084b13bb74c57c90b012ca0/href">https://medium.com/media/250b0e05f084b13bb74c57c90b012ca0/href</a></iframe><p>The process going forward consisted of mapping all of the reviews to topics, normalizing the topic weights to sum up to 1, and averaging the topic distributions for all reviews of a bagel shop to map that topic to the restaurant.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ixvZXgymEoWDwCvvIF73FA.png" /></figure><p>The process is seen clearly with my interactive Flourish visualization you can see here:</p><iframe src="https://flo.uri.sh/visualisation/6116741/embed" width="700" height="575" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d5958b235ce6ac3ff3cceea7e84f7e9/href">https://medium.com/media/0d5958b235ce6ac3ff3cceea7e84f7e9/href</a></iframe><iframe src="https://flo.uri.sh/visualisation/6124425/embed" width="700" height="575" frameborder="0" scrolling="no"><a href="https://medium.com/media/d398e8fa6de70959ed629f5d998c582a/href">https://medium.com/media/d398e8fa6de70959ed629f5d998c582a/href</a></iframe><iframe src="https://flo.uri.sh/visualisation/6162285/embed" width="700" height="575" frameborder="0" scrolling="no"><a href="https://medium.com/media/54c968ba78664e06e99b6e69891e4c2b/href">https://medium.com/media/54c968ba78664e06e99b6e69891e4c2b/href</a></iframe><iframe src="https://flo.uri.sh/visualisation/6162243/embed" width="700" height="575" frameborder="0" scrolling="no"><a href="https://medium.com/media/4c7ed09e086e53f65478710b2a7bfab9/href">https://medium.com/media/4c7ed09e086e53f65478710b2a7bfab9/href</a></iframe><p>The newly modelled data is used to show topic strengths for both positive and negative reviews.</p><p>I set out to determine which city has the better bagel but as I spent more time with the data and started my modelling, it became clear to me that review data doesn’t really reflect the question of which city has the better bagel, as they are too conflated with other things like the various topics seen in my topic modelling.</p><p>Despite not being able to say which city has the better bagel after my investigation, I believe that with this new information, instead of relying on numeric attributes already existing on Yelp like star rating and number of reviews, my findings offers a look into the actual text data which has been converted into meaningful insights about the bagel experience at the given restaurant.</p><p>My next steps with this project are to look more into the customer metadata on a larger Yelp dataset to analyze the different review behaviour between locals and tourists. It would also be helpful to spend more time working on better tokenization (with more data) to see a greater variety of topics. Because of the overlap in meaning between negative and positive reviews, I could also try creating a singular topic space for all reviews instead of just positive and negative.</p><p>View the code on my GitHub: <a href="https://github.com/setadrift/capstone_1">https://github.com/setadrift/capstone_1</a></p><p>Thanks,</p><p>Duncan Anderson<br>LinkedIn: <a href="https://www.linkedin.com/in/duncan-kg-anderson/">https://www.linkedin.com/in/duncan-kg-anderson/</a><br>Twitter: @duncanand</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ffc4ef65a76f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL Simplified — 10 advanced practice problems and solutions covering most SQL concepts]]></title>
            <link>https://duncan-kg-anderson.medium.com/sql-simplified-10-advanced-practice-problems-and-solutions-covering-most-sql-concepts-61fb96e7ec5a?source=rss-13c8859ee168------2</link>
            <guid isPermaLink="false">https://medium.com/p/61fb96e7ec5a</guid>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Duncan Anderson]]></dc:creator>
            <pubDate>Thu, 08 Jul 2021 20:08:03 GMT</pubDate>
            <atom:updated>2021-07-13T01:13:27.664Z</atom:updated>
            <content:encoded><![CDATA[<h3>SQL Simplified — 10 advanced practice problems and solutions covering most SQL concepts</h3><p>Using SQL effectively is a critical skill for any data scientist and is one of the most tested in interviews. In our own attempts at improving at this skill, we found an incredibly useful article by Mrinal Gupta <strong>(</strong><a href="https://towardsdatascience.com/10-problems-to-practice-almost-all-sql-concepts-37545e7c5219"><strong>original article linked here</strong></a><strong>)</strong> that covered 10 high quality questions with most SQL concepts tested in interviews. After the article, however difficult a practice question we tried, it paled in comparison to those 10 and it was surprising to see the sudden jump in our abilities! To best benefit from the aforementioned article, we had to do 3 preparatory steps:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WvKKTfHj6SRcPrIXKypdBg.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/@shvetsa?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Anna Shvets</a> from <a href="https://www.pexels.com/photo/person-in-blue-denim-jeans-using-macbook-3987114/?utm_content=attributionCopyText&amp;utm_medium=referral&amp;utm_source=pexels">Pexels</a></figcaption></figure><ol><li>Prepare practice data (In the original article, the training data wasn’t readily available. We had to manually make SQL tables &amp; enter in the data to try our test queries).</li><li>Write down our step by step thought process (Some answer queries were very intimidating at first glance. It took a long while to figure out the step by step thought process behind them).</li><li>Outdated keywords replacement (Some SQL keywords used in the original article were outdated possibly due to version changes. We had to find replacements for the current version).</li></ol><p>In this article, we share our preparatory steps (with the 10 key questions) for a smoother learning experience. We hope you find it as beneficial as we did!</p><p><strong><em>Q1. Given two tables below, write a query to display the comparison result (higher/lower/same) of the average salary of employees in a department to the company’s average salary.</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36b190f5d8f429989ec909925ad81607/href">https://medium.com/media/36b190f5d8f429989ec909925ad81607/href</a></iframe><p>First, let’s create the necessary tables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f8c4ee40992b3b062e7aa18cdffb46d/href">https://medium.com/media/9f8c4ee40992b3b062e7aa18cdffb46d/href</a></iframe><p>Now that we have our tables set up, let’s do the initial JOIN.</p><p><strong>Step #1. </strong>JOIN</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ecbbb30f92596c7189bee2dcc1bbe42f/href">https://medium.com/media/ecbbb30f92596c7189bee2dcc1bbe42f/href</a></iframe><p>Now the two tables are joined together using the shared ‘employee_id’ column:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7e0e59a84acc1ea20a0e25cd251ea1ac/href">https://medium.com/media/7e0e59a84acc1ea20a0e25cd251ea1ac/href</a></iframe><p><strong>Step #2. </strong>Now that we have the two tables joined, consider the intermediary columns needed for our final table. Select and engineer those.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7898572b2b6d09540563af3312d8a15a/href">https://medium.com/media/7898572b2b6d09540563af3312d8a15a/href</a></iframe><p>With this step, we achieve a few things. By using the DATE_FORMAT keyword, we allow our window functions to be averaged by month. Then we alias these statements for clarity. Here is the output of the above statement. If that seems a bit unclear, we suggest quickly Googling what Window Functions are, because they will show up a lot in the proceeding questions.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8be315592edfe826284db68726dc7b4/href">https://medium.com/media/e8be315592edfe826284db68726dc7b4/href</a></iframe><p><strong>Step #3. </strong>Let’s convert our existing code into a sub-query &amp; pipe into a blank final query.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c43002b2950e4b246070d560b6b3b42/href">https://medium.com/media/3c43002b2950e4b246070d560b6b3b42/href</a></iframe><p>We are creating the temporary tables using the WITH keyword, as this allows us to query aliases. Here’s the output from the above code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d46d4fb5433be0ea1487cb3f45dae66e/href">https://medium.com/media/d46d4fb5433be0ea1487cb3f45dae66e/href</a></iframe><p><strong>Step #4. </strong>With that, all that’s left is to do the relevant selections &amp; feature engineering for our final display. We’ll use the DISTINCT keyword to remove the duplicate columns from the result set and set our conditions using the CASE WHEN clause.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2ad411c9e42e354d744ee154b75fe23/href">https://medium.com/media/e2ad411c9e42e354d744ee154b75fe23/href</a></iframe><p>Now we can display the comparison result (higher/lower/same) of the average salary of employees in a department to the company’s average salary.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b2b4a5542e490845b6e34254e3023e1/href">https://medium.com/media/6b2b4a5542e490845b6e34254e3023e1/href</a></iframe><p><strong><em>Q2.</em></strong> <strong><em>Write an SQL query to report the students (student_id, student_name) being “quiet” in ALL exams. A “quiet” student is the one who took at least one exam and didn’t score neither the high score nor the low score.</em></strong></p><p><strong><em>Solution 2:</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c7bf33c17882d278e1ff1fba532ce7c5/href">https://medium.com/media/c7bf33c17882d278e1ff1fba532ce7c5/href</a></iframe><p>First, let’s create the necessary tables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10066d02818090928550bcfb3e5a24d1/href">https://medium.com/media/10066d02818090928550bcfb3e5a24d1/href</a></iframe><p><strong>Step #1. </strong>Join (We do an inner join since no interest in children with no exams)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02cdb85c18167ead2536d5a6d5493880/href">https://medium.com/media/02cdb85c18167ead2536d5a6d5493880/href</a></iframe><p>The tables are now joined on student_id.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2056123d5abf86ecd1bfe74334cd417/href">https://medium.com/media/a2056123d5abf86ecd1bfe74334cd417/href</a></iframe><p><strong>Step #2. </strong>Next we create all the intermediary columns needed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aad24839190a686a8eda8e2c0ab8b1cf/href">https://medium.com/media/aad24839190a686a8eda8e2c0ab8b1cf/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf4d9a7877e9f91c29e3f117db6b38b0/href">https://medium.com/media/bf4d9a7877e9f91c29e3f117db6b38b0/href</a></iframe><p><strong>Step#3. </strong>Convert previous query to a sub-query so we can access intermediary columns.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7fc88c50d530b1d5a95f3f9cef1e27df/href">https://medium.com/media/7fc88c50d530b1d5a95f3f9cef1e27df/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e5afcca31a52dc3d848272dfe4d68b9/href">https://medium.com/media/5e5afcca31a52dc3d848272dfe4d68b9/href</a></iframe><p><strong>Step#4. </strong>Consider final output &amp; use relevant ‘selecting’ &amp; ‘filtering’ to produce.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fff1f6b337a4069b860278b1dae21a8a/href">https://medium.com/media/fff1f6b337a4069b860278b1dae21a8a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74a3e2019036d63f09fbdf85f76f9422/href">https://medium.com/media/74a3e2019036d63f09fbdf85f76f9422/href</a></iframe><p><strong><em>Q3. Write a query to display the records which have 3 or<br>more consecutive rows and the amount of people more than 100 (inclusive).</em></strong></p><p><strong><em>Solution 3:</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/54e3d8fda1be49603ae1bd78319a072b/href">https://medium.com/media/54e3d8fda1be49603ae1bd78319a072b/href</a></iframe><p>First, let’s create the necessary tables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36e0e8aa66489dacd7c4a14529df08c4/href">https://medium.com/media/36e0e8aa66489dacd7c4a14529df08c4/href</a></iframe><p><strong>Step#1.</strong> Make a column that identifies 100+ streaks.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8cec3029ee38fb8e3307ba0a58a51c3b/href">https://medium.com/media/8cec3029ee38fb8e3307ba0a58a51c3b/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e299de3165f93b363a0ee8794d2bbc4/href">https://medium.com/media/3e299de3165f93b363a0ee8794d2bbc4/href</a></iframe><p><strong>Step#2.</strong> Select other relevant intermediary columns &amp; make into a sub-query so we can access them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9abcd66bd834579b856b30f945d832ce/href">https://medium.com/media/9abcd66bd834579b856b30f945d832ce/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c61f361cda7d1329941377e30115561/href">https://medium.com/media/8c61f361cda7d1329941377e30115561/href</a></iframe><p><strong>Step#3.</strong> We then make a table that counts the streak size from t1 for each streak.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17633e74e0a08d6e4c7fc4863927e243/href">https://medium.com/media/17633e74e0a08d6e4c7fc4863927e243/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/92fd4177fc3cb63764610a32ab83ad3c/href">https://medium.com/media/92fd4177fc3cb63764610a32ab83ad3c/href</a></iframe><p><strong>Step#4.</strong> We filter out only streaks with 3+ streak_size.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1997112a4c897986727db96b6defc3d5/href">https://medium.com/media/1997112a4c897986727db96b6defc3d5/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc9287c54b8c732e01bbfe18e6f9aacf/href">https://medium.com/media/bc9287c54b8c732e01bbfe18e6f9aacf/href</a></iframe><p><strong>Step#5.</strong> Let’s inner join this to t1 so we get all the entries of the shortlisted streak_num.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1eaabf378bcb9a55b52efe8551d63b5a/href">https://medium.com/media/1eaabf378bcb9a55b52efe8551d63b5a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4de38c8ac6ed6af67807eafacb5a3765/href">https://medium.com/media/4de38c8ac6ed6af67807eafacb5a3765/href</a></iframe><p><strong>Step# 6.</strong> Then make relevant selections as needed for final output.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6147c5353cce199b42e2846cc31d9c3/href">https://medium.com/media/d6147c5353cce199b42e2846cc31d9c3/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69edadeeab182a8b49f49b603781bf1f/href">https://medium.com/media/69edadeeab182a8b49f49b603781bf1f/href</a></iframe><p><strong><em>Q4. Write an SQL query to find how many users visited the bank and didn’t do any transactions, how many visited the bank and did one transaction and so on.</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06082624880c4c6572affc8d02409a97/href">https://medium.com/media/06082624880c4c6572affc8d02409a97/href</a></iframe><p>First, let’s create the necessary tables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbfb6e8eb76f82510f015ebed4249328/href">https://medium.com/media/fbfb6e8eb76f82510f015ebed4249328/href</a></iframe><p><strong>Step#1.</strong> Let us start off by finding the # of visits by day by user_id.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/876cd0ad8a411103bfa1e8ec7e9a8daf/href">https://medium.com/media/876cd0ad8a411103bfa1e8ec7e9a8daf/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/855f4a3fbe57e03cff47e76eb715ffb6/href">https://medium.com/media/855f4a3fbe57e03cff47e76eb715ffb6/href</a></iframe><p><strong>Step#2.</strong> We then calculate the number of transactions by day by user_id.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ba3d0a60c8647582ee08b7c6620ff2f5/href">https://medium.com/media/ba3d0a60c8647582ee08b7c6620ff2f5/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ac36cda7c3fb740d3d7440b6bac5a60/href">https://medium.com/media/5ac36cda7c3fb740d3d7440b6bac5a60/href</a></iframe><p><strong>Step#3.</strong> Let us do a left join so we can maintain total visits &amp; wrangle data together.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f778548b4997fb2d5d7950ce1af0b51/href">https://medium.com/media/0f778548b4997fb2d5d7950ce1af0b51/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/558be24f1dcbd7211d6afed0e53d00c9/href">https://medium.com/media/558be24f1dcbd7211d6afed0e53d00c9/href</a></iframe><p><strong>Step#4.</strong> We select only date column, num_visits column &amp; replace null num_trans by 0.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72287ab52f26c43734c65ae9c951f2f0/href">https://medium.com/media/72287ab52f26c43734c65ae9c951f2f0/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5cf3614686c719e140a5d39c78a4fec/href">https://medium.com/media/e5cf3614686c719e140a5d39c78a4fec/href</a></iframe><p><strong>Step#5.</strong> Let’s convert our previous result into a sub query then make appropriate bins using another query t2. We will have to use the RECURSIVE clause for t2 to correctly make it function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46df9bd343bbf02d6a683c24800864ef/href">https://medium.com/media/46df9bd343bbf02d6a683c24800864ef/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/800db33dc5ae4c5db1f06e2514c75a74/href">https://medium.com/media/800db33dc5ae4c5db1f06e2514c75a74/href</a></iframe><p><strong>Step#6.</strong> Join t1 &amp; t2 appropriately, and we have our desired output!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/73bea0940edd74081c6811073607a134/href">https://medium.com/media/73bea0940edd74081c6811073607a134/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fd4ae51f4c00838e1202a56b6b2e9e2/href">https://medium.com/media/8fd4ae51f4c00838e1202a56b6b2e9e2/href</a></iframe><p><strong><em>Q5. Write an SQL query to generate a report of period_state for each continuous interval of days in the period from 2019–01–01 to 2019–12–31.</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2ef4b44685f05116e7c7a0eb97c577ad/href">https://medium.com/media/2ef4b44685f05116e7c7a0eb97c577ad/href</a></iframe><p>Let’s start off by making our tables.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74397a2da5b620e6effee297b0f0581c/href">https://medium.com/media/74397a2da5b620e6effee297b0f0581c/href</a></iframe><p><strong>Step#1.</strong> Let’s make a column that records each streak in the year 2019. State 1 added to simply signify success state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/702ba75d4269c725a107ab0df13d6082/href">https://medium.com/media/702ba75d4269c725a107ab0df13d6082/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fec540993097fccd8cb631510eab585a/href">https://medium.com/media/fec540993097fccd8cb631510eab585a/href</a></iframe><p><strong>Step#2.</strong> Let’s find the start &amp; end date of each streak.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2179b793d2c80f627b75bfb3c48ee2ee/href">https://medium.com/media/2179b793d2c80f627b75bfb3c48ee2ee/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae1c07c48455bbf3cd41e93490bfabd6/href">https://medium.com/media/ae1c07c48455bbf3cd41e93490bfabd6/href</a></iframe><p><strong>Step#3.</strong> Let’s repeat the process for failed state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5733cb1b21ed40212e2f31bdc71d892a/href">https://medium.com/media/5733cb1b21ed40212e2f31bdc71d892a/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/02e85e00314c21e2375641021d62a649/href">https://medium.com/media/02e85e00314c21e2375641021d62a649/href</a></iframe><p><strong>Step#4.</strong> Let’s combine t1 &amp; t2 using UNION ALL &amp; order by date.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3dc09455f65edb50b069ebfdf3fd7338/href">https://medium.com/media/3dc09455f65edb50b069ebfdf3fd7338/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/175053d5be4daec1908e2e60b3ca08c2/href">https://medium.com/media/175053d5be4daec1908e2e60b3ca08c2/href</a></iframe><p><strong>Step#5.</strong> Let’s use case when to replace 1 &amp; 0 with succeeded &amp; failed, and we have our desired output!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0074a042308999255d8bd6e2ba9dbc1c/href">https://medium.com/media/0074a042308999255d8bd6e2ba9dbc1c/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a333fa914a41137fdfa9af9680c0cc3/href">https://medium.com/media/3a333fa914a41137fdfa9af9680c0cc3/href</a></iframe><p><strong><em>Q6. Write an SQL query to report how many units in each category have been ordered on each day of the week.</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17ab23ec4635b285e677af04057eb515/href">https://medium.com/media/17ab23ec4635b285e677af04057eb515/href</a></iframe><p>First, create the schema and tables:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a346ae977ff21930590e442fc2415835/href">https://medium.com/media/a346ae977ff21930590e442fc2415835/href</a></iframe><p><strong>Step #1. </strong>Join the tables with a RIGHT JOIN, as every item will not have an order.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7798190be4270d51f4b4416a3bfa5a78/href">https://medium.com/media/7798190be4270d51f4b4416a3bfa5a78/href</a></iframe><p>If you look at the inputted values for the results table, we see that ‘T-Shirt’ has no values, but all of our items in the table are there.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19b63067f5d013aa9462d8e3afb26aa5/href">https://medium.com/media/19b63067f5d013aa9462d8e3afb26aa5/href</a></iframe><p><strong>Step #2.1. </strong>Let’s create a temporary table and join the necessary columns from the orders and items tables and test with the 1st day of the week, Monday.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6c21555f56474f6bc242ef8c771271c6/href">https://medium.com/media/6c21555f56474f6bc242ef8c771271c6/href</a></iframe><p>For the output, we should expect to see the sum of all items just for the Monday.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec7a032e3828e84f4b06ab251a2f142f/href">https://medium.com/media/ec7a032e3828e84f4b06ab251a2f142f/href</a></iframe><p><strong>Step #2.2. </strong>Great, now that we know that works, let’s fetch all the days of the week.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/52c6b65596d4f65606bf115a6a9783ae/href">https://medium.com/media/52c6b65596d4f65606bf115a6a9783ae/href</a></iframe><p>Output:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a50de290aab236553af1190e673a704a/href">https://medium.com/media/a50de290aab236553af1190e673a704a/href</a></iframe><p><strong>Step #3. </strong>What’s wrong with the above output? We don’t want to see multiple item_categories appearing, so let’s use DISTINCT on the item_category column.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46789aa2d48209ca1e65f7422fcc96ca/href">https://medium.com/media/46789aa2d48209ca1e65f7422fcc96ca/href</a></iframe><p>Now we can see how many units in each category have been ordered on each day of the week.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4af243733ce9481ecaab0f6dfbe1916/href">https://medium.com/media/f4af243733ce9481ecaab0f6dfbe1916/href</a></iframe><p><strong><em>Q7. Write an SQL query to find employees who earn the top three salaries in each of the department. For the above tables, your SQL query should return the following rows (order of rows does not matter).</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b266562659942cd0542ec35507afb661/href">https://medium.com/media/b266562659942cd0542ec35507afb661/href</a></iframe><p>First let’s create the schema and tables:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/224bb60f488705c9a5f2b5a76098d730/href">https://medium.com/media/224bb60f488705c9a5f2b5a76098d730/href</a></iframe><p><strong>Step #1. We’ll need to JOIN the two tables on the respective id’s.</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e43387d7698c24cfbea6c0378775ad5a/href">https://medium.com/media/e43387d7698c24cfbea6c0378775ad5a/href</a></iframe><p>We have the following results to work with. What data do we care about from our joined table?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf76e5a3924fc7365fdc294c1843bca9/href">https://medium.com/media/bf76e5a3924fc7365fdc294c1843bca9/href</a></iframe><p><strong>Step #2. </strong>Well, we’ll need the employee name, department name, and salary</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cf67a5abe568a3069da9dc92037da01e/href">https://medium.com/media/cf67a5abe568a3069da9dc92037da01e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd59c434eac552ea217fd08472b6185e/href">https://medium.com/media/bd59c434eac552ea217fd08472b6185e/href</a></iframe><p><strong>Step #3. </strong>Great, so we have the info we’ll need in the end. Let’s turn our existing code into a subquery, and set a condition to return only top 3 values.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24ecac808b554f47bce25dc682058e66/href">https://medium.com/media/24ecac808b554f47bce25dc682058e66/href</a></iframe><p>In IT dept, Max earns the highest salary, Randy and Joe have a tie for second, and Will earns the third highest. There are only two employees in the Sales department, Henry earns the highest salary while Sam earns the second highest salary.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a968f9e88e29f79d4626e7d73ef64d64/href">https://medium.com/media/a968f9e88e29f79d4626e7d73ef64d64/href</a></iframe><p><strong><em>Q8. Write an SQL query to compute moving average of how much customer paid in a 7 days window (current day + 6 days before) .</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/356fe3fb6aea1ce5ae41e0e2e1599765/href">https://medium.com/media/356fe3fb6aea1ce5ae41e0e2e1599765/href</a></iframe><p>First let’s create the schema and ‘customers’ table we need.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d7c5b1f7769977aa80eeb4df7a8e9e3/href">https://medium.com/media/2d7c5b1f7769977aa80eeb4df7a8e9e3/href</a></iframe><p><strong>Step #1. </strong>Let’s start by aggregating the amount with SUM and grouping by visited_on in order to see the window of time</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/152d02e7d4ac257c4505f1b01226731d/href">https://medium.com/media/152d02e7d4ac257c4505f1b01226731d/href</a></iframe><p>So now we can see the amount of customers who visited each day.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5dbe101bb0cf3a79e0db2b186501c239/href">https://medium.com/media/5dbe101bb0cf3a79e0db2b186501c239/href</a></iframe><p><strong>Step #2. </strong>We need to find the rolling average. We want to know the total amount during this time period, as well as the average amount. Each 7 day period out of the 10 days should equate to 4 possibilities per day.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c42f44ae9f117c64acad5ca2ebfc9e20/href">https://medium.com/media/c42f44ae9f117c64acad5ca2ebfc9e20/href</a></iframe><p><strong>Step #3. </strong>In order to SELECT visited_on in this statement, we’ll need to make our first query a temporary table, as well as this one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25d2cd5d3a852b068130042a6e78f2e1/href">https://medium.com/media/25d2cd5d3a852b068130042a6e78f2e1/href</a></iframe><p>Now we can see the moving average and the total amount of how much a customer paid in a 7 day window (current day + 6 days before).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4b948bd321a83e399dd7edf12c8d30eb/href">https://medium.com/media/4b948bd321a83e399dd7edf12c8d30eb/href</a></iframe><p><strong><em>Q9. Write a query to find the shortest distance between these points rounded to 2 decimals.</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f56f88157d256b07e9ae836471c0c00a/href">https://medium.com/media/f56f88157d256b07e9ae836471c0c00a/href</a></iframe><p>First let’s create the table:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bed19a8ce4290441ed5393090232e9da/href">https://medium.com/media/bed19a8ce4290441ed5393090232e9da/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16ffcb934a15ce57c8a21552c1223459/href">https://medium.com/media/16ffcb934a15ce57c8a21552c1223459/href</a></iframe><p><strong>Step#1. </strong>First let’s use a CROSS JOIN, which is a type of join that generates a paired combination of each row of the first table with each row of the second table.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c062687d4487d25008075cfe38dc714/href">https://medium.com/media/2c062687d4487d25008075cfe38dc714/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d22b89a90d8ce92e62a85bc52305594b/href">https://medium.com/media/d22b89a90d8ce92e62a85bc52305594b/href</a></iframe><p><strong>Step#2. </strong>Now that we have the cross join taken care of, let’s find the shortest distance between each coordinate from every other.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e28bc2876bac8df88e9389eb0ad6d899/href">https://medium.com/media/e28bc2876bac8df88e9389eb0ad6d899/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/445a3b8a6837fd5431f80174f1838c8e/href">https://medium.com/media/445a3b8a6837fd5431f80174f1838c8e/href</a></iframe><p><strong>Step#3. </strong>We can clearly see from the output that the shortest distance is 1, but to keep things clean, we’ll finish this question off by sorting by ASC and limiting 1 so only the shortest distance is displayed.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/111a2116c786cb33b83f1db8b14fff7b/href">https://medium.com/media/111a2116c786cb33b83f1db8b14fff7b/href</a></iframe><p>The above query will output 1, which is what we expected.</p><p><strong><em>Q10. Write an SQL query to find all numbers that appear at least three times consecutively.</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f604c8ffbef4946bafabf09daa9e9267/href">https://medium.com/media/f604c8ffbef4946bafabf09daa9e9267/href</a></iframe><p><strong>Step#1. </strong>Since we’re interested in getting the consecutive numbers to see which appear 3x in a row, we’ll use the LAG function to fetch data from the previous row, and LEAD for the subsequent row.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/99fa35f0799e8fa24e9cb6f7ec4065a8/href">https://medium.com/media/99fa35f0799e8fa24e9cb6f7ec4065a8/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/210dc9ec5f12335a663e0b5c270eb3c8/href">https://medium.com/media/210dc9ec5f12335a663e0b5c270eb3c8/href</a></iframe><p>Notice that the first row for prev is null, since the row from which the value (num) should be obtained doesn’t exist. The inverse is true for ‘next’.</p><p><strong>Step#2. </strong>Let’s turn our SELECT statement into subquery to enable WINDOW function condition.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66f2be9b955caf8a02ca3665518f46e1/href">https://medium.com/media/66f2be9b955caf8a02ca3665518f46e1/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa47c8cc38ac21abb2a410ec2221d1b2/href">https://medium.com/media/aa47c8cc38ac21abb2a410ec2221d1b2/href</a></iframe><p><strong>Step#3. </strong>Let’s finish this off by using DISTINCT to return the numbers that meet our condition (as we see, it’s simply 1 from the previous query).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5441fd8d9bb9ba5492c7e90c0d630e2/href">https://medium.com/media/a5441fd8d9bb9ba5492c7e90c0d630e2/href</a></iframe><p>We hope you enjoyed our article and are feeling much more confident about SQL. For next steps, we recommend just free-styling some medium &amp; high difficulty data science SQL interview questions. <a href="https://platform.stratascratch.com/">Stratascratch</a> has several such questions available in their free tier (at least at the time of writing this article). Then after that, considering projects where you can apply your new found SQL skills. Thanks again for engaging with our work. It makes us happy to know it helped someone out, like our past-selves!</p><p>Sincerely,<br>Duncan Anderson &amp; Ammar Khan.<br>Ammar’s <a href="https://www.linkedin.com/in/ammar-masood-khan/">LinkedIn</a><br>Duncan’s <a href="https://www.linkedin.com/in/duncan-kg-anderson/">LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61fb96e7ec5a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interview with a Data Scientist about Data Journalism]]></title>
            <link>https://duncan-kg-anderson.medium.com/interview-with-a-data-scientist-about-data-journalism-64237bdb4f7e?source=rss-13c8859ee168------2</link>
            <guid isPermaLink="false">https://medium.com/p/64237bdb4f7e</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[data-journalism]]></category>
            <category><![CDATA[journalism]]></category>
            <dc:creator><![CDATA[Duncan Anderson]]></dc:creator>
            <pubDate>Thu, 08 Oct 2020 21:32:50 GMT</pubDate>
            <atom:updated>2020-10-08T21:42:28.931Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HYAd1aal5Et1A-Qzs6VAtQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@nasa?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">NASA</a> on <a href="https://unsplash.com/s/photos/data-visualization?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>For almost a year, I’ve been very interested in transforming my traditional practice of journalism into a more data oriented approach. What better way to do this than to speak directly with data scientists and get some advice from them, right? Indeed.</p><p><strong>Can you please just start by saying your name, a surprising fact about yourself, and what it is you do exactly with U Squared?</strong></p><p>My name is Daniel Usvyat. One surprising fact is where my father is from. Most people have never heard of the place. It’s kind of an unusual place, politically charged, recognized as a country by only six other countries. So he’s originally from the Soviet Union and the independent state is Abkhazia.</p><p>Before U SQUARED, I used to work at a management consulting company. I did that for a few years but after 1 year I started to get sick of it. There is more to life than living out of a suitcase, traveling week by week. I was learning lots of skills but also building up a client base, and so I wanted to do it on my own on a smaller scale. So I went into freelance consulting in data science, and with like-minded people to myself, Istarted talking about creating some kind of brand, which we could do projects from and that’s kind of how U SQUARED was born. So we’ve grown, worked with lots of interesting clients in media, finance, insurance, and more recently the public sector. It’s been an interesting story from the get go to now. I’ve really enjoyed it. I like working on challenging projects and feeling rewarded by giving clients what we do best.</p><p><strong>How many clients have you worked with?</strong></p><p>Big clients? Around 6 projects. I’ve also worked with smaller startups, advising them on how they can use AI, and advisory or proof of concepts.</p><p><strong>Have you partnered or worked with any data journalists or news organizations?</strong></p><p>Recently I did a project with The Guardian — I didn’t work directly with journalists per se, but I was doing some stuff with NLP (Natural Language Processing), which allows them to essentially extract more information from news articles. Before this, the main thing we were looking at was keyword tags, which were human derived. Maybe it was something like “Sport” or “Brexit” but that was used to kind of cluster or group articles together. We were finding more intelligent ways of extracting additional meta-data from articles to use as a way to find commonalities in articles and understand what people like, and what drives engagement around journalism. So that was actually a really cool and interesting project.</p><p><strong>How do you see machine learning and AI influencing journalism in the future?</strong></p><p>I’d say it’s more data mining than AI/ML. The Washington Post a few months ago was showing the spread of COVID in an interactive way. Using some kind of simulation to show like, if you change this, how would that likely imprint a spread of the virus. And what I like about the Guardian is that they do good visualizations on elections and broad events.</p><p><strong>For a journalist like myself looking to become equipped with some tools to produce solid data journalism, how would you structure a self-learning data science environment? So far I’ve been learning Python and I’m wondering what I should learn next.</strong></p><p>Understanding basic data retrieval methods like how to scrape information from websites is essential. If you want to go through Google, different news sources and collect info from different topics, how would you do that? I’d say the best way to do it is using Python. It’s probably the easiest entry point and easiest thing to learn. Start with the problem and ask yourself — “What am I trying to solve?” And then ask… “Is that achievable with python through existing packages?” Probably. Then understand what package you need to use, which is as simple as going online to see if people are sharing their work and have done something similar before. Take that code and look at how you can adapt it and solve your problem and bend it to your use case and needs.</p><p>This allows you to save a lot of time rather than building from stuff that exists. No point in repeating the same thing again. So once you’ve done that, it’s a question of doing some very small collections. So if you want to collect info on the current US election, you’d map out all the different news sources you’re interested in and try it on this and see how it works. If it works, great, if not, look at what’s going wrong and adapt it. After that, you’ll look at spinning it out across everything and once you’ve done that, you look at what you have to do next.</p><p>I like to make it very simple and not get too caught up on the details. So thinking — “What do I have to do first? Ok, I’ll do that. What next? Okay I’ll do that.” Otherwise you can get lost in the cycle in trying to find the perfect solution and never really starting. So I guess to start, start small and build up and learn as you go along.</p><blockquote>You don’t need a PHD in math or data science, you just need a willingness to learn, curiosity and resilience — sometimes you’re going to have a lot of bugs and it might be quite painful, and you might feel like you have no clue what you’re doing but you may suddenly have a breakthrough and keep on pushing on.</blockquote><p>Sounds a bit ironic, but I’m a bit opposed to studying data science as a degree. I think it’s not really necessary — all of the info is online for free. You can do your own custom projects and get a job in data science. I think there’s too much emphasis on getting a degree. I’m a big advocate on personal projects and learning that way, as opposed to academic learning.</p><p><strong>The market is exploding with data viz charts and visuals — some good, some not so good. What are the common pitfalls of these visualizations, and what should data journalists keep in mind when creating a data visualization.</strong></p><p>That’s a very good point — It’s very important to not present information in misleading ways. So it’s always good to have transparency in the information that’s presented so when you are presenting these charts it needs to be very clear how they should be used. It should be user friendly, and shouldn’t need instructions… You should just be able to look at it and not misunderstand information. I guess, my opinion in the way with journalism is, is that there is always some bias — like the newspaper’s political viewpoint. I’ve never seen a newspaper that’s not biased in some way, whether it’s good or bad, it depends on whoever is looking at it. The Guardian is a more left leaning, more liberal, and has a more present view point and it represents younger generations on the viewpoint of the world which is good in my opinion. If you read some newspapers on the right wing it’s a bit more conservative, a bit cringe sometimes to read those, and feels like a kind of past generation representation of the world which doesn’t really exist anymore.</p><blockquote>So I feel like when it comes to AI and ML the same bias can be perpetrated on the newspapers’ inherent bias they have, or whatever news source they produce.</blockquote><p><strong>Is data journalism less biased than traditional journalism?</strong></p><p>Generally I would like to say that it is, but from experience I’ve seen it sometimes may not be. I feel like you can always take data sets and adjust it to suit your view point. This is what politicians do all the time — they take the same data set and make it say two different things because they have two different metrics or two cuts of data. I’ve heard people often say stuff like “We want to show this, and if we take this measure will it still make sense…?” I mean, you’re just massaging the numbers now, so it’s always down to the beholder of the info, the analyst — or on the receiving end, in consulting you have a client who is paying for the project, they may have a certain belief and unless you want to get fired, you’re going to find a way to support that belief. It sounds bad and that’s the reality unfortunately. I like to think that if you’re working with someone more open minded and with less of an ego, than hopefully what you’re saying will be true but unfortunately people have egos and agendas.</p><p><strong>How important is it to link back to the original source and show we got the data?</strong></p><p>In my opinion it’s good to have a high level understanding of how the data is processed and where it’s coming from. If I’m honest, the main important point is where the data comes from first of all, because if it comes from a suspect source or unreliable source that’s the first point of failure. After that, you have all the processing and data wrangling and the next step is what kind of average are you taking? The mean, or the medium? That’s quite important and then of course, understanding the core statistics of the data. What is the spread? Is it evenly distributed? This is quite important to understand. After that, they’re doing some kind of prediction from it, and they’re kind of changing some kind of interpretation.</p><p>If I have some time series data with financial news I’m using it to extrapolate what’s going to happen in 3 months time. If you don’t understand what’s done to get that result, then it can be dangerous. You can say something completely insane that doesn’t actually make sense. Just because somebody has made a prediction, but come up with a terrible prediction, understanding the efficacy of what’s being done is important and necessary.</p><p><strong>Do you foresee data becoming more readily available from businesses, governments and organizations?</strong></p><p>I think it hopefully will, because it would give people more ability have more understanding of what’s going on. You have, which is an interesting source, google data which has a bunch of random data sets you can search through. Google Trends is good to understand what’s trending alongside Twitter and other things especially with news and popularly of incoming trends.</p><p>A growing trend is alternative data, which is basically finding disparate data sets that aren’t used for something and using it to make predictions. So this is quite commonly used now in finance or the stock market as a basic as new sentiment, or age distribution of a country and seeing how that can influence equity prices or the future value of a company. Finding disparate data sets that aren’t used for something and using it to make predictions can be used in journalism to understand what is happening in society around injustice and inequality. You can use all these measures that are out there to get a better picture and not just a simplified measure on gender or race inequality. There’s all sorts of measures you can take in and understand on a deeper level.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64237bdb4f7e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>