SQL get value based on corresponding min/max(value) from column in another related table

nautograph :

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    |
Fahmi :

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.

edited at
0

Comments

0 comments
Login to comment

Related

SQL - Get corresponding column based on another row in the same table

How to get Corresponding Column Value from a table based on Maximum values among columns in SQL

R: Print A Value from A Column Based on the Corresponding Row of Another Column

Get column value corresponding to the idxmax from another column per group

SQL - get value from another table if column is null

SQL to Update a column based on another table that isn't directly related

pandas: get the exact corresponding value with the corresponding index based on a value in another column

Need to get the value from a column whose column name is based on a value in another table

get corresponding column value from another dataframe pandas

create table column based on another table column value in sql

SQL query to copy column from one table to another table based off index value

Function that will select value from SQL table and do a sum based on another column value

UPDATE sql column with value from another column based on a date column

How can I get the corresponding value of a column based on an aggregate of another column?

Updating column in a table based on the value from another table

Update column value of one table based on values from another table

SQL group by get value on one column based on order of another column

SQL get last column in a group based on order by and value of another column

SQL - How to get a duplicate column based on the value of another column

Is SQL how to get a value of a column of table from a datetime range of another table?

Replacing an empty cell in one column based on corresponding value from another column?

Oracle - SQL query to bin the data from one table to another based on a column value?

Get Values from columns corresponding to some values in another column till we get same value in second column

Get a Value from a column based on Effective Date in another column

SQL select value from one column based on values in another column

Get value based on column name stored in another table

Conditional format cell in column based on it corresponding value in another column

Return SQL Server column name and corresponding value based on column value

panda group on set column and get corresponding max value from another column