SQL 50, decoded
0 of 50 solved/50

619. Biggest Single Number

SQL 50 · Sorting and Grouping (day 4). Decode it with the hints, solve it on LeetCode, then compare with the reference solution in MySQLPostgreSQL.

EasySolve it on LeetCode ↗

Your work

Hints

Open one at a time. Try to finish with as few as you can.

1. What does one output row represent?
One number, or NULL.
2. Approach
Subquery: GROUP BY num HAVING COUNT(*) = 1. Outside: SELECT MAX(num).
3. The trap
MAX over an empty set returns NULL, which is what’s required.

Reference solution

Tested on MySQL 8.0 and PostgreSQL 16. Switch dialect at the top of the page.

Show the MySQLPostgreSQL solution
SELECT MAX(num) AS num
FROM (
  SELECT num
  FROM MyNumbers
  GROUP BY num
  HAVING COUNT(*) = 1
) singles;

Patterns used

Toolbox