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
Pivot and Unpivot in Alteryx and SQL
00:00
5 left

Pivot and Unpivot in Alteryx and SQL

MediumSQL · PostgreSQL

Problem

How would you pivot and unpivot the same table in Alteryx and in SQL?

Use the sales_matrix table below. Write SQL that returns the pivoted result, then write SQL that converts that pivoted result back to the original row format.

Output

  1. For the pivoted result, return one row per region with separate columns for each product_name.
  2. For the unpivoted result, return one row per region and product_name pair with the corresponding sales_amount.
  3. Keep the output deterministic by sorting by region, then product_name where applicable.

Schema

sales_matrix
ColumnTypeDescription
sales_idPKINTUnique row identifier
regionVARCHAR(50)Sales region
product_nameVARCHAR(50)Product name
sales_amountINTSales amount for the region and product
Tablessales_matrix
Interviewer

Your question is Pivot and Unpivot in Alteryx and SQL. 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.