Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Grouping With Conditions

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is SQL Grouping With Conditions. Start with the requirements and the two tables on the right.

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.

Problem

CarGurus wants to compare listing-quality marks across dealer groups. Write a PostgreSQL query that calculates the average mark for each dealer group using only listings with marks greater than 500.

Requirements

  1. Join dealer_groups to listings using the dealer group identifier.
  2. Exclude listings with marks less than or equal to 500, including NULL marks.
  3. Return groups with at least two qualifying listings, their qualifying listing count, and their average mark rounded to two decimal places.
  4. Sort results by average mark from highest to lowest, then group name alphabetically.

Schema

dealer_groups
ColumnTypeDescription
group_idPKINTUnique dealer group identifier
group_nameVARCHAR(100)Dealer group name
marketVARCHAR(50)Primary geographic market
listings
ColumnTypeDescription
listing_idPKINTUnique vehicle listing identifier
group_idINTDealer group associated with the listing
vehicle_titleVARCHAR(150)Vehicle title displayed on CarGurus
marksINTListing-quality mark
Tablesdealer_groupslistings
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results