<?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 Prateek Shrestha on Medium]]></title>
        <description><![CDATA[Stories by Prateek Shrestha on Medium]]></description>
        <link>https://medium.com/@iprateeksymbolize?source=rss-3659e9952820------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*bcnKb2H6nBvei3iQ</url>
            <title>Stories by Prateek Shrestha on Medium</title>
            <link>https://medium.com/@iprateeksymbolize?source=rss-3659e9952820------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 13:33:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@iprateeksymbolize/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[SQL Project Planning]]></title>
            <link>https://medium.com/@iprateeksymbolize/sql-project-planning-df47971f38d0?source=rss-3659e9952820------2</link>
            <guid isPermaLink="false">https://medium.com/p/df47971f38d0</guid>
            <category><![CDATA[advanced-joins]]></category>
            <category><![CDATA[hackerrank-solutions-sql]]></category>
            <dc:creator><![CDATA[Prateek Shrestha]]></dc:creator>
            <pubDate>Mon, 30 Sep 2024 09:48:25 GMT</pubDate>
            <atom:updated>2024-09-30T09:51:50.795Z</atom:updated>
            <content:encoded><![CDATA[<p>You are given a table, Projects, containing three columns: Task_ID, Start_Date and End_Date. It is guaranteed that the difference between the End_Date and the Start_Date is equal to 1 day for each row in the table.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/568/1*Lurim82glVG53WmwbgG62g.png" /></figure><p>If the End_Date of the tasks are consecutive, then they are part of the same project. Samantha is interested in finding the total number of different projects completed.</p><p>Write a query to output the start and end dates of projects listed by the number of days it took to complete the project in ascending order. If there is more than one project that have the same number of completion days, then order by the start date of the project.</p><p><strong>Sample Input</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/563/1*BulfEbngOz7VLRKptwPLSw.png" /></figure><p><strong>Sample Output</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/276/1*rW_-z9obFaMUjhSWk1OMEw.png" /></figure><p><strong>Explanation</strong></p><p>The example describes following four projects:</p><ul><li>Project 1: Tasks 1, 2 and 3 are completed on consecutive days, so these are part of the project. Thus start date of project is 2015–10–01 and end date is 2015–10–04, so it took 3 days to complete the project.</li><li>Project 2: Tasks 4 and 5 are completed on consecutive days, so these are part of the project. Thus, the start date of project is 2015–10–13 and end date is 2015–10–15, so it took 2 days to complete the project.</li><li>Project 3: Only task 6 is part of the project. Thus, the start date of project is 2015–10–28 and end date is 2015–10–29, so it took 1 day to complete the project.</li><li>Project 4: Only task 7 is part of the project. Thus, the start date of project is 2015–10–30 and end date is 2015–10–31, so it took 1 day to complete the project.</li></ul><p>Solution:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/732/1*FlMeBzQwcKAvM55NWEaaKw.png" /></figure><ul><li>The code creates two sets of dates: t1 contains Start_Date values not present in End_Date, and t2 contains End_Date values not present in Start_Date.</li><li>Each set is assigned a row number for ordering.</li><li>The two sets are joined using their row numbers (r) to pair Start_Date and End_Date.</li><li>The final result is sorted by the duration End_Date - Start_Date (smallest to largest) and then by Start_Date.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df47971f38d0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Top Earners]]></title>
            <link>https://medium.com/@iprateeksymbolize/top-earners-84dbf1ecd520?source=rss-3659e9952820------2</link>
            <guid isPermaLink="false">https://medium.com/p/84dbf1ecd520</guid>
            <category><![CDATA[hackerrank-solution]]></category>
            <category><![CDATA[aggregation]]></category>
            <dc:creator><![CDATA[Prateek Shrestha]]></dc:creator>
            <pubDate>Tue, 24 Sep 2024 11:33:02 GMT</pubDate>
            <atom:updated>2024-09-24T11:34:18.250Z</atom:updated>
            <content:encoded><![CDATA[<p>We define an employee’s total earnings as their monthly <em>salary </em>x <em>months </em>worked and the maximum total earnings in the Employee table. Write a query to find the maximum total earnings for all employees as well as the total number of employees who have maximum total earnings. Then print these values as 2 space-separated integers.</p><p><strong>Sample Input:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*ljRtw3pbDgceAUaQTTZjiw.png" /></figure><p>Solution:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*xB2CfbobezvEp1wU6tJzhA.png" /></figure><ol><li><strong>Main Query</strong>:</li></ol><ul><li>MAX(salary*months): This calculates the maximum total earnings, where the total earnings are derived from multiplying the employee&#39;s salary by the number of months (presumably the number of months they&#39;ve worked).</li><li>COUNT(employee_id): This counts how many employees have total earnings equal to the maximum total earnings.</li><li><strong>FROM Employee</strong>: The data is pulled from the Employee table.</li></ul><p><strong>2. Subquery</strong>:</p><ul><li>(SELECT MAX(salary*months) FROM employee): This subquery calculates the maximum value salary * months for all employees. Essentially, it identifies the highest total earnings for any employee.</li></ul><p>3. <strong>WHERE Clause</strong>:</p><ul><li>WHERE salary*months = (SELECT MAX(salary*months) FROM employee): This condition ensures that only the rows where the employee’s total earnings are equal to the highest total earnings (calculated by the subquery) are considered.</li></ul><p>The query finds the <strong>maximum total earnings</strong> (calculated as salary multiplied by months worked) and counts the number of employees with those maximum earnings. This could be used to identify a company&#39;s top earners and determine if multiple employees share the same top earnings.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=84dbf1ecd520" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Blunder]]></title>
            <link>https://medium.com/@iprateeksymbolize/the-blunder-ba1d9ef4557c?source=rss-3659e9952820------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba1d9ef4557c</guid>
            <category><![CDATA[hackerrank-solutions-sql]]></category>
            <category><![CDATA[aggregation]]></category>
            <dc:creator><![CDATA[Prateek Shrestha]]></dc:creator>
            <pubDate>Tue, 24 Sep 2024 09:38:00 GMT</pubDate>
            <atom:updated>2024-09-24T09:38:00.482Z</atom:updated>
            <content:encoded><![CDATA[<p>Samantha was tasked with calculating the average monthly salaries for all employees in the <strong>EMPLOYEES</strong> table but did not realize her keyboard’s 0 key was broken until after completing the calculation. She wants your help finding the difference between her miscalculation (using salaries with any zeros removed), and the actual average salary.</p><p>Write a query calculating the amount of error (i.e.: actual — miscalculated average monthly salaries), and round it up to the next integer.</p><p><strong>Note:</strong> Salary is per month.</p><p><strong>Sample Input</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*VIvLvFpgynERBM88ZSep1g.png" /></figure><p><strong>Explanation</strong></p><p>The table below shows the salaries without zeros as they were entered by Samantha:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/582/1*BUVGrc145ciir8fr_Ko8OQ.png" /></figure><p>Samantha computes an average salary of 98.00. The actual average salary is 2159.</p><p>The resulting error between the two calculations is</p><p>2159.00–98.00 = 2061.00. Since it is equal to the integer 2061, it does not get rounded up.</p><p><strong>Solution:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*AjYtCE1YlPEDed90BvGotw.png" /></figure><p><strong>1. AVG(SALARY)</strong>:</p><ul><li>This calculates the average salary from the EMPLOYEES table.</li></ul><p><strong>2. REPLACE(SALARY, &quot;0&quot;, &quot;&quot;)</strong>:</p><ul><li>The REPLACE function removes all occurrences of the digit &quot;0&quot; from the SALARY field. For example, if the salary is 1020, it would be transformed into 12. This altered value is used to compute a second average.</li></ul><p><strong>3. AVG(REPLACE(SALARY, &quot;0&quot;, &quot;&quot;))</strong>:</p><ul><li>This calculates the average of the modified salaries, where all 0s have been removed.</li></ul><p><strong>4. AVG(SALARY) - AVG(REPLACE(SALARY, &quot;0&quot;, &quot;&quot;))</strong>:</p><ul><li>This computes the difference between the original average salary and the average of the modified salaries (without the zeros).</li></ul><p><strong>5. CEIL(...)</strong>:</p><ul><li>The CEIL The (ceiling) function rounds the difference <strong>result upward</strong> to the nearest integer, ensuring that any decimal portion of the result is rounded up.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba1d9ef4557c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Type of Triangle]]></title>
            <link>https://medium.com/@iprateeksymbolize/type-of-triangle-dadcd20f57fd?source=rss-3659e9952820------2</link>
            <guid isPermaLink="false">https://medium.com/p/dadcd20f57fd</guid>
            <category><![CDATA[hackerrank-solutions-sql]]></category>
            <dc:creator><![CDATA[Prateek Shrestha]]></dc:creator>
            <pubDate>Tue, 24 Sep 2024 07:23:56 GMT</pubDate>
            <atom:updated>2024-09-24T08:08:28.755Z</atom:updated>
            <content:encoded><![CDATA[<p>QUES: Write a query identifying the type of each record in the <strong>TRIANGLES</strong> table using its three side lengths. Output one of the following statements for each record in the table:</p><ul><li><strong>Equilateral</strong>: It’s a triangle with sides of equal length.</li><li><strong>Isosceles</strong>: It’s a triangle with sides of equal length.</li><li><strong>Scalene</strong>: It’s a triangle with sides of differing lengths.</li><li><strong>Not A Triangle</strong>: The given values of A, B, and C don’t form a triangle.</li></ul><p>Each row in the table denotes the lengths of each of a triangle’s three sides.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*qy3Jxd4v83_A1-3qc0PoWQ.png" /></figure><p>Solution 1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/616/1*ie56nnPkgSeCBTGvTQplnA.png" /></figure><p>This SQL query checks whether three given sides (A, B, and C) form a valid triangle and, if so, classify the type of triangle.</p><p>Here&#39;s a breakdown of the logic:</p><ol><li><strong>Outer </strong><strong>CASE Statement</strong>:</li></ol><ul><li><strong>Triangle Validity Check</strong>: The first WHEN clause (A+B &gt; C AND B+C &gt; A AND C+A &gt; B) ensures the sides satisfy the <strong>triangle inequality theorem</strong>. This theorem states that for any valid triangle, the sum of the lengths of any two sides must be greater than the length of the remaining side. If this condition is satisfied, the inner CASE block is executed to classify the triangle.</li><li>If the sides <strong>do not</strong> satisfy the triangle inequality, the result will be &#39;Not A Triangle&#39;.</li></ul><p><strong>2. Inner </strong><strong>CASE Statement</strong> (only executed if it&#39;s a valid triangle):</p><ul><li><strong>Equilateral Triangle</strong>: If all three sides are equal (A = B AND B = C), the triangle is classified as &#39;Equilateral&#39;.</li><li><strong>Isosceles Triangle</strong>: If exactly two sides are equal (A = B OR B = C OR C = A), the triangle is classified as &#39;Isosceles&#39;.</li><li><strong>Scalene Triangle</strong>: If no sides are equal (covered by the ELSE clause in the inner CASE), the triangle is classified as &#39;Scalene&#39;.</li></ul><p>This approach ensures that invalid triangles are ruled out before trying to classify them.</p><p>— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —</p><p>Solution 2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/944/1*u8pS64ApM0bC6gflhDAcLA.png" /></figure><p>This SQL query classifies triangles based on the values of three sides:</p><p>A, B, and C, which are assumed to be columns in the Triangles table. It uses a CASE statement to determine the type of triangle or whether the sides do not form a triangle at all:</p><ul><li><strong>Equilateral Triangle</strong>: If all three sides are equal (A = B AND B = C), it returns &#39;Equilateral&#39;.</li><li><strong>Not a Triangle</strong>: If the sum of any two sides is less than or equal to the third side (A + B &lt;= C, etc.), it returns &#39;Not A Triangle&#39;. This checks if the values do not satisfy the triangle inequality.</li><li><strong>Scalene Triangle</strong>: If none of the sides are equal (A != B AND B != C AND C != A), it returns &#39;Scalene&#39;.</li><li><strong>Isosceles Triangle</strong>: If none of the previous conditions apply, meaning two sides are equal but not all three, it returns &#39;Isosceles&#39;.</li></ul><p>The query evaluates these conditions in the order of precedence and returns the appropriate classification for each row in the Triangles table.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dadcd20f57fd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>