
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
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 Articles
- MySQL SELECT from two tables with a single query
- MySQL select and insert in two tables with a single query
- MySQL select only a single value from 5 similar values?
- A single MySQL select query on two tables is possible?
- Select minimum row value from a column with corresponding duplicate column values in MySQL
- Insert values in two tables with a single stored procedure call in MySQL
- How to find the minimum and maximum values in a single MySQL Query?
- Count NOT NULL values from separate tables in a single MySQL query
- How can I get maximum and minimum values in a single MySQL query?
- Select distinct values from three columns and display in a single column with MySQL
- Insert values from the first table to the second table using two SELECT statements in a single MySQL query
- Finding the minimum and maximum value from a string with numbers separated by hyphen in MySQL?
- MYSQL select DISTINCT values from two columns?
- How to count rows from two tables in a single MySQL query?
- Select a specific value between two column values in MySQL?

Advertisements