Looking for the top 10 scores between students listed in two tables (sqlite3)

33 Views Asked by At

Here's the code that I'm trying with but the results were not correct or only showing the column with names:

sql = ("Select * from (Select name, SCORES from  Table 1 where SCORES>0 order by SCORES) "
       "UNION ALL "
       "Select * from (Select name, SCORES from  Table 2 where SCORES >0 order by SCORES) "
       "ORDER BY SCORES DESC Limit 10")

Here's a sample of table 1

name Gender SCORES
Steve Male 1200
Robert Male 1600

Here's a sample of table 2

name Gender SCORES
Cindy Female 3500
Judy Female 4500
1

There are 1 best solutions below

0
jarlh On

Simply UNION ALL the tables, order by scores descending, and pick the 10 first rows:

select name, scores from table1
union all
select name, scores from table2
order by scores desc
limit 10