# A first data project for your portfolio, from zero

Pick one question a small dataset can answer. Keep the raw file untouched and clean it in SQL with DuckDB, which needs no server or account. Send bad rows to a rejects table with a reason, answer the question in one query, test that raw rows equal clean plus rejected rows, and write a README that states the limits.

- Checked on 2026-11-29
- Rodrigo Lombana Reina, https://thefabricofdata.com/profile/
- https://thefabricofdata.com/patterns/first-data-project-portfolio-duckdb/

## The problem
You have finished the courses and have nothing to show. Your notebooks have no question, the data is a famous toy dataset, and nobody can run your code without an afternoon of setup. This Pattern is for people starting in data who need one small project that answers one question and that a reader can open and run in a few minutes.

## Steps
![Raw CSV with 300 rows never edited, silver with 292 clean rows, gold with revenue per month and category, then the answer. Eight rejected rows sit under silver with a reason each.](https://thefabricofdata.com/_astro/11-project-flow.C22y0ciC.svg)

*The shape of the project. 300 raw rows equal 292 silver rows plus 8 rejected rows.*

The example answers a small question on synthetic orders: which category earns the most revenue each month. You keep the method and replace the question and the data. The advice about what a reader values is the author's opinion; no source measures it.

1. **Write the question before you touch the data.** Make it the first line of the README. A good one names a measure, a split and a period: "Which category earns the most revenue each month?" If you cannot write it, you are not ready to open the data.

2. **Get data you are allowed to use.** Either generate it, or take a public dataset whose licence you have read and copy that licence into the README. A script with a fixed seed gives every reader the same file. This one adds eight problems on purpose (five repeated order ids, an empty units value, a negative price and 31 February), because real files have them.

   ```bash
   python generate_data.py
   ```

   You should see `wrote data/sales_raw.csv`.

3. **Install DuckDB.** It is a Python package; there is no server to set up and no account. DuckDB's own page says it runs "completely embedded within a host process".

   ```bash
   pip install duckdb==2.0.1
   ```

4. **Profile the raw file before cleaning it.** Load everything as text first, so that nothing is lost or converted silently.

   `all_varchar = true` skips type detection and reads every column as text. `project.py` runs this with the file path as a parameter.

   ```sql
   CREATE TABLE raw_orders AS
   SELECT * FROM read_csv('data/sales_raw.csv', header = true, all_varchar = true);
   ```

   Then count. `try_cast` returns NULL instead of raising an error when a value does not convert.

   ```sql
   -- Step 1: look before you clean. One row of numbers about the raw file.
   SELECT
       count(*)                                                   AS rows,
       count(DISTINCT order_id)                                   AS distinct_order_ids,
       min(try_cast(order_date AS DATE))                          AS first_date,
       max(try_cast(order_date AS DATE))                          AS last_date,
       count(*) FILTER (WHERE try_cast(order_date AS DATE) IS NULL) AS bad_dates,
       count(*) FILTER (WHERE try_cast(units AS INTEGER) IS NULL)   AS missing_units,
       count(*) FILTER (WHERE try_cast(unit_price AS DECIMAL(10,2)) <= 0) AS non_positive_prices
   FROM raw_orders;
   ```

   The run printed 300 rows, 295 distinct order ids, 1 bad date, 1 missing units value and 1 non-positive price.

5. **Clean in SQL and never edit the raw file.** Give every raw row exactly one destination: the clean table (silver) or a rejects table with a reason. Rows are counted, not dropped quietly.

   ```sql
   -- Step 2: clean in SQL. The raw file is never edited.
   -- Every raw row ends up in exactly one place: silver_orders or rejects.
   CREATE OR REPLACE TABLE typed AS
   SELECT
       row_number() OVER ()                                   AS raw_row,
       try_cast(order_id AS INTEGER)                          AS order_id,
       try_cast(order_date AS DATE)                           AS order_date,
       upper(left(trim(category), 1)) || lower(substr(trim(category), 2)) AS category,
       try_cast(units AS INTEGER)                             AS units,
       try_cast(unit_price AS DECIMAL(10,2))                  AS unit_price
   FROM raw_orders;

   CREATE OR REPLACE TABLE checked AS
   SELECT *,
       CASE
           WHEN order_id IS NULL                               THEN 'bad_order_id'
           WHEN order_date IS NULL                             THEN 'bad_date'
           WHEN units IS NULL OR units <= 0                    THEN 'bad_units'
           WHEN unit_price IS NULL OR unit_price <= 0          THEN 'bad_price'
           WHEN row_number() OVER (PARTITION BY order_id ORDER BY raw_row) > 1
                                                               THEN 'duplicate_order_id'
       END AS reject_reason
   FROM typed;

   CREATE OR REPLACE TABLE silver_orders AS
   SELECT order_id, order_date, category, units, unit_price,
          units * unit_price AS revenue
   FROM checked WHERE reject_reason IS NULL;

   CREATE OR REPLACE TABLE rejects AS
   SELECT raw_row, order_id, reject_reason FROM checked WHERE reject_reason IS NOT NULL;
   ```

   The run printed `raw rows 300 = silver rows 292 + rejected rows 8`. Five rows were rejected as repeated order ids, one for a bad date, one for a bad price and one for missing units. Three category spellings (` games`, `BOOKS`, `Home `) were fixed, not rejected. The duplicate rule keeps the first row in file order, so it depends on the file keeping its order.

6. **Answer the question with one query.** First a gold table with one row per month and category, then the answer. `QUALIFY` filters on a window function, here the best row per month.

   ```sql
   -- Step 3: the answer table. One row per month and category.
   CREATE OR REPLACE TABLE gold_monthly_revenue AS
   SELECT strftime(order_date, '%Y-%m') AS month,
          category,
          sum(revenue)                  AS revenue,
          count(*)                      AS orders
   FROM silver_orders
   GROUP BY ALL;

   -- Step 4: the question. Which category earns the most each month?
   SELECT month, category, revenue
   FROM gold_monthly_revenue
   QUALIFY row_number() OVER (PARTITION BY month ORDER BY revenue DESC, category) = 1
   ORDER BY month;
   ```

   The synthetic data gave Games for January, February and June 2026, Home for March and Books for April and May. These are random numbers and prove nothing about a business.

7. **Test it, so the numbers stay true.** The most useful test is the balance of rows (excerpt from `tests/test_project.py`). A second test recomputes the clean total in plain Python, outside SQL, and compares it with the gold table.

   ```python
   def test_every_raw_row_lands_in_exactly_one_place(con):
       raw = con.execute("SELECT count(*) FROM raw_orders").fetchone()[0]
       silver = con.execute("SELECT count(*) FROM silver_orders").fetchone()[0]
       rejected = con.execute("SELECT count(*) FROM rejects").fetchone()[0]
       assert raw == silver + rejected == g.N_ORDERS
   ```

8. **Write the README with six sections.** Question, Data, Method, Result, Limits, Next. Add the command that runs everything. The project includes a template with these headings. Limits is the section that shows judgement: say what the data cannot tell.

9. **Publish it and be ready to explain every line.** Put it on GitHub and pin it to your profile. If you used an AI assistant to draft a query, explain it in your own words before anyone asks.

## The project
`first-data-project` is a small Python and DuckDB project with synthetic data. It contains the data generator, four SQL files (profile, silver, gold, answer), a runner that prints a text chart, 13 tests and a README template.

```bash
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
python run.py && python -m pytest -v
```

What the recorded run showed (full output in `RUN_LOG.txt`, inside the project):

- The profile found the planted problems: 300 rows, 295 distinct ids, one bad date, one missing units value, one non-positive price.
- 300 raw rows became 292 silver rows and 8 rejected rows. Total revenue in silver was 24367.25.
- The top category per month came out as in step 6, and 13 tests passed. One of them checks that the raw file has the same hash after the run.

![Terminal output of the project and the test summary.](https://thefabricofdata.com/_astro/11-run-output.DShgLdUx.svg)

*Real output of `python run.py` and the test summary, DuckDB 2.0.1, 2026-11-29. Some lines shortened.*

## Copy this prompt

If you want a portfolio project on a different topic, paste this into your assistant’s chat and share your interests and the tools you already know. The assistant will suggest three questions you can answer with small datasets and outline how to solve and document them yourself.

```text
I am starting in data and want a portfolio project.
My interests: <topic>. Tools I know: <SQL | Python | Excel | none>.
Give me 3 business questions I could answer with a small dataset in DuckDB.
For each: the data I would need, whether I can generate it or must find a public
dataset with a licence, 4 SQL steps, and the README sections.
Do not write the project. After each step, ask me to explain it back
before you continue. Use only fake or public data; never ask for real personal data.
```
Check the answer for three things: a question with a measure and a period, a data source whose licence you can read, and a step where you explain your own work.

## Test result

The DuckDB exercise separated clean from rejected rows and checked that every original row was accounted for.

## Limits
- **Synthetic data proves the method, not a fact.** The numbers are random. A real result needs real data you may use, with its licence in the README.
- **Public datasets were not checked.** Each has its own licence and terms. Read them before you publish anything built on one.
- **The advice on what readers value is opinion.** Nothing here measures what a recruiter or a hiring manager prefers.
- **Rejecting is a decision.** Dropping a row for a bad price or a repeated id changes the total. Count the rejected rows and say why, as the project does.
- **File order matters for duplicates.** The first row of a repeated id wins. If your file has no stable order, choose the winner by a column, such as a timestamp.
- **The chart is text.** It keeps the project free of extra packages. Use a plotting library if you want an image.
- **One project is a start.** It does not show cloud tools, orchestration or large data. Those are later steps (Pattern 07 covers the layers).

## Sources
- [Why DuckDB](https://duckdb.org/why_duckdb.html), accessed 2026-11-29
- [DuckDB Python client overview](https://duckdb.org/docs/2.0/clients/python/overview.html), accessed 2026-11-29
- [DuckDB CSV import overview](https://duckdb.org/docs/2.0/data/csv/overview.html), accessed 2026-11-29
- [DuckDB QUALIFY clause](https://duckdb.org/docs/2.0/sql/query_syntax/qualify.html), accessed 2026-11-29
- [DuckDB friendly SQL (GROUP BY ALL)](https://duckdb.org/docs/2.0/sql/dialect/friendly_sql.html), accessed 2026-11-29
- [DuckDB typecasting (TRY_CAST)](https://duckdb.org/docs/2.0/sql/data_types/typecasting.html), accessed 2026-11-29
- [DuckDB licence (MIT)](https://github.com/duckdb/duckdb/blob/main/LICENSE), accessed 2026-11-29

## Author
Rodrigo Lombana Reina, data engineer. Personal opinions. Checked against the documentation on 2026-11-29.

## Questions

### What is a good first data project for a portfolio?

One that answers a single clear question with a small dataset, shows how you cleaned it, and runs in minutes. The example here uses DuckDB 2.0.1 and SQL to find the top revenue category per month, rejects eight bad rows with a reason each, and ends with a README. Scope matters more than size. See the section The project.

### How do I build a data portfolio project step by step?

Write the question, get data you may use, profile it, clean it in SQL without touching the raw file, answer with a few queries, add tests, and write a README with Question, Data, Method, Result, Limits and Next. Steps 1 to 9 of this Pattern do exactly that with DuckDB 2.0.1 and a synthetic CSV file.

### What is the main mistake in a first data project?

In the author's opinion, starting without a question. A famous toy dataset, a notebook of charts and no stated purpose give a reviewer nothing to judge. Next come a missing README, no way to run it, and claims the data cannot support. The project here starts from one question and states in its README what the data cannot say.

### Do I need a cloud account or a server to practise SQL?

No. DuckDB 2.0.1 installs with pip install duckdb and runs inside your Python process, so there is no database server to set up and no account to create. It reads a CSV file directly with SQL. Cloud tools such as Microsoft Fabric are a later step for a portfolio, not a requirement for a first project.

### Can I use synthetic data in a data portfolio project?

Yes, if the README says so and you claim only the method. The generated data in this project is random, so its top category per month, for example Games in January 2026 at 1663.80, means nothing about any business. Synthetic data shows that your pipeline works; it cannot support a business conclusion.

### Should I use SQL files or a notebook for a portfolio project?

Either can work. This is the author's choice, not a rule. The project keeps each step in a short SQL file called from a small Python script, so a reader can open one file per step in the repository and read what was done, and tests can check the result. A notebook is fine for exploring the data first.

### Which DuckDB version and licence does the project use?

DuckDB 2.0.1, pinned in requirements.txt, released under the MIT licence according to the DuckDB project's own pages. The run was recorded on 2026-11-29 with Python 3.14.8 and pytest 9.2.0. QUALIFY and GROUP BY ALL are DuckDB syntax, and the project was not run on any other database engine.
