From the course: SQL Hands-On Practice: Solve Business Problems
Unlock this course with a free trial
Join today to access over 25,200 courses taught by industry experts.
Solution: Comparing rows within the same table - SQL Tutorial
From the course: SQL Hands-On Practice: Solve Business Problems
Solution: Comparing rows within the same table
- [Instructor] We've already looked at some examples of self joins where we're representing a hierarchical relationship but now we're going to look at the use case where you can self join to compare rows within the same table. This is a more creative and advanced use case of self joins. It's not something I use all the time on the job but it's good to know how to do so you exercise your SQL skills and grow in your understanding of how self joins work. Since we're comparing monthly revenues for this problem, we first need to create a CTE that calculates the monthly revenues from the subscriptions table. I've given you that code on lines two through eight. Let's run this to see what it looks like. When we run this, we have the total summed revenue amount for each month. Alright, now let's turn this into a CTE called "Monthly Revs" and start the next part of the query that involves the self join. We're going to scroll back up…