A Database Is like a Messy Wardrobe. SQL Is How You Find the Right Cloth.

You could be new to SQL(postgres) or probably transitioning careers, and maybe you don't have a guide. For querying this the easy way,think of it like a cluttered wardbobe and with one or a few query(ies) you declutter and find the right cloth. That's how you get to know your dataset.

Dataset I'll use has this info:
Table_name: sales_data
Columns:

  1. order_id
  2. order_date
  3. customer_id
  4. customer_name
  5. city
  6. region
  7. product_id
  8. product_name
  9. category
  10. unit_price
  11. quantity
  12. discount_pct
  13. total_amount
  14. payment_method
  15. sales_rep
  16. order_status

NOTE://

  • where: is a clause used to filters values that meets the condition.
  • * is a symbol for all, basically means you're querying all columns from the table.

Filtering our data :

1. Comparison (=, <>/!=, >, <, >=, <=)

  • Equal to =
  • Not Equal <>/!=
  • Greater than >
  • Less than <
  • Greater than or equal to >=
  • Less than or equal to <=

Let's use any of the comparison operators, the syntax is the same only operator changes.
For example: Filter orders where quantity is 3 or more.

select * from sales_data where quantity >=3; 

2. Logical (AND, OR, NOT, and parentheses)

  • AND: returns results when both conditions are met.
  • OR: returns results when one condition is met.
  • NOT: negates the condition or queries what does not match the criteria or viceversa of (true and false).

Let's use each operator and see how it works.

For example: Filter orders made by clients in Nairobi and Mombasa.

select * from sales_data where city = 'Nairobi' and city = 'Mombasa'; 

For Example: Filter cancelled or returned orders.

select * from sales_data where order_status = 'Cancelled' or order_status = 'Mombasa'; 

For example: Filter orders in the Eastern region that are not Completed.

select * from sales_data where region = 'Eastern' and not order_status = 'Completed'; 

Explanation : The AND is to combine the conditions but the NOT has to query orders where the order status was not completed.

3. Range/List (BETWEEN, IN, NOT IN)

  • Between: includes the beginning and end values of the range.
  • IN: checks if the conditions matches the values in the list.
  • NOT IN: excludes the values that match the list condition.

For example: Filter orders placed between mid-march and mid-may 2025.

select * from sales_data where order_date between '2025-03-15' and '2025-05-15'; 

For example: Filter orders paid by Card or Bank Transfer.

select * from sales_data where payment_method in ('Card', 'Bank Transfer'); 

For example: Filter orders made by clients out of Nairobi, kisumu, mombasa.

select * from sales_data where city not in ('Nairobi', 'Kisumu', 'Mombasa'); 

4. NULL (IS NULL, IS NOT NULL)

  • IS NULL: checks for empty values.
  • IS NOT NULL: checks for non empty values.

For example: Filter orders without discount.

select * from sales_data where discount_pct is null; 

For example: Filter orders that have a discount.
This will include zeros.

select * from sales_data where discount_pct is not null; 

A query for you: Filter orders with discount but not where values were zeros since it still mean the discount is zero only that is not empty.

Check the clause below

select * from sales_data where discount_pct is not null and discount_pct !=0; 

Explanation: Let me explain a bit. We've IS NOT NULL, AND and !=
Is not null retrieves non empty values, the AND makes sure both conditions have to be true, != means not equal to or it negates the equal to so it retrieves orders where discount is not null and not zero basically meaning there must be a discount.

5. LIKE wildcards(%, _)

  • %: shows zero, one or multiple characters.
  • _: shows a single character.

For example: Filter products with 'inch' in the name.

select * from sales_data where product_name like '%inch%'; 

For example: Filter customer Id whose name starts with C1.

select * from sales_data where customer_id like 'C1__'; 

Explanation: I used two underscores(__) to specify the ID should end with 2 characters.

Wrap Up.

For a beginner, dataset can feel like a messy wardrobe. All you need to do first is go through it and check the columns and data types. With a few filters you can understand it.
Then, ensure for every query you analyze which columns and which clauses you need to get the expected output before writing the code.
At last, One common mistake to avoid : don't forget to close with ;.