understanding data cleaning without modification in postgressql
Welcome! I'm excited to help you master non-destructive data cleaning in PostgreSQL. It's a game-changing skill because it lets you fix messy data for analysis while keeping your original source tables absolutely pristine and untouched.
Before we map out our learning path, I'd love to learn a little bit about you so I can make this session as valuable as possible. Could you tell me a bit about your current SQL experience and the specific types of data cleaning tasks you're hoping to perform?
I know basic SQL, and I want to learn how to use views and CTEs to clean up messy business data.
That is exactly what we will set up today. Having some basic SQL experience is the perfect foundation because non-destructive cleaning is all about writing smart queries rather than making permanent changes to your database tables.
The Goal of Non-Destructive Cleaning
When you are handed a messy corporate database, your first instinct might be to reach for an UPDATE or DELETE statement. But in professional environments, modifying original raw tables is highly dangerous. If you make a mistake in your logic, you can easily corrupt or permanently lose valuable data.
Non-destructive data cleaning is the practice of transforming data in-memory during query execution, leaving the underlying completely untouched. Think of it like looking at a messy room through a special pair of glasses that instantly organizes everything in your field of vision, without actually moving a single piece of furniture in the physical world.
By using techniques like virtual views and Common Table Expressions (CTEs), we can construct clean, polished outputs for our business reports on-the-fly. The raw data remains pristine, and your analysts get the clean data they need. Shall we look at how this fits into a standard data engineering workflow next?
how to count the number of now cells in a column
That is a perfect place to start. In the world of database systems, a blank cell can represent missing or uncollected information, and in PostgreSQL, we refer to these empty spaces as .
Because a NULL value represents the complete absence of data, standard mathematical operations like value = NULL do not work. If you try to find them using an equal sign, PostgreSQL will simply return empty results because you cannot mathematically compare something to the "unknown."
Instead, SQL gives us a dedicated tool to hunt down these empty spaces safely: the IS NULL operator.
A parallel evaluation of five database cells testing the IS NULL and = NULL conditions in PostgreSQL.
By pairing IS NULL with a WHERE clause, we can easily isolate and filter our data. For example, if we want to look at every record in our orders table where the shipping_date was never entered, we can write a clean SELECT query:
SELECT *
FROM orders
WHERE shipping_date IS NULL;
This simple filter lets us peek at our incomplete records without altering a single row in the database. Next, let's look at how we can count up all of these empty cells so we can see the scale of our messy data.
What happens if I try to use IS NOT NULL instead?
Using IS NOT NULL does exactly what you'd expect: it flips our search light to find only the rows that actually contain data.
While IS NULL filters out everything except the empty spaces, IS NOT NULL filters out the empty spaces and keeps only the completed entries. It is the logical inverse of IS NULL.
For example, if you want to pull a report of all orders that have successfully shipped, you would filter out the orders missing a shipping date by writing:
SELECT *
FROM orders
WHERE shipping_date IS NOT NULL;
This is incredibly useful because it lets you isolate your immediately. You can focus your analysis on active, complete transactions while ignoring the gaps entirely.
Now that you can find both populated and unpopulated rows, let's look at how we can get a high-level summary of our data. To do this, we need to introduce PostgreSQL's COUNT() function.
Continue to counting missing data with COUNT()
Let's put our counting tools to work! When you want to summarize how much missing data you are dealing with, PostgreSQL gives us the COUNT() to do the heavy lifting safely in memory.
To find out exactly how many rows are missing a value in a column, we can combine COUNT() with our WHERE clause.
Let's look at a concrete example. Imagine we have an orders table, and we want to know exactly how many orders are waiting for a shipping date. We can target and count those specific empty records like this:
SELECT COUNT(*)
FROM orders
WHERE shipping_date IS NULL;
When this query runs, PostgreSQL first scans the table to filter only the rows where shipping_date is indeed NULL. Then, it counts up those matched rows and hands you back a single, neat number. Your underlying table remains completely untouched, but you instantly get a clear metric of your missing data.
A three-stage database execution pipeline counting NULL values in a five-row table, filtering with WHERE IS NULL, and outputting 2.
Now that you can quantify exactly how much missing data you have, you're ready to start fixing it. In our next step, we will explore techniques for safe data transformation, where we actually replace those blank spaces with useful default values on-the-fly.