Hi! Could we please enable some services and cookies to improve your experience and our website?

SQLize | PHPize | SQLtest

Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code

A A A
Login    Share code      Blog   FAQ
Copy Format Clear
create table videos (id int); insert into videos values (1), (2), (3), (4), (5); -- Views table has 3 entries, video 2 watched 2 hours ago, video 4 watched 5 days ago, video 5 watched just now create table views (video_id int, watched_at timestamp); insert into views values (2, current_timestamp - interval '2 hour'), (4, current_timestamp - interval '5 day'), (5, current_timestamp); select id, last_watched_at from videos left join ( select video_id, max(watched_at) last_watched_at from views group by video_id ) last_views on id = video_id order by last_watched_at asc nulls first

Stuck with a problem? Got Error? Ask AI support!

Copy Clear