Essential cookies keep authentication working. With your permission, we also use analytics cookies to understand and improve the product. Read our Privacy Policy

DataEngPrep.tech
QuestionsPracticeAI CoachDashboardPricingBlog
ProLogin
Home/Questions/Python/Coding/How would you decide between using DISTKEY and SORTKEY?

How would you decide between using DISTKEY and SORTKEY?

Python/Codingmedium2 min read

Reviewed by Aditya Kumar · Last reviewed 2026-08-08

DISTKEY determines how data is distributed across compute nodes, aiming to co locate related rows for efficient joins and aggregations. SORTKEY defines the physical order of data within each…

🤖 Analyze Your Answer
Frequency
Low
Asked at 1 company
Category
179
questions in Python/Coding
Difficulty Split
127E|24M|28H
in this category
Total Bank
1,863
across 7 categories
Asked at these companies
Capco
Key Concepts Tested
joinpartition

Why This Question Matters

This medium-level Python/Coding question appears frequently in data engineering interviews at companies like Capco. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (join, partition) will help you answer variations of this question confidently.

How to Approach This

Break this problem into components. Identify the core trade-offs involved, then walk the interviewer through your reasoning step by step. Demonstrate awareness of edge cases and production considerations - this is what separates good answers from great ones. The expert answer includes a code example that demonstrates the implementation pattern.

Expert Answer
390 wordsIncludes code

DISTKEY determines how data is distributed across compute nodes, aiming to co-locate related rows for efficient joins and aggregations. SORTKEY defines the physical order of data within each distribution, optimizing range queries, filtering, and compression.

Mechanics and "Why"

DISTKEY (Distribution Key):
The DISTKEY specifies which column's values are used to distribute rows across the cluster's compute nodes (slices in Amazon Redshift). Rows with the same DISTKEY value are stored on the same slice.
* Why: This strategy is crucial for minimizing data movement (shuffle) during large table joins. When two tables are joined on their DISTKEY, the join can often occur locally on each slice without expensive network I/O, significantly improving query performance. It also benefits GROUP BY operations on the DISTKEY. A poorly chosen DISTKEY can lead to data skew, where some slices hold disproportionately more data, becoming performance bottlenecks.

SORTKEY (Sort Key):
The SORTKEY specifies the column(s) by which data is physically ordered within each slice. This can be a single column or a compound key (multiple columns).
* Why: Sorted data enables faster query execution through "zone map pruning" (or predicate pushdown). When a query filters on a SORTKEY column (e.g., a date range), the system can quickly skip large blocks of irrelevant data, reading only the necessary ones. This is similar to how micro-partitions work in Snowflake or Z-ordering in Delta Lake. Sorted data also generally compresses better and can improve the efficiency of merge joins and other operations.

Best Practices and Example

A common best practice is to choose your DISTKEY on the column most frequently used for large table joins (e.g., a foreign key to a large dimension table). Your SORTKEY should be on columns frequently used in WHERE clauses, especially date/timestamp columns for range scans, or high-cardinality columns for point lookups.

CREATE TABLE fact_sales (
    sale_id INT,
    sale_date DATE,
    customer_id INT,
    product_id INT,
    amount DECIMAL(10, 2)
)
DISTSTYLE KEY
DISTKEY (customer_id)
SORTKEY (sale_date, product_id);
In this example, customer_id as DISTKEY optimizes joins with a dim_customer table. sale_date and product_id as SORTKEY optimize queries filtering by date ranges and specific products.

In the interview, also mention…

Emphasize that the core goal of both is to optimize I/O and CPU utilization by ensuring data locality and enabling efficient pruning, which are fundamental concepts across distributed data systems, not just Redshift.

⚡
Pro Tip

Pro-Move: Compound SORTKEY. Red Flag: Random DISTKEY.

Want all answers as a PDF for offline study?
Seven focused volumes with 750+ in-depth answers — Answer Vault →

Related Python/Coding Questions

easyWhat are traits in Scala, and how are they different from classes?FreemediumWrite a Python function to check if a string is a palindrome.FreeeasyWhat is the difference between a list and a tuple in Python?FreeeasyExplain the difference between shallow copy and deep copy in Python.FreeeasyWrite a Python function to find the first non-repeating character in a string.Free

Level up your prep

Recommended
Educative
Educative Unlimited

800+ hands-on courses — Grokking System Design, Coding Patterns, and AI mock interviews for your DE loop.

Start learning →

Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.

According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.

← Back to all questionsMore Python/Coding questions →
Categories
All QuestionsSQLSpark / Big DataPython / CodingSystem DesignCloud / ToolsBehavioral
By Company
AmazonGoogleDatabricksSnowflakeAWSAzureMicrosoftNetflixUberTCS
Interview Guides
All GuidesTop SQL QuestionsTop Spark QuestionsPySpark QuestionsTop Python QuestionsTop System DesignKafka QuestionsAirflow QuestionsSQL Window FunctionsETL QuestionsData Modeling
Products
AI Interview CoachAnswer AnalyzerSQL PlaygroundResume AnalyzerAnswer Vault PDFsPricing
Company
About & Editorial PolicyContact UsAI DisclosureDisclaimerTerms of ServicePrivacy Policy
© 2026 DataEngPrep.tech. All rights reserved.
AboutBlogContactDisclaimer