account_owner_hist is a slowly changing dimension (type 2). An open (current) row has valid_to NULL or valid_to = '9999-12-31' (two loaders used different conventions). Because of a load bug an account can have more than one open row; then the open row with the latest valid_from is the current one. Accounts whose rows are all closed have no current owner and are excluded; history for accounts that are not in accounts is ignored. Return account_id, account_name, current_owner and owner_since (valid_from of the current row). Order by account_id.
| Column | Type |
|---|---|
| account_id | INTEGER |
| account_name | TEXT |
| region | TEXT |
| account_id | account_name | region |
|---|---|---|
| 1 | Acme Corp | NA |
| 2 | Bluefin Ltd | EMEA |
| 3 | Cobalt Inc | APAC |
| 4 | Dune Media | NA |
| 5 | Echo Labs | APAC |
| Column | Type |
|---|---|
| account_id | INTEGER |
| owner_name | TEXT |
| valid_from | DATE |
| valid_to | DATE |
| account_id | owner_name | valid_from | valid_to |
|---|---|---|---|
| 1 | Ravi | 2024-01-01 | 2025-03-31 |
| 1 | Meera | 2025-04-01 | NULL |
| 2 | John | 2024-06-01 | 2025-12-31 |
| 2 | Aisha | 2026-01-01 | NULL |
| 3 | Li | 2025-02-01 | NULL |
| 5 | Raj | 2024-01-01 | 2025-01-31 |
| account_id | account_name | current_owner | owner_since |
|---|---|---|---|
| 1 | Acme Corp | Meera | 2025-04-01 |
| 2 | Bluefin Ltd | Aisha | 2026-01-01 |
| 3 | Cobalt Inc | Li | 2025-02-01 |
Solve it in the live editor on this page — run on sample data, then submit against hidden tests.
‹ Reporting Chain to the Top · Overlapping Licence Subscriptions ›
{{ totalCount }} questions in the style asked at Amazon, Microsoft, Google, Meta, Flipkart and more — {{ hardPct }}% Hard. Write SQL in a real database engine in your browser, run it on sample data, then evaluate against hidden tests.
Company tags show where this type of question is commonly reported in interviews. Questions, data and solutions are written by TechTown.
{{ cur.statement }}
No submissions yet. Press Evaluate to submit your query.
{{ solutionSql }}
{{ out.error }}
You have solved your 3 free questions — nice work! Log in (free) to unlock all 150 questions, keep your streak and appear on the leaderboard.