Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
SQL Last 20 Rows Retrieval
00:00
5 left

SQL Last 20 Rows Retrieval

EasySQL · PostgreSQL

Problem

Ascendion's delivery analytics team needs a report showing the most recent activity records across its project portfolio. Write a PostgreSQL query that returns the latest 20 activity rows, preserving activities even when employee or project details are unavailable.

Requirements

  1. Use a CTE to identify the latest 20 rows from activity_events.
  2. Sort by occurred_at descending, using event_id descending as a deterministic tie-breaker.
  3. Use LEFT JOIN to include activity rows with missing employee or project references.
  4. Return the event ID, timestamp, event type, employee name, and project name in newest-first order.

The sample contains fewer than 20 activity rows, so the query should return every available row while still using LIMIT 20.

Schema

activity_events
ColumnTypeDescription
event_idPKINTEGERUnique activity identifier
occurred_atTIMESTAMPTime the activity occurred
event_typeVARCHAR(40)Activity classification
employee_idINTEGEROptional reference to the employee responsible
project_idINTEGEROptional reference to the related project
employees
ColumnTypeDescription
employee_idPKINTEGEREmployee identifier
employee_nameVARCHAR(120)Employee display name
departmentVARCHAR(80)Employee department
projects
ColumnTypeDescription
project_idPKINTEGERProject identifier
project_nameVARCHAR(150)Project display name
client_nameVARCHAR(120)Client organization
Tablesactivity_eventsemployeesprojects
Interviewer

Your question is SQL Last 20 Rows Retrieval. Start with the requirements and the three tables in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.