

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Select the minimum value from the maximum values of two tables with a single MySQL query?
For this, you can use UNION in MySQL. Let us first create a table −
mysql> create table DemoTable1 -> ( -> Value int -> ) -> ; Query OK, 0 rows affected (0.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1 values(60); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1 values(78); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable1 values(57); Query OK, 1 row affected (0.08 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1;
This will produce the following output
+-------+ | Value | +-------+ | 60 | | 78 | | 57 | +-------+ 3 rows in set (0.00 sec)
Here is the query to create second −
mysql> create table DemoTable2 -> ( -> Value int -> ); Query OK, 0 rows affected (0.48 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable2 values(90) ; Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable2 values(67) ; Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable2;
This will produce the following output
+-------+ | Value | +-------+ | 90 | | 67 | +-------+ 2 rows in set (0.00 sec)
Here is the query to select the minimum value from the maximum value of two tables −
mysql> select min(tbl.Value) as Value -> from -> ( -> select max(Value) as Value from DemoTable1608 -> union -> select max(Value) as Value from DemoTable1609 -> ) as tbl;
This will produce the following output
+-------+ | Value | +-------+ | 78 | +-------+ 1 row in set (0.04 sec)
- Related Questions & Answers
- MySQL SELECT from two tables with a single query
- MySQL select and insert in two tables with a single query
- A single MySQL select query on two tables is possible?
- How to find the minimum and maximum values in a single MySQL Query?
- MySQL select only a single value from 5 similar values?
- How to count rows from two tables in a single MySQL query?
- Count NOT NULL values from separate tables in a single MySQL query
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- How can I get maximum and minimum values in a single MySQL query?
- MySQL query to select a record with two exact values?
- A single MySQL query to find the highest and lowest among two tables?
- How can I get the output of multiple MySQL tables from a single query?
- A single query to get the sum of count from different tables in MySQL?
- A single MySQL query to select value from first table and insert in the second?
- MySQL query to fetch the maximum corresponding value from duplicate column values
Advertisements