简体   繁体   中英

how to i turn this query to a sub query SQL

how can I change this query where I want to turn the max to a subquery where when I enter the sql the only thing that will appear is the id and first name.

SELECT staff.staff_id,staff.firstname,max(payment.amount)
from payment ,staff
where payment.staff_id=staff.staff_id

use explicit join

SELECT staff.staff_id,staff.firstname,max(payment.amount)
from payment join staff
on  payment.staff_id=staff.staff_id

but i think you want

   SELECT staff.staff_id,staff.firstname,p.amount
    from payment p join staff
    on  p.staff_id=staff.staff_id
    where p.amount= select max(amount) from payment 

You can try using correlated subquery

SELECT staff.staff_id,staff.firstname
from payment inner join staff
on payment.staff_id=staff.staff_id where payment.amount in (select max(payment.amount) from payment)

From JOIN to (correlated) sub-query:

SELECT staff.staff_id, staff.firstname,
       (SELECT MAX(payment.amount) 
        FROM payment
        WHERE payment.staff_id = staff.staff_id)
FROM staff

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM