Guest User

Untitled

a guest
May 24th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. mysql> select * from accounts limit 5 group by type;
  2.  
  3. SELECT account_type, account_balance FROM accounts WHERE account_type='savings'
  4. ORDER BY account_balance DESC LIMIT 5
  5. UNION
  6. SELECT account_type, account_balance FROM accounts WHERE account_type='chequing'
  7. ORDER BY account_balance DESC LIMIT 5
  8. UNION
  9. SELECT account_type, account_balance FROM accounts WHERE account_type='USD'
  10. ORDER BY account_balance DESC LIMIT 5;
  11.  
  12. SELECT *
  13. FROM accounts a1
  14. WHERE 5 >
  15. (
  16. SELECT COUNT(*)
  17. FROM accounts a2
  18. WHERE a2.type = a1.type
  19. AND a2.balance > a1.balance
  20. )
  21.  
  22. select * from accounts group by type limit 5;
  23.  
  24. SELECT a.*
  25. FROM accounts a
  26. LEFT JOIN accounts a2 ON (a2.type = a.type AND a2.id < a.id)
  27. WHERE count(a2.id) < 5
  28. GROUP BY a.id;
Add Comment
Please, Sign In to add comment