DEV Community

Mungai Erick Gachuhi
Mungai Erick Gachuhi

Posted on

SQL BASICS

During the past few days I have learned a few basics on SQL and would like to share here.
After the first lesson, 'SELECT * FROM table' felt like enough. However, after the third lesson as well as an assignment, I worked with a hospital database of 15 patients. I have now realized the real power is in asking precise questions: Which patients have no insurance? Whose name starts with K? Who needs a phone call because there is no email on file?

In this article I walk through the operators I practiced in PostgreSQL using DBeaver, with queries from my own assignment.

The dataset

I worked with a 'patients' table with columns such as 'first_name,' 'gender,' 'age,' 'county,' and 'admission_date.' Some emails and insurance values were left empty on purpose, which turned out to be very important later.

1. Comparison operators

These compare a column with a value: '=', '<>', '>', '<', '>=' and '<='.
Examples include
-- To select patients from Kisumu County.
The code is:
SELECT * FROM patients WHERE county = 'Kisumu';

-- Patients admitted on or after 1 April 2026
SELECT * FROM patients WHERE admission_date >= '2026-04-01';

Text and dates go in single quotes; numbers don't. '<> ' means "not equal to," so 'department' <> 'General' returns everyone outside the General department.

2. Logical operators: AND, OR, NOT

Logical operators combine conditions. 'AND' needs both conditions to be true, 'OR' needs at least one, and 'NOT' reverses a condition.

Examples include:
-- Male patients over 40, or anyone with a bill above 150,000 (5 rows)
SELECT * FROM patients
WHERE (gender = 'Male' AND age > 40) OR bill_amount > 150000;

SQL evaluates 'AND' before 'OR,' just like multiplication comes before addition in math. I still use brackets because they show what I mean, and they protect me when conditions get more complicated.

3. IN and BETWEEN

When I needed patients from Nairobi or Mombasa, I first wrote it with OR:

An example is
SELECT * FROM patients
WHERE county = 'Nairobi' OR county = 'Mombasa';

'IN' does the same job in a shorter and cleaner way:

An example is:
SELECT * FROM patients
WHERE county IN ('Nairobi', 'Mombasa');

'BETWEEN' filters a range, and it includes both endpoints. For example, 'bill_amount BETWEEN 10000 AND 50000' is the same as 'bill_amount >= 10000 AND bill_amount <= 50000'. It works with dates too, and NOT BETWEEN returns everything outside the range.

4. Handling NULL

NULL means a value is missing or unknown. It is not zero, and it is not an empty string. To find missing data, use 'IS NULL' or 'IS NOT NULL':

An example is:
-- Patients with no email address
SELECT * FROM patients WHERE email IS NULL;

SELECT * FROM patients
WHERE insurance IS NULL AND bill_amount > 10000

If you're learning SQL too, build a small table and break these operators on purpose. You'll remember the mistakes much better than the rules.

Top comments (1)

Collapse
 
alexshev profile image
Alex Shev •

For a SQL basics guide, adding one example that contrasts a filtered aggregate with a WHERE clause helps learners avoid a common mistake. Showing the input rows and expected result makes the difference easier to reason about than syntax alone.