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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
SQL Valid Event and Anomaly Counts
00:00
5 left

SQL Valid Event and Anomaly Counts

MediumSQL · PostgreSQL

Problem

Tell me about a time you handled tricky data manipulation issues, including edge cases and unexpected input, in a production system.

For a hands-on SQL follow-up, use the users and production_events tables. Treat supported event types as valid only when their timestamp is present and their value is either nonnegative or NULL.

Output

  1. One row per user, including users with no events
  2. Columns: user_id, email, valid_event_count, anomaly_count, and latest_valid_at
  3. Exclude unknown users from the result, count invalid known-user events as anomalies, and sort by user_id ascending

Output details

  • Treat session_started, session_ended, and note_created as the only supported event types.
  • Count qualifying events only when event_id is non-NULL, without deduplicating event rows.
  • Report valid_event_count and anomaly_count as 0 when no counted events exist.
  • Set latest_valid_at to NULL when no valid event exists.

Schema

users
ColumnTypeDescription
user_idPKINTUnique identifier for a known user
emailVARCHAR(255)User email address
external_keyVARCHAR(64)Optional identifier from an external system
production_events
ColumnTypeDescription
event_idPKINTUnique production event identifier
user_idINTUser associated with the event, when known
event_typeVARCHAR(64)Reported event type
occurred_atTIMESTAMPTime at which the event occurred
valueDECIMAL(10,2)Optional nonnegative event value
sourceVARCHAR(32)Originating ingestion source
Tablesusersproduction_events
Interviewer

Your question is SQL Valid Event and Anomaly Counts. Start with the requirements and the two 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.