Comparison Operators

In SQL and Ecto, comparison operators are used to compare values, allowing you to filter results based on specified conditions.

Comparison operators

In SQL and Ecto, comparison operators are the same except for the equality operator. In SQL, = is used for equality, while in Ecto, == is used.

In SQL

= - Equal to

!= - Not equal to

\< - Less than

\> - Greater than

\>= - Greater than or equal to

<= - Less than or equal to

In Ecto

== - Equal to

!= - Not equal to

\< - Less than

\> - Greater than

\>= - Greater than or equal to

<= - Less than or equal to

SQL

Example 1:

Let's select those records from employees whose salary is less than 6000 using the less than (<) operator.

select * from employees where salary < 6000;

Result:

Ecto

HR.Employee
|> where([c], c.salary < 6000)
|> HR.Repo.all()

Example 2:

let's select all locations in our table, whose country_id is equal to 'US', using the equal to (=) operator.

select * from locations where country_id = 'US';

Ecto

HR.Employee
|> where([c], c.country_id == US)
|> HR.Repo.all()