SQL
Lab
Google Google Maps Interview Question 03

Popular
Routes

Find the most frequently searched route.

Table Schema

Inspect
Table

interactive
ColumnType
search_idinteger
user_idinteger
sourcevarchar
destinationvarchar
search_timedate

Sample Data

Input
Output

Sample Input: search_routes
search_iduser_idsourcedestinationsearch_time
1101AB2024-01-01
2102AB2024-01-01
3103BC2024-01-01
4104AB2024-01-02
5105BC2024-01-02
6106AD2024-01-02
7107AB2024-01-03
8108BC2024-01-03
9109AB2024-01-03
Expected Output
sourcedestinationcount
AB5

SQL Editor

Run
Query

postgresql
Waiting for query

sourcedestinationcount
AB5

Hints

Unlock
Clues

Hint 01: A route is the source plus destination pair.
Hint 02: Count searches per pair.
Hint 03: Sort descending and keep the top row.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

SELECT
  source,
  destination,
  COUNT(*) AS count
FROM search_routes
GROUP BY source, destination
ORDER BY count DESC
LIMIT 1;

Explanation

Step By
Step

01

Group searches by route.

02

Count each route search.

03

Order by frequency and return the most searched route.

Related Questions

Keep
Solving