Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Rank Redshift Tables by Optimization Priority
00:00
5 left

Rank Redshift Tables by Optimization Priority

HardSQL · PostgreSQL

Problem

How do you optimize query performance and manage storage costs in Amazon Redshift?

Using the supplied query history, table usage, and storage metadata, write a SQL query that identifies tables requiring optimization attention. Consider completed queries executed during January 2025 and include tables with no qualifying query activity.

Output

  1. One row per stored table with table_name, query_count, total_runtime_ms, avg_runtime_ms, storage_mb, and optimization_priority.
  2. Classify priority as HIGH, MEDIUM, or LOW using runtime, storage, dead-space, and unused-table signals.
  3. Order by total runtime descending, then storage size descending.

Output details

  • Treat January as executed_at >= TIMESTAMP '2025-01-01 00:00:00' and < TIMESTAMP '2025-02-01 00:00:00'.
  • Round avg_runtime_ms and storage_mb to two decimal places, leaving avg_runtime_ms NULL when no qualifying queries exist.
  • Use zero for query_count and total_runtime_ms when no qualifying queries exist, while preserving NULL averages.
  • Compute storage_mb as total_bytes / 1048576.0 before rounding.
  • Assign HIGH for total runtime at least 1000, dead-space ratio at least 0.40, or unused storage at least 10 MiB.
  • Otherwise assign MEDIUM for unused tables, total runtime at least 500, or dead-space ratio at least 0.20; assign LOW otherwise.

Schema

query_log
ColumnTypeDescription
query_idPKINTUnique query identifier
executed_atTIMESTAMPQuery execution timestamp
runtime_msINTQuery runtime in milliseconds
statusVARCHAR(20)Query completion status
query_table_usage
ColumnTypeDescription
query_idINTReferenced query identifier
table_idINTReferenced table identifier
table_storage
ColumnTypeDescription
table_idPKINTUnique table identifier
table_nameVARCHAR(100)Stored table name
total_bytesBIGINTTotal storage consumed in bytes
dead_bytesBIGINTEstimated reclaimable or dead storage in bytes
Tablesquery_logquery_table_usagetable_storage
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the three tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.