DEV Community

John Wakaba
John Wakaba

Posted on

COMPARISON OPERATORS (POSTGRES)

BETWEEN

It is case sensitive with string data types.

It is a crucial operator for filtering data within a specific range.

It signifies range based conditionsincluding both the lower and upper boundaries(inclusive of the two values)

SELECT * FROM meds WHERE price **BETWEEN** 1000 AND 4000; 
Enter fullscreen mode Exit fullscreen mode

In a date Scenario

SELECT * FROM patients WHERE appointment_date **BETWEEN** '2025-01-01 **AND** 2025-10-10'; 
Enter fullscreen mode Exit fullscreen mode
  • When making date queries date literals should be in ISO 8601 format YYY-MM-DD

When seeking to exclude values one can use the NOT operator.

IN

It is used to filter records by matching any value in a list

Simplifies complex queries, replacing multiple OR conditions with a single IN clause.

Checking against a list of values:

SELECT * FROM patient WHERE city **IN** ('Nairobi', 'Mombasa'); 
Enter fullscreen mode Exit fullscreen mode

Using a Subquery

SELECT f_name, l_name FROM patient WHERE patient_id **IN** (SELECT patient_id from doctors WHERE diagnosis='Malaria'); 
Enter fullscreen mode Exit fullscreen mode
  • For large datasets use EXISTS/JOIN operators instead of IN operator.

LIKE OPERATOR

It is essential for pattern matching

LIKE is used to search for patterns in text data.

  • % means any number of characters

Utilizes wild card search techniques and is commonly used with the WHERE clause for filtering records.

% Matching any sequence of characters

_ Matching any single characters

The above are two special wildcard characters

Patients with the fist name starting with K

SELECT * FROM patient WHERE f_name LIKE 'K%'; 
Enter fullscreen mode Exit fullscreen mode

Patients with the fist name containing bert

SELECT * FROM patient WHERE f_name LIKE ''%bert%'; 
Enter fullscreen mode Exit fullscreen mode

Patients with the email ending with gmail.com

SELECT * FROM patients WHERE email LIKE '%gmail.com'; 
Enter fullscreen mode Exit fullscreen mode

Using the underscore

Patients where the first name begins with any single character and is followed by string 'oma' and ends with any number of characters

SELECT * FROM patient WHERE f_name LIKE '_oma%'; 
Enter fullscreen mode Exit fullscreen mode

NOT LIKE

Used whenever we want to exclude records that match a certain pattern.

Best fit for when filtering out data that does not fit a certain criteria.

WHERE first_name NOT LIKE 'ken%'

ILIKE

Used when performing case insensitive pattern matching

WHERE first_name ILIKE 'ken%'

  • It matches any string starting with 'Ken', 'ken', 'KEN' ignoring the others

JOINS BRIEF

Joins allow you to combine related data from different tables into one result set.

JOIN DESCRIPTION
INNER Returns only rows that have matching values in both tables
LEFT Returns all rows from left and matching from right table (IF NO MATCH FILLS IN NULL)
RIGHT Returns all rows from right and matching from the left table
CROSS Returns every combination of rows from both tables cartesian product
SELF Table joins with itself There is need to use aliases Used to model relationships within one table
NATURAL Automatically joins tables using all columns that have the same name
FULL OUTER LEFT + RIGHT all rows from both tables if no match, uses null

Top comments (0)