Kunwar Gurkirat Singh
Kunwar Gurkirat Singh

Reputation: 21

joining two tables and getting minimum value out of it in mysql

I have two different tables in sql both contaning name and marks. I want to make new table having colums names and minimum marks out of two tables for each name.

Upvotes: 0

Views: 53

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520988

One possibility is to do a UNION of the two tables and then query out the minimum mark for each name:

SELECT t.name, MIN(t.mark) AS min_mark
FROM
(
    SELECT name, mark FROM table1
    UNION ALL
    SELECT name, mark FROM table2
) t
GROUP BY by t.name

Upvotes: 1

Related Questions