Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
Start free
SQL Join and Sorting
00:00
5 left

SQL Join and Sorting

MediumSQL · PostgreSQL

Problem

InMobi campaign reporting combines campaign records with their associated advertisers. Write a PostgreSQL query that joins these two tables and returns the campaign name and advertiser name twice: first in ascending campaign-name order, then in descending campaign-name order.

Requirements

  1. Join inmobi_campaigns to inmobi_advertisers using the advertiser identifier.
  2. Return only campaigns with a matching advertiser.
  3. Produce one ascending result block followed by one descending result block.
  4. Place NULL campaign names last within either sort direction, while retaining matched rows with NULL advertiser names.

Output details

  • Return columns named campaign_name followed by advertiser_name.
  • Within the ascending block, sort ties by advertiser_name ascending with NULLs last.
  • Within the descending block, sort ties by advertiser_name descending with NULLs last.
  • Use campaign_id ascending as the final tie-breaker in both blocks.
  • Preserve duplicate matched rows without deduplication.

Schema

inmobi_campaigns
ColumnTypeDescription
campaign_idPKINTEGERUnique campaign identifier
campaign_nameVARCHAR(150)Campaign name
advertiser_idINTEGERAdvertiser associated with the campaign
inmobi_advertisers
ColumnTypeDescription
advertiser_idPKINTEGERUnique advertiser identifier
advertiser_nameVARCHAR(150)Advertiser name
Tablesinmobi_campaignsinmobi_advertisers
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.