How to use Subqueries In SQL

Another approach for computing the delta between two columns on different rows is, by using subqueries to retrieve the value of the previous row and then calculate the value of the difference or delta.

Use the below command to compute the delta between two columns on different rows using subqueries.

SELECT
A.geek_value,
A.geek_value - (SELECT B.geek_value FROM geeks_table B WHERE B.geek_id + 1 = A.geek_id ) AS Delta
FROM
geeks_table A;

Output:

geek_value

Delta

100

NULL

200

100

300

100

400

100

500

100

Explanation:

  • (SELECT B.geek_value FROM geeks_table B WHERE B.geek_id + 1 = A.geek_id ): This kind of subqueries is called correlated subqueries. For every row value (A.geek_value) of geeks_table(A), this subquery returns the previous row value(B.geek_value) of geeks_table(B) by using the subsequent geek_id of the geeks_table.
  • SELECT A.geek_value, A.geek_value – (SELECT B.geek_value FROM geeks_table B WHERE B.geek_id + 1 = A.geek_id ) AS Delta: This part of the query computes the difference between the first occurrence of geeks_table(A) geek_value column and value returned by subquery as aliases it as Delta.

Compute a Difference (Delta) Between Two Columns on Different Rows

MySQL is an open-source, Relational Database Management System that stores data in a structured format using rows and columns. It’s software that enables users to create, manage, and manipulate databases. Similar to SQL we use queries to store and access data in the MySQL databases.

In this article, we’ll learn how to calculate the difference (delta) between two columns on different rows. Before moving into this article, you need to install MySQL. To do so you can refer to the below articles,

Similar Reads

Computing Difference (Delta) Between Two Columns on Different Rows

In MySQL, computing the difference between two columns on different rows can be done in multiple ways,...

1. Using Window Functions

Window functions in MySQL allow for performing calculations across a set of rows related to the current row. Using LAG(), and LEAD() window functions, we can compute the delta between values in two columns on different rows....

2. Using SELF JOIN

Another approach for computing the delta between two columns on different rows is, by using self-join to retrieve the value of the previous row and then calculate the value of the difference or delta....

3. Using Subqueries

Another approach for computing the delta between two columns on different rows is, by using subqueries to retrieve the value of the previous row and then calculate the value of the difference or delta....

4. Using Common Table Expressions (CTEs)

Other ways of computing delta between two columns on multiple rows is, by using common table expressions. CTEs are helpful in defining temporary result sets that can be referenced within a MySQL query....

Conclusion

Therefore, In MySQL, we can compute the difference (delta) between two columns on different rows using window functions, self-join, subqueries, and CTEs common table expressions. Each approach may vary in computation speed and methodology. So one can choose ones own approach according to the requirement....