Skip to content

Instantly share code, notes, and snippets.

@GEverding
Last active December 31, 2015 22:59
Show Gist options
  • Save GEverding/8057060 to your computer and use it in GitHub Desktop.
Save GEverding/8057060 to your computer and use it in GitHub Desktop.
Example SQL Query to find all the records with the maximum value
select
oid, count(distinct pid)
from
owns
group by oid
having count(distinct pid) >= (select
T.max
from
(select
oid, count(distinct pid) as max
from
owns
group by oid
order by max desc
limit 1) as T)

Relations

  • Pet (pid, name, age, species, breed)
  • Owner (oid, first_name, last_name, age, address, phone, email)
  • Owns (pid, oid)
  • Owns.pid is a foreign key that references Pet.pid
  • Owns.oid is a foreign key that references Owner.oid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment