Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL Aggregation Over Positives and Negatives

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

Your question is SQL Aggregation Over Positives and Negatives. 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

3Pillar's project delivery dashboard stores financial adjustments and credits as positive or negative work log values. Write a PostgreSQL query that reports the separate positive and negative totals for every project, including projects without work log entries.

Requirements

  1. Use a CTE to classify each work log amount as either a positive value or a negative value.
  2. Return one row per project with positive_sum and negative_sum.
  3. Treat zero and NULL amounts as contributing zero to both totals.
  4. Use a LEFT JOIN so projects without work logs remain in the result, and order the output by project ID.

Schema

projects
ColumnTypeDescription
project_idPKINTEGERPrimary key for the project
project_nameVARCHAR(100)Project name
work_logs
ColumnTypeDescription
work_log_idPKINTEGERPrimary key for the work log
project_idINTEGERReferences projects.project_id and may be NULL for unassigned entries
amountDECIMAL(12,2)Positive, negative, zero, or NULL adjustment
entry_labelVARCHAR(100)Short description of the work log entry
Tablesprojectswork_logs
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results