- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display records from two columns based on comparison in MySQL?
Let us first create a table −
mysql> create table DemoTable -> ( -> Num1 int, -> Num2 int -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100,200); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(200,100); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values(300,400); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable values(400,300); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values(500,600); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values(600,500); Query OK, 1 row affected (0.18 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
This will produce the following output −
+------+------+ | Num1 | Num2 | +------+------+ | 100 | 200 | | 200 | 100 | | 300 | 400 | | 400 | 300 | | 500 | 600 | | 600 | 500 | +------+------+ 6 rows in set (0.00 sec)
Following is the query that compares values from both the columns −
mysql> select *from DemoTable where Num1 < Num2;
Output
This will produce the following output −
+------+------+ | Num1 | Num2 | +------+------+ | 100 | 200 | | 300 | 400 | | 500 | 600 | +------+------+ 3 rows in set (0.00 sec)
- Related Articles
- Replace records based on conditions in MySQL?
- Find “greatest” between two columns and display with some records already null in MySql
- Count values based on conditions and display the result in different columns with MySQL?
- Select all duplicate MySQL rows based on one or two columns?
- Implement MySQL IN for 2 columns to display only selected records
- ORDER BY records in MySQL based on a condition
- Comparison of varchar date records from the current date in MySQL
- Display records with more than two occurrences in MySQL?
- Selecting records within a range and with a condition set on two columns in MySQL?
- Display table records from a stored procedure in MySQL
- Divide numbers from two columns and display result in a new column with MySQL
- Display selected records from a MySQL table with IN() operator
- Display records on the basis of key-value pairs in MySQL
- MySQL to fetch records based on a specific month and year?
- Display records ignoring NULL in MySQL

Advertisements