I want to get the maximum Column5 from both the table and then the maximum Column3 from the table which has max Column5.
I don't know if it is the right forum and also am not good in sql :(

SELECT max(Column5_1)
FROM
  (SELECT max(Column5) Column5_1
   FROM TABLE1
   WHERE Column4 = -1
     AND Column1 = 'STRING1'
     AND Column2 = 'STRING2'
     AND Column3 >=
       (SELECT max(Column3_2)
        FROM
          (SELECT max(Column3_1) Column3_2
           FROM
             (SELECT max(Column3) Column3_1
              FROM TABLE1
              WHERE Column4 = -1
                AND Column1 = 'STRING1'
                AND Column2 = 'STRING2'
              UNION ALL SELECT max(Column3) Column3_1
              FROM TABLE2
              WHERE Column4 = -1
                AND Column1 = 'STRING1'
                AND Column2 = 'STRING2')))
   UNION ALL SELECT max(Column5) Column5_1
   FROM TABLE2
   WHERE Column4 = -1
     AND Column1 = 'STRING1'
     AND Column2 = 'STRING2'
     AND Column3 >=
       (SELECT max(Column3_2)
        FROM
          (SELECT max(Column3_1) Column3_2
           FROM
             (SELECT max(Column3) Column3_1
              FROM TABLE1
              WHERE Column4 = -1
                AND Column1 = 'STRING1'
                AND Column2 = 'STRING2'
              UNION ALL SELECT max(Column3) Column3_1
              FROM TABLE2
              WHERE Column4 = -1
                AND Column1 = 'STRING1'
                AND Column2 = 'STRING2'))))

Above Query gives correct result but need to improve it.

Improve it how? Make it shorter? Make it faster? Make it more intuitive? You need a specific goal, and "improve it" is not specific at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.