Search code examples
iptables

Entry request of 2 tables with best of 3


How do I make a request that will list the names and rating of the top 3 in the tables


Solution

  • PostgreSQL:

    SELECT name, grade 
    FROM students
    JOIN grades USING(student_id)
    WHERE sex = 'm'
    ORDER BY grade desc
    LIMIT 3
    
    • JOIN the tables
    • filter by gender using WHERE
    • sort it in descending order (by grade) using ORDER BY..desc so the bigger numbers are at the top
    • trim the number of results to 3, using the LIMIT

    SQL Server:

    SELECT TOP 3 s.name, g.grade 
    FROM students s
    JOIN grades g ON ( s.student_id = g.student_id )
    WHERE sex = 'm'
    ORDER BY grade desc
    
    • JOIN the tables
    • filter by gender using WHERE
    • sort it in descending order (by grade) using ORDER BY..desc so the bigger numbers are at the top
    • trim the number of results to 3, using the TOP at the beginning of the query

    Note: you may want to include a unique ID in your results, since you may have many students with the same name, making the result ambiguous