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 Group by and Joins
00:00
5 left

SQL Group by and Joins

MediumSQL · PostgreSQL

Problem

SQL: find the average and count per group in one table, join it with another table to get other properties, and order the result (full join, group by, count, avg, order by). Asked in the HackerRank online test stage. Q2.

Write the query using the provided payment and merchant tables.

Output

  1. One row per merchant or payment group, including unmatched rows from either table.
  2. Columns: merchant_id, merchant_name, country, payment_count, and average_amount.
  3. Use zero for merchants without payments; retain NULL for an unavailable average.
  4. Order by merchant_name ascending with NULLs last, then merchant_id ascending.

Schema

payment_events
ColumnTypeDescription
payment_idPKINTUnique payment event identifier
merchant_idVARCHAR(20)Merchant identifier associated with the payment
amountNUMERIC(10,2)Payment amount
merchant_accounts
ColumnTypeDescription
merchant_idPKVARCHAR(20)Unique merchant identifier
merchant_nameVARCHAR(100)Merchant display name
countryVARCHAR(2)Two-letter merchant country code
Tablespayment_eventsmerchant_accounts
Interviewer

Your question is SQL Group by and Joins. 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.