Category: SQL


  • Concatenating multiple rows into a single column dynamically

    How to concatenate multiple rows of a column in a table into a single column? I have to concatenate multiple rows to a single column. For example, consider the below teacher’s table. The above table is a normalized table containing the subjects and teacher ID. We will denormalize the table, by concatenating the subjects of…

  • Different Ways (How) to Delete Duplicate Rows in Table

    In this article, I am going to show different ways of deleting duplicating records from the table. It is a common question in interviews which is asked frequently. Consider the following table with rows as an example: Table Name: Products ProductId Price ————— 1 10 1 10 2 20 3 30 3 30 Here, assume…

  • SQL Query to Group / Aggregate N Consecutive Rows

    Interview Question: In one of my project, I got a requirement to group N consecutive rows and find the min, max and sum of values in each group. In this article, I will show you how to do aggregation on N successive rows. Let’s take the sales table as an example. The data in the…

  • Self Join – How to Write Self Join Queries Easily

    Self Join: By definition, a self join query is a query in which the table is joined to itself. Self joins are written in cases where there is parent child relationships in the data. Let’s take the classic example of employees table. We have to write a self join query to find out the immediate…