Bibin Velayudhan
Bibin Velayudhan

Reputation: 3103

Mysql query to find highest and lowest among 2 tables

I have got two tables say tableA and tableB. Both tables have fileds say pointA and pointB. I need a single query to find the highest and lowest value among the columns pointA and pointB.

 tableA
 idA    pointA
 1        20
 2        12
 3        41
 4        8     


 tableB
 idB    pointB
 1        24
 2        74
 3        2
 4        16    

I want to get the result like this

 HIGHEST     LOWEST
    74         2      

Upvotes: 2

Views: 484

Answers (3)

yAnTar
yAnTar

Reputation: 4610

select max(pointB) AS result from tableB
union
select min(pointA) AS result from tableA

Upvotes: 1

Marco
Marco

Reputation: 57573

Try:

SELECT MAX(point) as HIGHEST, MIN(point) as LOWEST
FROM
  (SELECT pointA as point FROM tableA
   UNION
   SELECT pointB as point FROM tableB) t

Upvotes: 4

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115530

SELECT MAX(high) AS highest
     , MIN(low)  AS lowest
FROM
  ( SELECT MAX(pointA) AS high
         , MIN(pointA) AS low
    FROM tableA
  UNION ALL
    SELECT MAX(pointB) 
         , MIN(pointB) 
    FROM tableB
  ) AS tmp

Upvotes: 1

Related Questions