Data Engineering Interview Preparation #2: DuckDB SQL, Data Quality and Parquet
Summary: Practice SQL for data engineering with DuckDB and a small network-traffic dataset. This hands-on example covers table creation, filtering, joins, aggregations, window functions, Pandas and SQL result checks, data-quality rules, and a Parquet round trip.
SQL is a core tool for data engineers. It helps us explore source data, transform records, build analytical datasets, and check whether pipeline outputs are correct. You do not need to set up a separate database server to practice these patterns. With DuckDB, you can run analytical SQL directly from Python in a Kaggle notebook.
In this exercise, we use a small CSV containing network events. Each row includes a timestamp, source IP, destination IP, port, protocol, bytes sent, and event duration. The goal is not just to write queries, but to follow a simple engineering workflow: ingest the data, inspect it, transform it, validate it, and save a verified output.
Run the hands-on notebook: Data Engineering Interview Preparation #2: DuckDB SQL, Data Quality and Parquet on Kaggle. If you have not worked through the first notebook yet, start with Python and SQL Basics with Pandas and DuckDB.
Why use DuckDB for data engineering practice?
DuckDB is an embedded analytical database that works well with Python, Pandas, CSV, and Parquet. It is useful for hands-on exercises because you can run SQL in the same notebook where you inspect or prepare data. There is no separate database server to administer for this example.
DuckDB is designed for analytical workloads. It supports common SQL operations and can query data as part of a lightweight local workflow. For interview preparation, that makes it a practical way to demonstrate that you understand not only SQL syntax, but also how SQL fits into a data pipeline.
1. Create a table and inspect the source data
The first step is to load the CSV into a DuckDB table. In the notebook, we use CREATE TABLE AS SELECT with read_csv_auto to create a raw table from the file. We then inspect the inferred schema and query the largest transfers.
CREATE TABLE raw_network_traffic AS
SELECT * FROM read_csv_auto('network_traffic.csv');
Schema inspection is an important habit. Before building transformations, check that columns have sensible types and that the data is actually present. The notebook also prints a small sample to make the source structure easier to understand.
2. Use SELECT, WHERE, and JOIN
SELECT chooses the columns you need, and WHERE filters rows to a condition. A JOIN enriches records with attributes from another table.
The supplied CSV does not contain a protocol lookup table, so the notebook creates a small teaching lookup with one row for each protocol value in this sample. It joins that table to the traffic events and filters for records with at least 1,000 bytes sent. In a production pipeline, reference metadata would normally come from a managed source rather than being hard-coded in a notebook.
SELECT t.source_ip, t.destination_ip, t.protocol,
p.protocol_description, t.bytes_sent
FROM raw_network_traffic AS t
LEFT JOIN protocol_metadata AS p
ON t.protocol = p.protocol
WHERE t.bytes_sent >= 1000
ORDER BY t.bytes_sent DESC;
The notebook also checks that every protocol in the event data matched a lookup record. This is a simple example of validating a join, instead of assuming that it worked as expected.
3. GROUP BY and aggregations
GROUP BY combines events into summary rows. We calculate total bytes sent, event count, and average duration for each source IP. This kind of summary can support monitoring dashboards, operational reports, or a downstream analytical mart.
SELECT source_ip,
SUM(bytes_sent) AS total_bytes_sent,
COUNT(*) AS event_count,
AVG(duration_ms) AS average_duration_ms
FROM raw_network_traffic
GROUP BY source_ip
ORDER BY total_bytes_sent DESC;
Notice that the result has one row per source IP, not one row per network event. Understanding how aggregation changes the grain of a dataset is an important data modelling and interview concept.
4. Use a window function to rank events
Window functions calculate a value across related rows while preserving the individual event rows. The notebook uses RANK() to rank transfers by bytes sent within each protocol:
RANK() OVER (
PARTITION BY protocol
ORDER BY bytes_sent DESC
) AS rank_within_protocol
This answers a different question from GROUP BY. The aggregation summarizes each source IP, while the window query keeps each event and adds a rank that helps identify the largest transfers within a protocol.
5. Compare Pandas and SQL results
In data pipelines, the same transformation may be implemented in Pandas or SQL. The outputs should agree when both implementations apply the same logic. Looking at two tables and deciding that they “seem equal” is not enough, especially in a repeatable workflow.
In the notebook, we aggregate the network traffic by source_ip in Pandas, register the DataFrame with DuckDB, and run the matching SQL aggregation. We sort both outputs consistently and use pd.testing.assert_frame_equal to check their values. This turns parity into an executable test that can fail when a future change introduces a discrepancy.
If you want any of the following, send a message using the Contact Us (left pane) or message Inder P Singh (7 years' experience in Data Engineering, Gen AI, Agentic AI and ML) in LinkedIn at https://www.linkedin.com/in/inderpsingh/
- Production-oriented Data Engineering templates with playbooks
- Working Data Engineering projects for your portfolio
- Deep-dive hands-on Data Engineering Training
- Data Engineering resume updates
6. Run data-quality checks
A query returning rows does not prove the data is valid. The notebook checks whether the dataset is non-empty, looks for nulls in important columns, validates that ports fall within the numeric range 0 to 65,535, and verifies that bytes sent and durations are not negative. Assertions stop execution if a check fails.
These are intentionally small, understandable rules. A production pipeline would usually add requirements based on the source contract, such as valid timestamp formats, acceptable protocol values, duplicate handling, freshness checks, and monitoring of unexpected changes.
7. Export and verify Parquet
After validation, the notebook writes the raw network-traffic table to a canonical Parquet file using Zstandard compression. It then reads the file back into DuckDB and checks that the row count and column names match the original table.
This round-trip check is a useful smoke test. It does not replace full data-quality testing, but it helps catch basic file-writing or schema issues before another pipeline stage consumes the output.
What to practice for an interview
After running the notebook from top to bottom, try answering these questions in your own words:
- How does an embedded analytical engine differ from a client-server database?
- Why might a LEFT JOIN be appropriate when enriching event records with reference data?
- How does a window function differ from GROUP BY?
- Why compare Pandas and SQL outputs with an automated assertion?
- Which additional quality checks would you add before publishing a production data mart?
- How would you adapt the workflow for millions of records, incremental ingestion, and recovery after failure?
The key lesson is to treat SQL as one part of a reliable data workflow. Create a table, ask a clear analytical question, validate the result, compare alternative implementations when useful, and verify the saved output. The linked Kaggle notebook lets you run each step and experiment with the queries yourself.
Next in the series: reproducible data ingestion, transformations, automated checks, and production-minded patterns for building a data engineering portfolio.
Comments
Post a Comment