logo

Back to questions

Laptop vs. Mobile Viewership [New York Times SQL Interview Question]

Easy

This is the same question as problem #3 in the SQL Chapter of Ace the Data Science Interview!

Assume you're given the table on user viewership categorised by device type where the three types are laptop, tablet, and phone.

Write a query that calculates the total viewership for laptops and mobile devices where mobile is defined as the sum of tablet and phone viewership. Output the total viewership for laptops as and the total viewership for mobile devices as .

Effective 15 April 2023, the solution has been updated with a more concise and easy-to-understand approach.

Table

Column NameType
user_idinteger
device_typestring ('laptop', 'tablet', 'phone')
view_timetimestamp

Example Input

user_iddevice_typeview_time
123tablet01/02/2022 00:00:00
125laptop01/07/2022 00:00:00
128laptop02/09/2022 00:00:00
129phone02/09/2022 00:00:00
145tablet02/24/2022 00:00:00

Example Output

laptop_viewsmobile_views
23

Explanation

Based on the example input, there are a total of 2 laptop views and 3 mobile views.

The dataset you are querying against may have different input & output - this is just an example!

PostgreSQL 14