Window Functions / Ranking

/* Available in MySQL, SQL Server, PostgreSQL */
SELECT 
    Name,
    CountryCode,
    NTILE(3) over p_code AS _ntile
FROM city
WHERE
    Name IN ('San Jose''La Paz')
WINDOW p_code AS (PARTITION BY Name ORDER BY CountryCode)


Name CountryCode _ntile
La Paz BOL 1
La Paz MEX 2
La Paz MEX 3
San José CRI 1
San Jose PHL 1
San Jose PHL 2
...

The NTILE() function partitions a dataset into N equal-sized groups. The number of groups is determined by the function's parameters.