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
GROUP BY and SUM OVER Outputs
00:00
5 left

GROUP BY and SUM OVER Outputs

MediumSQL · PostgreSQL

Problem

Given two SQL snippets using GROUP BY and SUM() OVER(), what are their outputs?

Asked in the First round stage. Use the sales table and evaluate these PostgreSQL queries:

SELECT department, SUM(amount) AS total_amount
FROM sales
GROUP BY department
ORDER BY department NULLS LAST;
SELECT department,
       SUM(SUM(amount)) OVER () AS grand_total
FROM sales
GROUP BY department
ORDER BY department NULLS LAST;

Output

  1. Return both outputs in one result set with columns query_name, department, and result_value.
  2. Label rows from the first query grouped_total and rows from the second query window_grand_total.
  3. Preserve department ordering, with NULL departments last, and place the first query's rows before the second query's rows.

Schema

sales
ColumnTypeDescription
sale_idPKINTUnique sale identifier
departmentVARCHAR(50)Department associated with the sale
amountNUMERIC(10,2)Sale amount, which may be NULL
Tablessales
Interviewer

Your question is GROUP BY and SUM OVER Outputs. Start with the requirements and the one table 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.