
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare Rows and Columns in the Same Table in SQL
It is not uncommon in SQL to analyze tables vertically or horizontally in an attempt to obtain a solution. This might include establishing a baseline for evaluating successive states, determining boolean variables, or even classifying information without requiring joins over distinct tables. In this article, we explain relevant similarities with examples.
Setting Up the Database
Let us set up a database with a sample table to illustrate the processes involved in the case studies, this setup will feature the insertion of dummy values to test comparisons.
First of all, create a database using the CREATE DATABASE statement ?
CREATE DATABASE SalesData; -- Create the database USE SalesData; -- Select the database
Then, we will create a table called Sales in which sales information will be kept. The table has sale_id, sale_date, amount, product, and region columns.
CREATE TABLE Sales ( sale_id INT AUTO_INCREMENT PRIMARY KEY, sale_date DATE, amount DECIMAL(10, 2), product VARCHAR(50), region VARCHAR(50) );
Let's insert some sample records in the Sales table, just for this demonstration ?
INSERT INTO sales (sale_date, amount, product, region) VALUES ('2023-01-10', 500.00, 'Product_A', 'East'), ('2023-01-11', 650.00, 'Product_A', 'East'), ('2023-01-12', 720.00, 'Product_B', 'North'), ('2023-01-13', 450.00, 'Product_A', 'West'), ('2023-01-14', 780.00, 'Product_B', 'North'), ('2023-01-15', 630.00, 'Product_A', 'East');
Comparing Rows in the Same Table
To compare rows you can use self-joins to relate one row to another in the same table for example, let's compare the daily sales amount for consecutive days for Product_A in the East region.
Example: Comparing Daily Sales Amount
In the following query ?
- s1 and s2 are aliases for the same table allowing it to be joined to itself.
- Condition s1.sale_id = s2.sale_id - 1 links each row to the next consecutive row.
- Daily_Change column calculates the difference in sales amount between the two days.
SELECT s1.sale_date AS Date1, s2.sale_date AS Date2, s1.amount AS Amount1, s2.amount AS Amount2, (s2.amount - s1.amount) AS Daily_Change FROM sales s1 JOIN sales s2 ON s1.sale_id = s2.sale_id - 1 WHERE s1.product = 'Product_A' AND s1.region = 'East';
Comparing Columns in the Same Row
Sometimes, you need to evaluate relationships between columns within a single row.
Example: Categorizing Sales Based on Amount
Using a CASE statement you can categorize sales into groups based on the amount column.
SELECT sale_id, sale_date, amount, product, region CASE WHEN amount >= 700 THEN 'High' WHEN amount >= 500 AND amount < 700 THEN 'Medium' ELSE 'Low' END AS Sales_Category FROM sales;
Flagging Regions Based on Conditions
Suppose you want to flag regions with high sales (amount >= 700) for specific products.
Example: Flagging Priority Regions
SELECT sale_id, sale_date, amount, product, region CASE WHEN amount >= 700 AND region = 'North' THEN 'Priority' ELSE 'Standard' END AS Region_Priority FROM sales;