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 OR/AND Range Logic
00:00
5 left

SQL OR/AND Range Logic

MediumSQL · PostgreSQL

Problem

What is the meaning of 'OR' here, and how do you use OR/AND between ranges?

Write a PostgreSQL query that evaluates each observation against the two ranges configured for its range set. A match is valid when the values fit the ranges in either order. Range boundaries are inclusive, and inactive range sets must be excluded.

Output

  1. Return one row per matching observation with observation_id, range_set_id, first_value, second_value, and matched_order.
  2. Sort by range_set_id, then observation_id.
  3. Label the matching orientation as range_1_then_range_2 or range_2_then_range_1.

Schema

range_sets
ColumnTypeDescription
range_set_idPKINTIdentifier for a pair of numeric ranges
range_set_nameVARCHAR(100)Descriptive range set name
range_1_minNUMERIC(10,2)Inclusive lower bound of the first range
range_1_maxNUMERIC(10,2)Inclusive upper bound of the first range
range_2_minNUMERIC(10,2)Inclusive lower bound of the second range
range_2_maxNUMERIC(10,2)Inclusive upper bound of the second range
is_activeBOOLEANWhether the range set can be used
observations
ColumnTypeDescription
observation_idPKINTUnique observation identifier
range_set_idINTReferenced range set
first_valueNUMERIC(10,2)First value to evaluate
second_valueNUMERIC(10,2)Second value to evaluate
Tablesrange_setsobservations
Interviewer

Your question is SQL OR/AND Range Logic. 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.