DATA with BARAA

SQL Comparison Operators

Examples

SQL Comparison Operators

In this tutorial, you will learn about SQL comparison operators and how to use them to build up conditions using the WHERE clause. 

Using comparison operators is the most basic way to filter data. Lets check the following list in order to understand them:

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

The result of comparison operators is always TRUE or FALSE .

Example > Operator

To understand the comparison operators in a better way, let’s look at the following customers table in our tutorial database:

Customers
customer_id first_name last_name country score
1 Maria Cramer Germany 350
2 John Steel USA 900
3 Georg Pipps UK 750
4 Martin Müller Germany 500
5 Peter Franken USA NULL

Now, let’s check out some examples that demonstrate how they actually works.

We have the following task be to solve using SQL statements

Find all customers whose score is higher than 500

The following SQL statement will returns only customers with score higher than 500

				
					SELECT *
FROM customers
WHERE score > 500
				
			

After executing the above query, you’ll get the result set something like this:

customer_id first_name last_name country score
2 John Steel USA 900
3 Georg Pipps UK 750

As you can see the output contains only customers with score higher than 500

Example < Operator

We have the following task be to solve using SQL statements

Find all customers whose score is lower than 500

The following SQL statement will returns only customers with score smaller than 500

				
					SELECT *
FROM customers
WHERE score < 500
				
			

After executing the above query, you’ll get the result set something like this:

customer_id first_name last_name country score
1 Maria Cramer Germany 350

As you can see the output contains only customers with score lower than 500

.

Example >= Operator​

We have the following task be to solve using SQL statements

Find all customers whose score is higher than or equal to 500

The following SQL statement will return only customers with score higher than or equal to 500

				
					SELECT *
FROM customers
WHERE score >= 500
				
			

After executing the above query, you’ll get the result set something like this:

customer_id first_name last_name country score
2 John Steel USA 900
3 Georg Pipps UK 750
4 Martin Müller Germany 500

Example <= Operator​

We have the following task be to solve using SQL statements

Find all customers whose score is less than or equal to 500

The following SQL statement will return only customers with score higher than or equal to 500

				
					SELECT *
FROM customers
WHERE score <= 500
				
			

After executing the above query, you’ll get the result set something like this:

customer_id first_name last_name country score
1 Maria Cramer Germany 350
4 Martin Müller Germany 500

Example != Operator​

We have the following task be to solve using SQL statements

find all non-german customers

The following SQL statement will non-german customers

				
					SELECT *
FROM customers
WHERE country != 'Germany'
				
			
				
					SELECT *
FROM customers
WHERE country <> 'Germany'
				
			

After executing the above query, you’ll get the result set something like this:

customer_id first_name last_name country score
2 John Steel USA 900
3 Georg Pipps UK 750
5 Peter Franken USA NULL
.
Share it !
Exit mobile version