This phase is the best moment since we are not writing queries just to retrieve data but to answer business questions. First, I always set the schema to avoid repeatedly typing the schema name.
SET search_path TO hospital;
SHOW search_path;
1. Identifying all the insurance providers
SELECT insurance_provider
FROM patient_records
GROUP BY insurance_provider
ORDER BY insurance_provider ASC;
This query returned a clean list of unique insurance providers instead of displaying thousands of repeated values. It also introduced me to combining:
GROUP BY
ORDER BY for alphabetical sorting
Simple but incredibly useful when exploring unfamiliar data.
2. Calculating total hospital bills per Insurance provider
SELECT insurance_provider,SUM(bill_amount) AS total_bill
FROM patient_records
GROUP BY insurance_provider;
Instead of counting records, the SUM() aggregate function calculated the total billing amount for every insurance provider in just one query.
3. Filtering the hospital data
I wanted to get more data on each insurance provider individually. I opted to use filtering to get further information from each insurance provider. For example, to analyze Britam, I wrote:
SELECT payment_status,SUM(bill_amount) AS total_bill
FROM patient_records
WHERE insurance_provider ILIKE 'Britam'
GROUP BY payment_status;
Using the WHERE clause allowed me to focus on one insurance provider without being distracted by the rest of the dataset. One thing I learned was the usefulness of ILIKE in PostgreSQL. Unlike LIKE, ILIKE performs case-insensitive matching. That means: Britam, BRITAMand britam, would all return the same results. This makes queries more flexible, especially when datasets contain inconsistent capitalization. Repeating the same approach for NHIF, Jubilee, APA Insurance, AAR Healthcare, and uninsured patients helped reinforce an important lesson.
Top comments (0)