<?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 Suharsh Mahajan on Medium]]></title>
        <description><![CDATA[Stories by Suharsh Mahajan on Medium]]></description>
        <link>https://medium.com/@suharshmahajan07?source=rss-cd2774fcedad------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Fm1dAFbQqK9iI1xojPLZhw.jpeg</url>
            <title>Stories by Suharsh Mahajan on Medium</title>
            <link>https://medium.com/@suharshmahajan07?source=rss-cd2774fcedad------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 12:38:21 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suharshmahajan07/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[Mastering the Language of Data: SQL-12]]></title>
            <link>https://medium.com/@suharshmahajan07/mastering-the-language-of-data-sql-12-492710ba7ce3?source=rss-cd2774fcedad------2</link>
            <guid isPermaLink="false">https://medium.com/p/492710ba7ce3</guid>
            <category><![CDATA[sql-queries]]></category>
            <category><![CDATA[order-of-execution]]></category>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[business-analyst]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Suharsh Mahajan]]></dc:creator>
            <pubDate>Tue, 05 Sep 2023 04:58:28 GMT</pubDate>
            <atom:updated>2023-09-05T04:58:28.317Z</atom:updated>
            <content:encoded><![CDATA[<p>In the previous blog post we have solved few problems on Window Functions. If you haven’t read that then click <a href="https://medium.com/devops-dev/mastering-the-language-of-data-sql-11-1559ccc30cf">here</a>.</p><p>In this post we will be learning about the <strong>order of execution of SQL Query</strong>, this is a very important concept to understand.</p><p><strong>Order of execution of SQL Query</strong> helps in <strong>Query Optimisation</strong>, <strong>Performance Tuning</strong>, <strong>Debugging</strong>, <strong>Query Designing</strong>. We will see this terms later in some other post. Today we will only focus on <strong>order of execution of SQL Query</strong>.</p><p><strong>Order of Execution of SQL Query —</strong></p><ol><li><strong>FROM Clause</strong>- FROM clause helps us to determine source tables or views which will be used further.</li><li><strong>JOIN Clause</strong>- This is an optional clause used to combine tables based on a common column.</li><li><strong>WHERE Clause</strong>- After the tables are defined, the database may apply few conditions which are specified in WHERE Clause to filter the rows based on certain conditions.</li><li><strong>GROUP BY Clause</strong>- This is also an optional clause, if present thr rows are grouped in sets based on certain columns.</li><li><strong>HAVING Clause</strong>- This clause if present use to filter records based on a conditions on grouped set of rows.</li><li><strong>SELECT Clause</strong>- SELECT Clause returns the final output. It contains all the columns which needs to be returned.</li><li><strong>DISTINCT Clause</strong>- DISTINCT Clause is used within SELECT clause and it is use to discard those rows which have similar values.</li><li><strong>ORDER BY Clause</strong>- If an ORDER BY clause is present, the result set is sorted according to the specified columns and sort order.</li><li><strong>LIMIT or OFFSET Clause</strong>- If LIMIT or OFFSET clauses are used, they restrict the number of rows returned in the result set or skip a certain number of rows, respectively.</li></ol><p><strong>Window Functions</strong> if present are executed within SELECT Clause.</p><p>This is all from this post.</p><p>Any queries you can comment down below.</p><p>Follow me on <a href="https://www.linkedin.com/in/suharshmahajan/">Linkedin</a>.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=492710ba7ce3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering the Language of Data: SQL- 11]]></title>
            <link>https://blog.devops.dev/mastering-the-language-of-data-sql-11-1559ccc30cf?source=rss-cd2774fcedad------2</link>
            <guid isPermaLink="false">https://medium.com/p/1559ccc30cf</guid>
            <category><![CDATA[window-functions]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[learn-sql]]></category>
            <dc:creator><![CDATA[Suharsh Mahajan]]></dc:creator>
            <pubDate>Thu, 24 Aug 2023 15:44:39 GMT</pubDate>
            <atom:updated>2023-08-25T17:52:40.703Z</atom:updated>
            <content:encoded><![CDATA[<p>In the previous blog post we learnt about <strong>RANK(), DENSE_RANK() and ROW_NUMBER()</strong> window functions in SQL.</p><p>If you haven’t read that then click <a href="https://medium.com/p/69a73f22dcf8">here</a>.</p><p>Today we will be solving few problems related to window function and build the intuition for the same.</p><p><strong>Problem -1: Rank Scores</strong></p><p><a href="https://leetcode.com/problems/rank-scores/">Rank Scores - LeetCode</a></p><p>In this problem we are given a table Scores with 2 columns id and score.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/137/1*2CHkd2wJynY7ue23udTQew.jpeg" /><figcaption>Input Table</figcaption></figure><p>We have to write a SQL query to calculate <strong>rank</strong> of the scores keeping in mind that the scores should be ranked in descending order and same scores should be given similar rank. And after the tie the next rank should be a next consecutive integer.</p><p>Now firstly let’s see that what all functions are required. It is clear that we have to use a ranking function.</p><p><strong>ROW_NUMBER()</strong> can’t be used as <strong>ROW_NUMBER()</strong> gives consecutive numbers hence if it’s a tie then also tied ranks will be given different rankings.</p><p>Now amongst <strong>RANK()</strong> and <strong>DENSE_RANK()</strong> if you recall that both of these functions allocates same rank if it is a tie but <strong>DENSE_RANK()</strong> doesn’t skip any integer while <strong>RANK()</strong> skips it hence <strong>DENSE_RANK()</strong> should be used.</p><p><strong>Code:</strong></p><pre>SELECT score, _____ AS rank<br>FROM Scores</pre><p>Now here this all is what we need only <strong>DENSE_RANK()</strong> function to be placed inplace of blanks.</p><p>So now what we have to do is firstly sort the scores in Descending order and so we will be using <strong>ORDER BY</strong> clause, there is no such need to partition hence <strong>PARTITION BY</strong> will be skipped.</p><pre>DENSE_RANK() OVER(ORDER BY score DESC)</pre><p>This is the window function code.</p><p>So here <strong>DENSE_RANK()</strong> is used to allocate the Ranks, <strong>OVER()</strong> clause is used to define the set of rows in which function is to be applied(all rows in this case), <strong>ORDER BY</strong> is used to sort scores in <strong>DESC </strong>order.</p><p>So our full code will be —</p><pre>SELECT score, DENSE_RANK() OVER(ORDER BY score DESC) AS rank<br>FROM Scores</pre><p>So here is the full solution to the problem we will be getting 2 rows as the output one with <strong>score</strong> and the other <strong>rank</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/170/1*m76PDc5MgYd5XiP_DvPhew.jpeg" /><figcaption>Output Table</figcaption></figure><p>Above shown is the output table, here we can see that score 4.0 is occuring twice hence both times it has been allocated 1st rank while 3.85 the next score is been allocated 2nd rank. If we have used <strong>RANK()</strong> then 3.85 would have been allocated 3rd rank.</p><p><strong>Problem -2: Department Highest Salary</strong></p><p><a href="https://leetcode.com/problems/department-highest-salary/">Department Highest Salary - LeetCode</a></p><p>This is one of the most important and interesting problem.</p><p>In this problem we have been given 2 tables <strong>Employee</strong> and <strong>Department</strong>.</p><p><strong>Employee</strong> table contains id,name,salary and departmentId as columns where <strong>Department</strong> table contains id and name as columns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/450/1*F2nz3zA1jrvF7CPH6KteFw.jpeg" /><figcaption>Input Tables</figcaption></figure><p>Here shown above are both the input tables.</p><p>We have to write SQL Query to return the<strong> department name, employee name and salary of employee(s)</strong> who have <strong>maximum salary</strong> in each department.</p><p>Firstly developing the intuition —</p><p>Now see to start with since name of the <strong>department</strong> and <strong>salary</strong> are in different tables hence we have to <strong>JOIN</strong> both the tables on <strong>departmentId</strong>.</p><p>With this we will be getting all the desired columns in a table. So this will be out first step. So for this we can use a <strong>Common Table Expression</strong> to define a temporary table. That <strong>Common Table Expression</strong> should return <strong>departmentId</strong>,<strong>employee name, department name</strong> and<strong> salary</strong>.</p><p>Now using that Common Table Expression we will use that table to solve the query.</p><p>Now here we have to<strong> sort</strong> the values <strong>based on salary in each department</strong>. So what we have to do is take out <strong>salaries</strong> from each <strong>department</strong> and give them <strong>Ranks</strong> then take all the rows with <strong>rank 1</strong>.</p><p><em>So it might look little confusive but it is just a line function.</em></p><p>So firstly let’s see how to develop the above function.</p><p>In this it <strong>doesn’t matter if we skip ranks or not</strong> hence we can use <strong>DENSE_RANK()</strong> or <strong>RANK()</strong>.</p><p>We will be using <strong>OVER()</strong> clause to define the set of <strong>rows</strong>. In this case we have to define the rows by keeping 2 things in mind.</p><p>Firstly, the <strong>salaries should be sorted in Descending Order</strong> and the <strong>departments should be separated</strong>.</p><p>To sort salaries in Descending order within the <strong>OVER()</strong> clause we will be using <strong>ORDER BY</strong> clause while to separate the departments we will be using <strong>PARTITION BY</strong> clause.</p><p>By using <strong>RANK() function </strong>we can allocate ranks to salaries for each department. And at last only select those rows with rank value 1.</p><p>So this will give highest salary at each department.</p><p>Now let’s code the query.</p><pre>WITH cte AS(<br>SELECT e.departmentId, e.name AS Employee,e.salary,d.name as Department<br>FROM Employee e<br>INNER JOIN Department d<br>ON e.departmentId = d.id)<br><br>SELECT Department, Employee, Salary <br>FROM(<br>SELECT Department, Employee,Salary,<br>RANK() OVER(PARTITION BY departmentId ORDER BY Salary DESC) as rnk<br>FROM cte) a<br>WHERE rnk=1</pre><p>In the code in the first part we are creating a <strong>Common Table Expression</strong> where we <strong>JOIN</strong> <strong>Employee table</strong> with <strong>Department table</strong>.</p><p>The main purpose of using <strong>Common Table Expression</strong> is to create a table with the given input tables which will <strong>help us to reach to the final result.</strong></p><p>In the next part we have used a<strong> subquery means a query within a query.</strong></p><p>So here what we are doing is in the inner query we are calculating rank using <strong>RANK() function</strong>.</p><p><strong>We can do it by using a different Common Table Expression but subquery is a substitute to it.</strong></p><p>Now after calculating the <strong>RANK()</strong> in the <strong>outer query</strong> we are checking when <strong>rank is equal to 1</strong> we are returning those rows with the values which are required.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/374/1*ovzltiHPO7U23RI59yW0sw.jpeg" /><figcaption>Output Table</figcaption></figure><p>Here we have got the output for the above query.</p><p>This is all from this post.</p><p>Any queries you can comment down below.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1559ccc30cf" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/mastering-the-language-of-data-sql-11-1559ccc30cf">Mastering the Language of Data: SQL- 11</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering the Language of Data: SQL- 10]]></title>
            <link>https://blog.devops.dev/mastering-the-language-of-data-sql-10-69a73f22dcf8?source=rss-cd2774fcedad------2</link>
            <guid isPermaLink="false">https://medium.com/p/69a73f22dcf8</guid>
            <category><![CDATA[learn-sql]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[business-analytics]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[data-analytics]]></category>
            <dc:creator><![CDATA[Suharsh Mahajan]]></dc:creator>
            <pubDate>Sat, 12 Aug 2023 17:01:48 GMT</pubDate>
            <atom:updated>2023-08-15T10:18:35.953Z</atom:updated>
            <content:encoded><![CDATA[<p>In the previous blog post we started learning <strong>Window Function</strong> in SQL. We read about what are window functions, there basic syntax and a problem on <strong>Window Function</strong>. You can go through it <a href="https://medium.com/@suharshmahajan07/mastering-the-language-of-data-sql-9-19f117d55ebd">here</a>.</p><p>To revise <strong>Window Functions</strong> are SQL Functions which are performed on <strong>set of rows</strong>.</p><p>In previous post we saw <strong>AVG()</strong> function as <strong>Window Function</strong>, which comes under <strong>Aggregate Window Function</strong>.</p><p>Today we will read and understand <strong>Rank Window Functions</strong>.</p><p>The major <strong>Rank Window Functions</strong> incluse <strong>RANK()</strong>, <strong>DENSE_RANK()</strong> and <strong>ROW_NUMBER()</strong>.</p><p>Firstly let’s understand about each then we will see them in an examples.</p><p><strong>ROW_NUMBER()</strong>- <strong>Row Number</strong> allocates a <strong>unique sequential integer</strong> starting from 1 to each row within a set of rows.</p><p><strong>RANK()</strong>- <strong>Rank function</strong> allocates a <strong>rank</strong> to each <strong>distinct row</strong> within a set of rows. If 2 or more rows have <strong>same rank</strong>, the next rank will be incremented by <strong>number of tied rows</strong>.</p><p><strong>DENSE_RANK()</strong>- <strong>Dense Rank function</strong> also allocates a rank to each <strong>distinct row</strong> within a set of rows. Unline <strong>RANK(), DENSE_RANK()</strong> doesn’t leave the <strong>gap</strong> in the<strong> ranking sequence</strong>.</p><p>Now we will understand these terms by an example.</p><p>Let’s consider a simple <strong>Students</strong> table with <strong>Name</strong>,<strong> Score</strong> as Column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/460/1*TkxjdUC7FsH1cGubJp3Vpg.jpeg" /><figcaption>Input Table</figcaption></figure><p>Let this be our input table.</p><p>Now we will find <strong>Rank</strong>,<strong> Dense Rank</strong> and <strong>Row Number</strong> for this dataset.</p><p>First let’s start with <strong>RANK()</strong></p><pre>SELECT Name, Score,<br>_________ AS Rank<br>FROM Students</pre><p>This will be the basic <strong>syntax</strong> we just need to add how to calculate <strong>Rank</strong> of each student. First let’s see how we calculate <strong>Rank mathematically</strong>.</p><p>So how we calculate <strong>Rank</strong> is firstly we arrange all the scores in <strong>Descending</strong> <strong>order</strong> then starting from the highest we allocate <strong>Ranks</strong> from 1 uptill the last. In calculating <strong>Rank</strong> we follow <strong>pattern</strong> like when there are 2 or more students with same score then we skip those <strong>Ranks</strong>.</p><p>We will be using Window Function here.</p><pre>RANK() OVER(ORDER BY Score DESC)</pre><p>So above shown is the statement which will help us to calculate <strong>Rank</strong>. As we know in <strong>Window Function</strong> firstly we define a function so <strong>RANK()</strong> is that <strong>function</strong>. Then <strong>OVER clause</strong> which is use to define <strong>set of rows</strong> where this function is to be applied.</p><p>Then we use <strong>PARTITION BY</strong> but since here there is nothing to make <strong>PARTITIONS</strong>. Then we add <strong>ORDER BY</strong> as we all know that to calculate <strong>Ranks</strong> we need to <strong>sort scores</strong> in <strong>Descending Order</strong>.</p><pre>SELECT Name, Score,<br>RANK() OVER(ORDER BY Score DESC) AS Rank<br>FROM Students</pre><p>This is the full query to calculate <strong>RANK()</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*Y4-55z4a4BFB_-XRKet8cw.jpeg" /><figcaption>RANK() Query Output</figcaption></figure><p>So here we can see Bob and Dana have same Score so <strong>Rank</strong> is allocated as <strong>1</strong>, then John is allocated <strong>3</strong> as <strong>Rank.</strong></p><p>Now we will see query for <strong>DENSE_RANK()</strong></p><pre>SELECT Name, Score,<br>_________ AS DenseRank<br>FROM Students</pre><p>Above is the basic syntax for <strong>DENSE_RANK() function</strong>.</p><p><strong>Dense Rank</strong> is calculated just similar to Rank i.e. by arranging <strong>Scores</strong> in Descending Order just the difference here lies as in <strong>RANK()</strong> there can be gap in the sequence while in <strong>DENSE_RANK()</strong> there is no gap in the sequence.</p><p>Though if 2 or more students have to be allocated same <strong>DENSE_RANK()</strong> then also the next one will be given <strong>DENSE_RANK()</strong> in continuous sequence.</p><p>The code for <strong>DENSE_RANK()</strong> will be same as <strong>RANK()</strong>.</p><pre>SELECT Name, Score,<br>DENSE_RANK() OVER(ORDER BY Score DESC) AS DenseRank<br>FROM Students</pre><p>Above shown is the final query. As you can see it is exactly same only <strong>RANK()</strong> word is replaced by <strong>DENSE_RANK()</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*twUfdRKfJ81AbSZlJgJllg.jpeg" /><figcaption>DENSE_RANK() Query Output</figcaption></figure><p>So here we can see Bob and Dana have been allocated <strong>DENSE_RANK()</strong> 1 while John has been allocated 2nd <strong>DENSE_RANK()</strong> unlike in <strong>RANK()</strong> he was allocated 3rd <strong>RANK()</strong>.</p><p>Hence undertanding this difference is quite <strong>important</strong>.</p><p>Now we will see query for <strong>ROW_NUMBER()</strong></p><pre>SELECT Name, Score,<br>_________ AS RowNumber<br>FROM Students</pre><p>Above is the basic syntax for <strong>ROW_NUMBER() function</strong>.</p><p>The way we write the code for <strong>ROW_NUMBER()</strong> is same as <strong>RANK() </strong>and <strong>DENSE_RANK()</strong> only we need to understand is what does <strong>ROW_NUMBER()</strong> return.</p><p>In <strong>ROW_NUMBER()</strong> we start from 1 and allocate subsequent integers to each row without seeing any other parameters.</p><p>Here we can skip <strong>ORDER BY</strong> clause as well as it is not necessary in <strong>ROW_NUMBER()</strong> to be arranged in any order.</p><pre>SELECT Name, Score,<br>ROW_NUMBER() OVER(ORDER BY Score DESC) AS RowNumber<br>FROM Students</pre><p>Above shown is the final query. As you can see it is exactly same only we have used<strong> ROW_NUMBER()</strong>.</p><p>Here <strong>ORDER BY</strong> clause is shown so as to make difference between <strong>RANK()</strong>, <strong>DENSE_RANK()</strong> and <strong>ROW_NUMBER()</strong> clear.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/603/1*SBKCleSFxOZPQ360abEZyQ.jpeg" /><figcaption>ROW_NUMBER() Query Output</figcaption></figure><p>So here we can see Bob has been allocated <strong>ROW_NUMBER()</strong> 1st, while Dana as 2nd and John as 3rd.</p><p>Hence we can clearly make the difference while executing <strong>RANK()</strong>, <strong>DENSE_RANK(</strong>) and <strong>ROW_NUMBER()</strong>.</p><blockquote>These queries find major applications in problems which involves returning person name with Highest salary, score, height etc… from each Department.</blockquote><p>We will see these type of problems in upcoming posts.</p><p>This is all from this post.</p><p>Any queries you can comment down below.</p><p>In next post we will be seeing some interesting problems related to Window Functions.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69a73f22dcf8" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/mastering-the-language-of-data-sql-10-69a73f22dcf8">Mastering the Language of Data: SQL- 10</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering the Language of Data: SQL- 9]]></title>
            <link>https://blog.devops.dev/mastering-the-language-of-data-sql-9-19f117d55ebd?source=rss-cd2774fcedad------2</link>
            <guid isPermaLink="false">https://medium.com/p/19f117d55ebd</guid>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[business-analytics]]></category>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[learn-sql]]></category>
            <category><![CDATA[sql-window-functions]]></category>
            <dc:creator><![CDATA[Suharsh Mahajan]]></dc:creator>
            <pubDate>Sun, 06 Aug 2023 10:52:34 GMT</pubDate>
            <atom:updated>2023-08-14T15:18:10.451Z</atom:updated>
            <content:encoded><![CDATA[<p>In the previous blog post we learnt about an important concept <strong>Common Table Expressions</strong> in SQL. As well we solved few problems related to that concept. If you haven’t gone though then click <a href="https://medium.com/@suharshmahajan07/mastering-the-language-of-data-sql-8-a1971e0ef24e">here</a>.</p><p>Today we will be learning about <strong>Window Functions</strong> in SQL which is very important and at the same time a little complex.</p><p>Firstly to start with what are <strong>Window Functions</strong>?</p><p>So to understand what it is let’s break down into Window and Functions. So what is window in SQL? In SQL <strong>window</strong> is defined as set of row or rows in a table. We use <strong>OVER()</strong> clause to define a window or set of rows in SQL.More detail about <strong>OVER()</strong> clause ahead.</p><p>Now what is <strong>functions</strong>? In <a href="https://blog.devops.dev/mastering-the-language-of-data-sql-3-a8156d5eca05">Mastering the language of Data: SQL- 3 </a>we read about many aggregate functions similarly there are many predifined functions for ranking, dates in SQL which we will be dealing ahead. These functions are used to perform some operations.</p><p>So combining Window and Function we can say a <strong>Window Functions</strong> are some powerful features which are use to perform functions on set of rows in SQL. <strong>Window functions</strong> are use to simplify complex queries.</p><p>So now let’s talk about Syntax of <strong>Window Function</strong></p><p><strong>Syntax:</strong></p><pre>SELECT column1, column2, ..., <br>       window_function(column)<br> OVER (PARTITION BY partition_column <br>ORDER BY order_column) AS result_column<br>FROM your_table;</pre><p>Let’s understand the meaning of the syntax first.</p><p>Firstly column1, column2,…. tells which all Columns are we returning.</p><p>window_function(column) is a <strong>Window Function</strong> they may include SUM,AVG,RANK etc…</p><p><strong>OVER()</strong> claue define a window of rows over which the function must be applied.</p><p><strong>PARTITION BY</strong> is an optional statement of OVER() clause that divides the rows into different sets on which the function must be applied separately.</p><p><strong>ORDER BY</strong> is also an optional statement of OVER() clause that tells how the rows are to be processed within each partition(if, any).</p><p>Now let’s understand by an Example-</p><p><strong>Example:</strong></p><p>We are given a table say <strong>Department</strong> table with <strong>id</strong>,<strong>name</strong>,<strong>salary</strong> and <strong>departmentId</strong> as columns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/636/1*GeKCd9GsWwZ-cvuXrm8fhA.jpeg" /><figcaption>Input Table</figcaption></figure><p>Now we have to write SQL Query to return <strong>AVG()</strong> of salaries of each employee per department.</p><p>Let’s build the intuition first and then write the code.</p><p>We have to return name, departmentId and averge of salary of that department.</p><pre>SELECT name, departmentId,_____ AS avg_salary<br>FROM Department</pre><p>So now this will be the basic query to start with and here only we have used <strong>SELECT</strong> to return name, departmentId and avg_salary one is blank and table is <strong>Department</strong> table.</p><p>If we complete that blank our query will be solved.</p><p>Now what we have to do is from each department we need Average of the salary. Do we have <strong>function</strong> for Avergae? Yes we have a <strong>function</strong> AVG().</p><p>Do we need to apply this function on a <strong>set of rows</strong>? Yes we need to apply AVG() on a <strong>set of rows</strong>. Like here all those with departmentId 1 we need to average there salary and similarly with departmentId 2 we need to average there salary too. So that means we have <strong>window </strong>as well as <strong>function</strong> hence we need to apply <strong>Window Function</strong> to build our query quickly.</p><p>So now we got that we need to apply window function. Now let’s understand how to apply it.</p><p><strong>AVG(salary)</strong> will be our <strong>function</strong>. Here AVG() is an aggregate function and salary is the column name.</p><p>Now in <strong>OVER()</strong> clause we learnt 2 things <strong>PARTITION BY</strong> and <strong>ORDER BY</strong>.</p><p><strong>PARTITION BY</strong> separated rows into <strong>different sets </strong>with the help of a column. So do we nee it here? Yes we need it as we need to compute Average based on departmentId hence we need <strong>PARTITION BY departmentId.</strong></p><p><strong>ORDER BY </strong>tells in which order rows needs to be processed. Do we need <strong>ORDER BY </strong>here? We need to compute Average of salaries since here Ascending or Descending order <strong>doesn’t matter</strong> hence we can skip it.</p><p>So our OVER() clause will look something like-</p><pre>OVER(PARTITION BY departmentId)</pre><p>So our whole query will be-</p><pre>SELECT name,departmentid,<br>AVG(salary) OVER(PARTITION BY departmentid) as avg_salary<br>FROM Department</pre><p>Line 2 of the code contains Window Function.</p><p><em>We are using AVG(salary) to compute the average salary on a set of rows called window which we create using OVER() clause and PARTITION BY statements.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/873/1*i-1TdxT1U0Jj8CYCUvTZ-A.jpeg" /><figcaption>Output Table</figcaption></figure><p>Above shown is the <strong>output table formatted as we want</strong>.</p><p>This is all from this post.</p><p>Any queries you can comment down below.</p><p>In next one we will be learning about more Window Functions and problems based on that.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19f117d55ebd" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/mastering-the-language-of-data-sql-9-19f117d55ebd">Mastering the Language of Data: SQL- 9</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering the Language of Data: SQL- 8]]></title>
            <link>https://blog.devops.dev/mastering-the-language-of-data-sql-8-a1971e0ef24e?source=rss-cd2774fcedad------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1971e0ef24e</guid>
            <category><![CDATA[business-analyst]]></category>
            <category><![CDATA[learn-sql]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[mysql]]></category>
            <dc:creator><![CDATA[Suharsh Mahajan]]></dc:creator>
            <pubDate>Mon, 31 Jul 2023 15:55:42 GMT</pubDate>
            <atom:updated>2023-08-01T13:46:07.703Z</atom:updated>
            <content:encoded><![CDATA[<p>In the previous blog we solved few problems related to SQL. We started with problem statement then later we developed intution and then reached to our solution.</p><p>In this blog we will be learning about <strong>Common Table Expression</strong> in SQL.</p><p>This is one of another important concept in SQL which is used to solve complex SQL problems.</p><p><strong>Common table expressions</strong> are referred to as temporary result sets which are defined in a single SQL query which allows us to create more readable and modular SQL code.</p><p>Basically Common table expressions are used to simplify a complex SQL query.</p><p>Let’s look at synatx of Common table expressions first.</p><p><strong>Syntax:</strong></p><pre>WITH cte AS(<br>SELECT column1,column2,..<br>FROM table_name<br>Other Conditions....)<br>SELECT column1,column2...<br>FROM cte <br>Other Conditions....</pre><p>So here what are we doing exactly is that firstly we are creating a temporary table named <strong>cte</strong>, then final result is returned from <strong>cte</strong> table.</p><p>Basically <strong>Common table expressions</strong> are used to simplify or convert the Input table to an intermediate table which in turn used to give the output.</p><p><strong>Example:</strong></p><p>Let’s take a problem from Leetcode.</p><p>So the problems is <strong>Classes more than 5 students(Leetcode -596)</strong></p><p>Input table contains 2 columns students and class. Student column contains names of students and class contains class names.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/205/1*Yd4sck7HTmaIIss7m716qQ.jpeg" /><figcaption>Input Table</figcaption></figure><p>Write SQL query to return those classes which have atleast 5 students.</p><p>So now let’s see the intuition behind the problem then we will see the code.</p><p>Here we need to count number of student in each class then compare. So for this firstly we can create a temporary table which contains 2 rows class and count of students in each class.</p><p>So now how to count number of students in each class? We should use COUNT() as function.</p><p>Code to count number of students in each class.</p><pre>SELECT class , COUNT(student) AS NumStu<br>FROM Courses<br>GROUP BY class</pre><p>Above code says FROM tabe Courses firstly GROUP BY class then count number of students in each class.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/193/1*iwpRA5yCDIdqAsa2y7Db2g.jpeg" /><figcaption>Output</figcaption></figure><p>So above we have got a table which returns class and number of students in each class. This is a temporary table which we have created to reach our result.</p><p>Now next from this temporary table using WHERE clause we can check if count of students(NumStu here) is greater than or equal to 5 and return those class(es).</p><p>Code Implementation:</p><pre>WITH cte AS <br>(SELECT class , COUNT(student) AS NumStu<br>FROM Courses<br>GROUP BY class)<br>SELECT class<br>FROM cte<br>WHERE NumStu &gt;=5;</pre><p>In the above code firstly we created a <strong>Common table expression cte</strong> which contains 2 columns class and NumStu(which contains Number of students in each class). This can be seen in the above fig.</p><p>Now FROM cte we return those class(es) where NumStu is greater than or equal to 5.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/100/1*viosVqBpIAnZ-eNG1JrYAw.jpeg" /><figcaption>Final Output</figcaption></figure><p>This is the final desired output of the query.</p><p>Since the task was complex like firstly we need to count number of students in each class then perform operation on count hence to simplify the process we used <strong>common table expressions</strong>.</p><p>Note- We cannot use a single <strong>Common Table Expression</strong> table in subsequent queries. After a query containing is exectuted <strong>Common Table Expression</strong> if created is destroyed. If we want to use that same <strong>Common Table Expression</strong> in anyother subsequent query then we need to build it again.</p><p><em>We can also create more than 1 Common Table Expressions in a query if we need to do so.</em></p><p>That’s all from this post.</p><p>Any queries you can comment down below.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1971e0ef24e" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/mastering-the-language-of-data-sql-8-a1971e0ef24e">Mastering the Language of Data: SQL- 8</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering the Language of Data: SQL- 7]]></title>
            <link>https://blog.devops.dev/mastring-the-language-of-data-sql-7-42ef2958fd0f?source=rss-cd2774fcedad------2</link>
            <guid isPermaLink="false">https://medium.com/p/42ef2958fd0f</guid>
            <category><![CDATA[analyst]]></category>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[problem-solving]]></category>
            <dc:creator><![CDATA[Suharsh Mahajan]]></dc:creator>
            <pubDate>Thu, 27 Jul 2023 18:48:54 GMT</pubDate>
            <atom:updated>2023-07-30T04:58:59.411Z</atom:updated>
            <content:encoded><![CDATA[<p>So far we have studied various Statements and functions in SQL. So upto now we have covered almost all the basic topics. If you haven’t read yet the click <a href="https://medium.com/@suharshmahajan07">here</a>.</p><p>So starting from today we will be solving few problems each day and even solving case studies.</p><p><strong>Problem- 1</strong></p><p><strong>Combining 2 Tables(Leetcode- 175)</strong></p><p>In this we have 2 tables Person and Address. Person table contains personId, lastName and firstName as columns while Address table contains addressId, personId, city and state as columns.</p><p>We have to write SQL Query to return firstName, lastName, city and state of each person in the Person table. If the address of a personId is not present in the Address table, report null instead.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/473/1*2ML-CwgrKT7W529LoWHy_A.jpeg" /><figcaption>Input Table</figcaption></figure><p>Here you can see the input structure.</p><p>So it is clear that we have 2 join 2 tables. So we have to use JOIN for sure. Now comes next question that using which column we will JOIN so it is the <strong>personId</strong>.</p><p>Now other question is which type of JOIN to use, if you have read the problem then it is written that if address is not present for that person then return null. That means we need all the persons from the Person table so <strong>LEFT JOIN</strong> will be used.</p><p>Now last problem is left which is how to return only the selected columns for that we will use SELECT query and choose only those columns which we want as our output.</p><p><strong>Code:</strong></p><pre>SELECT p.firstName, p.lastName, a.city, a.state<br>FROM Person p<br>LEFT JOIN Address a<br>ON p.personId=a.personId;</pre><p>In this what are we doing is FROM Person table we are LEFT JOINING Address table on personId then we are returning only columns required.</p><p>You will be wondering what is p and a. So these are called alias as we name Person table as p and Address table as a so that we don’t write whole table name again and again.</p><p>Next why have we used p.personId and a.personId so that we are telling that first personId is of Person table then next one is for Address Id.</p><p>Is it necessary to use p.personId or only personId can work? This might be the question for many of you. So the answer is no here it won’t work as column names are same. If they had been different it would work but best practice is to use table name then column name. It will help anyone else to understand the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*XxqrjXxaU6aYd_oBLcpHPw.jpeg" /><figcaption>Output of the above code.</figcaption></figure><p><strong>Problem- 2</strong></p><p><strong>Find Customer Referee(Leetcode- 584)</strong></p><p>In this problem we have been given a Customer table with id, name and referee_id as column names.</p><p>Write an SQL query to report the names of the customer that are <strong>not referred by</strong> the customer with id=2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/277/1*Bxj-bswr2XKX82oHxRXmrg.jpeg" /><figcaption>Input table.</figcaption></figure><p>This problem looks simple right ?</p><p><strong>Code:</strong></p><pre>SELECT name <br>FROM Customer<br>WHERE NOT referee_id=2;</pre><p>This code looks fine to everyone ?</p><p>If yes then no it is not correct because on running this we only get Zack as the output.</p><p>But if we see the problem statement it states return names of the customers not referred by id =2. Those whose id is null are also not referred by id =2.</p><p><strong>Correct Code:</strong></p><pre><br>SELECT name <br>FROM Customer<br>WHERE NOT referee_id=2 OR referee_id IS NULL;</pre><p>Now this code is self explanatory as we are returning names of customers who are not referred by id=2 or where id is null. In both case we need to return the name.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/82/1*Mi-qTAk6AGZmridMNpv6nw.jpeg" /><figcaption>Output Table</figcaption></figure><p>So from these 2 problems we got to know that while solving a SQL problem the most important thing to keep in mind is understanding the problem.</p><p>Till problem is not understood then jumping on to the solution is worthless.</p><p>That’s all from this post.</p><p>Any queries you can comment down below.</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=42ef2958fd0f" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/mastring-the-language-of-data-sql-7-42ef2958fd0f">Mastering the Language of Data: SQL- 7</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering the Language of Data: SQL -6]]></title>
            <link>https://blog.devops.dev/mastering-the-language-of-data-sql-6-41f4d4ce562d?source=rss-cd2774fcedad------2</link>
            <guid isPermaLink="false">https://medium.com/p/41f4d4ce562d</guid>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[learn-sql]]></category>
            <category><![CDATA[business-analytics]]></category>
            <category><![CDATA[data-analytics]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Suharsh Mahajan]]></dc:creator>
            <pubDate>Mon, 24 Jul 2023 18:31:12 GMT</pubDate>
            <atom:updated>2023-07-25T15:27:41.534Z</atom:updated>
            <content:encoded><![CDATA[<p>In previous we learnt about examples of JOIN in SQL. If we haven’t gone through it then click <a href="https://medium.com/@suharshmahajan07/mastering-the-language-of-data-sql-5-bad24772330c">here</a>.</p><p>Today we learn about <strong>GROUP BY</strong> and <strong>HAVING</strong> clause.</p><p>So starting with <strong>GROUP BY</strong> clause.</p><p><strong>GROUP BY</strong> clause is used to group rows based on the values of one or more columns. It allows you to aggregate data and perform calculations on a per-group basis.</p><p>We can use aggregation functions with <strong>GROUP BY</strong> clause which we have studied earlier. <a href="https://blog.devops.dev/mastering-the-language-of-data-sql-3-a8156d5eca05">Link</a> to the blog.</p><p>Syntax:</p><pre>SELECT column1, column2, aggregate_function(column3)<br>FROM table_name<br>GROUP BY column1, column2;</pre><p>In the syntax what we are doing is grouping by 2 columns and returning those 2 columns and some aggregated result of 3 columns.</p><p>We can use <strong>GROUP BY</strong> with any number of columns.</p><p><strong>Example:</strong></p><p>Return total amount spent by each user this month.</p><p>So we will be given a table of say 3 users and there shopping expense of a particular month, We need to return each users total expense. So for that we need a aggregate function SUM().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/201/1*z7lE-4EJRPHrUr_NZN4pmQ.jpeg" /><figcaption>Input Table</figcaption></figure><pre>SELECT id,name, SUM(price) <br>FROM Expense<br>GROUP BY id</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/260/1*EB4-RVH-cor_8LhtM-MCXg.jpeg" /><figcaption>Output of above Query</figcaption></figure><p>So what we have done here let’s try to understand. Firstly we are calling table Expense then we are grouping by Id like in the input table you can see there are 3 ids present and each id has spent multiple times so since we need sum of each and every id separately hence we are using GROUP BY and not WHERE in WHERE we can only get sum of a particular id in a query.</p><p>So after grouping in SELECT query we are returing id, name and sum of total spent of each and every id.</p><p>On seeing the output table SUM(price) this doesn’t look nice so for that we use <strong>AS</strong> keyword to rename any column name. <strong>AS</strong> keyword is used to give temporary name to a column or table.</p><pre>SELECT id,name, SUM(price) AS Total_Spent<br>FROM Expense<br>GROUP BY id</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/265/1*9jNxYraDCln--u7n_nBNcA.jpeg" /></figure><p>So the output is exactly the same only column name is Total_Spent instead of SUM(price). Now it is more clear that Andrew has spent 7290, Joe has spent 10820 so on….</p><p>Next let’s do <strong>HAVING</strong> clause. <strong>HAVING</strong> clause is used to filter the results of a grouped query based on specific conditions.It allows you to apply conditions to the aggregated data after the <strong>GROUP BY</strong> operation has been performed.</p><p><strong>Syntax:</strong></p><pre>SELECT column1, column2, aggregate_function(column3)<br>FROM table_name<br>GROUP BY column1, column2<br>HAVING condition;</pre><p>Above shown is the syntax for <strong>HAVING</strong> clause. Generally we use <strong>WHERE</strong> with conditions but for aggregated functions we use <strong>HAVING</strong> clause.</p><p><strong>Example:</strong></p><p>Return id and name of the users who have spent more than 7300 a month.</p><pre>SELECT id,name<br>FROM Expense<br>GROUP BY id<br>HAVING SUM(price) &gt;7300</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/113/1*60d8UMRP5F3Ylx8OGZ4R8Q.jpeg" /><figcaption>Output Table.</figcaption></figure><p>In the above query rest all is same just we added a condition that if SUM(price)&gt;7300 we return only those id and name.</p><p>This is all for this post. More ahead…..</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41f4d4ce562d" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/mastering-the-language-of-data-sql-6-41f4d4ce562d">Mastering the Language of Data: SQL -6</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering the Language of Data: SQL -5]]></title>
            <link>https://blog.devops.dev/mastering-the-language-of-data-sql-5-bad24772330c?source=rss-cd2774fcedad------2</link>
            <guid isPermaLink="false">https://medium.com/p/bad24772330c</guid>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[sqlite]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[sql-joins]]></category>
            <category><![CDATA[learn-sql]]></category>
            <dc:creator><![CDATA[Suharsh Mahajan]]></dc:creator>
            <pubDate>Fri, 21 Jul 2023 16:14:09 GMT</pubDate>
            <atom:updated>2023-07-21T17:24:18.967Z</atom:updated>
            <content:encoded><![CDATA[<p>In previous blog we learnt about JOIN in SQL. There we learnt what is JOIN, why we need JOIN and types of JOIN in SQL. If you haven’t read that then click <a href="https://medium.com/@suharshmahajan07/mastering-the-language-of-data-sql-4-e011e2a6c9b9">here</a>.</p><p>So in continuation with JOIN here will be solve examples related to <strong>JOIN</strong>.</p><p>We will take 2 tables Name and Age with 3–3 rows each.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/227/1*L57VE6FzN3BNVIU3y03eVw.jpeg" /><figcaption>Name Table</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/215/1*bQWxuSMnMR0GkChhsmsXqQ.jpeg" /><figcaption>Age Table</figcaption></figure><p><strong>NOTE: In first column there are numbers written which can be ignored this only signifies serial number nothing else.</strong></p><p>Small sized table is taken so that we will visualise it easily and able to learn JOIN easily.</p><p><strong>Example for Inner Join</strong></p><pre>SELECT * <br>FROM Name<br>INNER JOIN Age<br>ON Name.id = Age.id</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/358/1*a1EAmfafq2I30_ntJaRGuA.jpeg" /><figcaption>Inner Join Result</figcaption></figure><p>In the above we can see that there are 2 id’s column one is of Name table and the other one from Age table.</p><p>On recalling previous blog knowledge in Inner Join we get records for only intersecting values for a common field. This can be seen in the above result as well.</p><p><strong>Example for Left Join</strong></p><pre>SELECT *<br>FROM Name<br>LEFT JOIN Age<br>ON Name.id = Age.id</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*vcTovkI_WhKniQRJS-lL8A.jpeg" /><figcaption>Left Join Result</figcaption></figure><p>In Left Join result which we obtained has all the values from the Name table and those values from the age table which have same values in the common column(Id here) and the row Id value for which row doesn’t match, NULL is returned there.</p><p><strong>Example for Right Join</strong></p><pre>SELECT *<br>FROM Name<br>RIGHT JOIN Age<br>ON Name.id = Age.id</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/1*k530GfR_ZzuXNoeGlDQyTQ.jpeg" /><figcaption>Right Join Result</figcaption></figure><p>In Right Join result which we obtained has all the values from the Age table and those values from the name table which have same values in the common column(Id here) and the row Id value for which row doesn’t match, NULL is returned there.</p><p><strong>Example for Full Outer Join</strong></p><pre>SELECT *<br>FROM Name<br>FULL OUTER JOIN Age<br>ON Name.id = Age.id</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*Rrru1pthFllVqLow7-Chaw.jpeg" /><figcaption>Full Outer Join Result</figcaption></figure><p>In Full Outer Join result which we obtained has all the values from both Name and Age column, and in Name table id 2 has no corresponding id in Age table hence NULL is returned in corresponding values of Age table. Similarly it is done for id 4 in Name table.</p><p>This is all for this post. More ahead…..</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bad24772330c" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/mastering-the-language-of-data-sql-5-bad24772330c">Mastering the Language of Data: SQL -5</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering the Language of Data: SQL -4]]></title>
            <link>https://blog.devops.dev/mastering-the-language-of-data-sql-4-e011e2a6c9b9?source=rss-cd2774fcedad------2</link>
            <guid isPermaLink="false">https://medium.com/p/e011e2a6c9b9</guid>
            <category><![CDATA[learn-sql]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[sqlite]]></category>
            <category><![CDATA[analytics]]></category>
            <dc:creator><![CDATA[Suharsh Mahajan]]></dc:creator>
            <pubDate>Thu, 20 Jul 2023 15:44:22 GMT</pubDate>
            <atom:updated>2023-07-25T15:28:10.081Z</atom:updated>
            <content:encoded><![CDATA[<p>In previous few blogs we were learning SQL statements and functions. In the previous blog post we learnt about few functions very important. If you haven’t read that then click <a href="https://medium.com/devops-dev/mastering-the-language-of-data-sql-3-a8156d5eca05">here</a>.</p><p>Today we will be learning the most important and relatively trickier concept <strong>JOIN</strong>.</p><p><strong>JOIN</strong> clause is used to combine rows from 2 or more tables on the basis of a common column.</p><p><strong>So now why we need JOIN ?</strong></p><p>Let’s take an example of a E-Commerce company, so the company have few tables in the database which includes Customers table, Orders table etc…</p><p>So now if we keep all the details of Customers table in Orders table then it will be of no use as it will take extra space as data will be duplicated. In this place what is done is we use a common column(Customer ID) to do all the operations. For accomplishing the above task we use <strong>JOIN</strong>.</p><p>Now since we have studied what is <strong>JOIN</strong> in SQL and why we need that now let’s learn types of <strong>JOIN</strong> and how to perform <strong>JOIN</strong> in SQL.</p><p>So let’s start with learning <strong>JOIN</strong> -</p><p><strong>INNER JOIN</strong> — It is used to return records that have matching values in a common field in two or more than two tables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/258/1*naDQxyNfbnxaCtgmrrolYA.png" /><figcaption>Inner Join</figcaption></figure><p><strong>Syntax:</strong></p><pre>SELECT column1,column2...<br>FROM TableA <br>INNER JOIN TableB <br>ON TableA.id=TableB.id</pre><p><strong>LEFT JOIN</strong> — It is used to return all the records from the Left table and those records that have common field value from the Right table.Records in the left table without matches will have NULL values for the right table columns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/258/1*XmSUjSVixNRIg7uEnP7UoA.png" /><figcaption>Left Join</figcaption></figure><p><strong>Syntax:</strong></p><pre>SELECT column1,column2...<br>FROM TableA<br>LEFT JOIN TableB<br>ON TableA.id = TableB.id</pre><p><strong>RIGHT JOIN</strong> — It is used to return all the records from the Right table and those records that have common field value from the Left table.Records in the right table without matches will have NULL values for the left table columns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/258/1*yMNyT0fQOrNP9Fx1z7Ssxg.png" /><figcaption>Right Join</figcaption></figure><p><strong>Syntax:</strong></p><pre>SELECT column1,column2...<br>FROM TableA<br>RIGHT JOIN TableB<br>ON TableA.id = TableB.id</pre><p><strong>FULL OUTER JOIN</strong> — It is used to combine rows from two tables, even if there is no matching data between them. It returns all the rows from both tables and matches the rows where there is a common value in the specified columns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/258/1*D97BkZKe25MFXR0FkiWhYg.png" /><figcaption>Full Outer Join</figcaption></figure><p><strong>Syntax:</strong></p><pre>SELECT column1,column2...<br>FROM TableA<br>FULL OUTER JOIN TableB<br>ON TableA.id = TableB.id</pre><p>These are some basic types of <strong>JOIN</strong> which are very important in SQL.</p><p>Examples on JOIN in next post…..</p><p>This is all for this post. More ahead…..</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e011e2a6c9b9" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/mastering-the-language-of-data-sql-4-e011e2a6c9b9">Mastering the Language of Data: SQL -4</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mastering the Language of Data : SQL -3]]></title>
            <link>https://blog.devops.dev/mastering-the-language-of-data-sql-3-a8156d5eca05?source=rss-cd2774fcedad------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8156d5eca05</guid>
            <category><![CDATA[business-analysis]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[learn-sql]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[analytics]]></category>
            <dc:creator><![CDATA[Suharsh Mahajan]]></dc:creator>
            <pubDate>Mon, 17 Jul 2023 17:05:54 GMT</pubDate>
            <atom:updated>2023-07-17T19:52:53.107Z</atom:updated>
            <content:encoded><![CDATA[<h3>Mastering the Language of Data : SQL -3</h3><p>In previous 2 blogs we have been learning about various SQL commands. In previous blog we have studied about WHERE, LIKE, ORDER BY and Logical Operators. If you haven’t read it you can read it <a href="https://medium.com/@suharshmahajan07/mastering-the-language-of-data-sql-statements-2-e1edf18bc9e6">here</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/288/1*zjLXy843UrZNwL61qMNhOA.jpeg" /><figcaption>Dataset</figcaption></figure><p>This is the dataset which will be using, it is same as previous blog.</p><p>Today we will start with <strong>MIN()</strong> and <strong>MAX()</strong> functions.</p><p>As everyone knows <strong>MAX()</strong> is used to return largest value from selected column and <strong>MIN()</strong> to return lowest value from selected column.</p><p><strong>Syntax:</strong></p><pre>SELECT MAX(column_name)<br>FROM table_name<br>WHERE conditions </pre><p>This syntax will be same for MIN() only MAX() will be replaced by MIN() and WHERE clause is optional.</p><p><strong>Example:</strong></p><p>We will select MIN() marks out of even marks.</p><pre>SELECT MIN(marks)<br>FROM Students<br>WHERE marks%2==0</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/155/1*E7OXULmQpaVBBVwa5HquJw.jpeg" /></figure><p>Here is the output of the above query the minimum marks out of even marks is 44 which can be seen from the above dataset table.</p><p>Like this we can do for MAX().</p><p>Now next inline we have <strong>SUM()</strong> function which returns total sum of a numerical column.</p><p><strong>Syntax:</strong></p><pre>SELECT SUM(column_name)<br>FROM table_name<br>WHERE conditions</pre><p>In this syntax also WHERE clause is optional.</p><p><strong>Example:</strong></p><p>Return sum of marks of all students.</p><pre>SELECT SUM(marks)<br>FROM Students</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/163/1*R2jh_cgFgYa3RrB4dOvwJw.jpeg" /></figure><p>Here we get total sum of marks.</p><p>Next we will do <strong>COUNT()</strong> and <strong>AVG()</strong> functions which is exactly same like <strong>SUM()</strong> function.</p><p><strong>COUNT()</strong> function returns number of rows which satisfies the conditions and <strong>AVG()</strong> function returns average value of a numeric column.</p><p><strong>Syntax:</strong></p><pre>SELECT COUNT(column_name)<br>FROM table_name<br>WHERE conditions</pre><pre>SELECT AVG(column_name)<br>FROM table_name<br>WHERE conditions</pre><p><strong>Example:</strong></p><p>Count number of students who have marks more than 50.</p><pre>SELECT COUNT(marks)<br>FROM Students<br>WHERE marks&gt;50</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/198/1*5TxPhODfoKcPC2Bl6F0Prw.jpeg" /></figure><p>So there are total 6 students whose marks are more than 50.</p><p>AVG() can be solved in similar way as we did SUM() and COUNT().</p><p>Now we will learn about <strong>LIMIT</strong> clause which is used to specify number of records to return.</p><p><strong>Syntax:</strong></p><pre>SELECT column1,column2...<br>FROM table_name<br>WHERE conditions<br>LIMIT number</pre><p><strong>Example:</strong></p><p>Return student name and id with highest marks.</p><p>So to solve this query firstly if we ORDER BY marks in descending order and then <strong>LIMIT</strong> to 1 (that will only return 1 value from the top so it will be student with highest marks)</p><pre>SELECT id,name<br>FROM Students<br>ORDER BY marks|DESC<br>LIMIT 1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/126/1*tdT69JsYlAZtxOU4Q7YFQQ.jpeg" /></figure><p>This is the result which we obtained after querying. This can be checked from the database as well. In this we skipped WHERE clause and used ORDER BY clause instead.</p><p>In SQL firstly FROM clause is executed then WHERE then ORDER BY and at last SELECT query is executed though we write it first. After 1 or 2 more blogs when we have studied all the important clauses then this order my change that we will see later on.</p><p>This is all for this post. More ahead…..</p><p>Thanks for reading!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8156d5eca05" width="1" height="1" alt=""><hr><p><a href="https://blog.devops.dev/mastering-the-language-of-data-sql-3-a8156d5eca05">Mastering the Language of Data : SQL -3</a> was originally published in <a href="https://blog.devops.dev/">DevOps.dev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>