I have the following two tables related by the ID column as the Primary Key. My Goal is to query the values from the "Name" column in Table 1 which correspond to the User_id with the Max and Min "Score" Column Values from Table 2.
Table 1:
| ID | Name |
|----|------|
| 1 | Foo |
| 2 | Bar |
| 3 | Zoo |
| 4 | Bar |
| 5 | Foo |
| 6 | Zar |
Table 2:
| ID | Score |
|----|-------|
| 1 | 98 |
| 2 | 67 |
| 3 | 86 |
| 4 | 59 |
| 5 | 75 |
| 6 | 73 |
The final output should give me something like this:
| Name | Score |
|------|-------|
| Foo | 98 |
| Bar | 59 |
You can try the below -
select name, score
from table1 t1 join table2 t2 on t1.id=t2.id
where
score=(select max(score) from t2)
or
score=(select min(score) from t2)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments