Search This Blog

Comparison Operators

 

SQL: Comparison Operators

This SQL tutorial explores all of the comparison operators used in SQL to test for equality and inequality, as well as the more advanced operators.

sql comparison operator

 

Description

Comparison operators are used in the WHERE clause to determine which records to select. Here is a list of the comparison operators that you can use in SQL:

Comparison OperatorDescription
=Equal
<>Not Equal
!=Not Equal
>Greater Than
>=Greater Than or Equal
<Less Than
<=Less Than or Equal
IN ( )Matches a value in a list
NOTNegates a condition
BETWEENWithin a range (inclusive)
IS NULLNULL value
IS NOT NULLNon-NULL value
LIKEPattern matching with % and _
EXISTSCondition is met if subquery returns at least one row

DDL/DML for Examples

If you want to follow along with this tutorial, get the DDL to create the tables and the DML to populate the data. Then try the examples in your own database!

Get DDL/DML

Example - Equality Operator

In SQL, you can use the = operator to test for equality in a query.

In this example, we have a table called suppliers with the following data:

supplier_idsupplier_namecitystate
100MicrosoftRedmondWashington
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
400Kimberly-ClarkIrvingTexas
500Tyson FoodsSpringdaleArkansas
600SC JohnsonRacineWisconsin
700Dole Food CompanyWestlake VillageCalifornia
800Flowers FoodsThomasvilleGeorgia
900Electronic ArtsRedwood CityCalifornia

Enter the following SQL statement:


SELECT *
FROM suppliers
WHERE supplier_name = 'Microsoft';

There will be 1 record selected. These are the results that you should see:

supplier_idsupplier_namecitystate
100MicrosoftRedmondWashington

In this example, the SELECT statement above would return all rows from the suppliers table where the supplier_name is equal to Microsoft.

Example - Inequality Operator

In SQL, there are two ways to test for inequality in a query. You can use either the <> or != operator. Both will return the same results.

Let's use the same suppliers table as the previous example.

supplier_idsupplier_namecitystate
100MicrosoftRedmondWashington
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
400Kimberly-ClarkIrvingTexas
500Tyson FoodsSpringdaleArkansas
600SC JohnsonRacineWisconsin
700Dole Food CompanyWestlake VillageCalifornia
800Flowers FoodsThomasvilleGeorgia
900Electronic ArtsRedwood CityCalifornia

Enter the following SQL statement to test for inequality using the <> operator:


SELECT *
FROM suppliers
WHERE supplier_name <> 'Microsoft';

OR enter this next SQL statement to use the != operator:


SELECT *
FROM suppliers
WHERE supplier_name != 'Microsoft';

There will be 8 records selected. These are the results you should see with either one of the SQL statements:

supplier_idsupplier_namecitystate
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
400Kimberly-ClarkIrvingTexas
500Tyson FoodsSpringdaleArkansas
600SC JohnsonRacineWisconsin
700Dole Food CompanyWestlake VillageCalifornia
800Flowers FoodsThomasvilleGeorgia
900Electronic ArtsRedwood CityCalifornia

In the example, both SELECT statements would return all rows from the suppliers table where the supplier_name is not equal to Microsoft.

Example - Greater Than Operator

You can use the > operator in SQL to test for an expression greater than.

In this example, we have a table called customers with the following data:

customer_idlast_namefirst_namefavorite_website
4000JacksonJoetechonthenet.com
5000SmithJanedigminecraft.com
6000FergusonSamanthabigactivities.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigeNULL
9000JohnsonDerektechonthenet.com

Enter the following SQL statement:


SELECT *
FROM customers
WHERE customer_id > 6000;

There will be 3 records selected. These are the results that you should see:

customer_idlast_namefirst_namefavorite_website
7000ReynoldsAllengoogle.com
8000AndersonPaigeNULL
9000JohnsonDerekoracle.com

In this example, the SELECT statement would return all rows from the customers table where the customer_id is greater than 6000. A customer_id equal to 6000 would not be included in the result set.

Example - Greater Than or Equal Operator

In SQL, you can use the >= operator to test for an expression greater than or equal to.

Let's use the same customers table as the previous example.

customer_idlast_namefirst_namefavorite_website
4000JacksonJoetechonthenet.com
5000SmithJanedigminecraft.com
6000FergusonSamanthabigactivities.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigeNULL
9000JohnsonDerektechonthenet.com

Enter the following SQL statement:


SELECT *
FROM customers
WHERE customer_id >= 6000;

There will be 4 records selected. These are the results that you should see:

customer_idlast_namefirst_namefavorite_website
6000FergusonSamanthabigactivities.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigeNULL
9000JohnsonDerektechonthenet.com

In this example, the SELECT statement would return all rows from the customers table where the customer_id is greater than or equal to 6000. In this case, the supplier_id equal to 6000 would be included in the result set.

Example - Less Than Operator

You can use the < operator in SQL to test for an expression less than.

In this example, we have a table called products with the following data:

product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL

Enter the following SQL statement:


SELECT *
FROM products
WHERE product_id < 5;

There will be 4 records selected. These are the results that you should see:

product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50

In this example, the SELECT statement would return all rows from the products table where the product_id is less than 5. A product_id equal to 5 would not be included in the result set.

Example - Less Than or Equal Operator

In SQL, you can use the <= operator to test for an expression less than or equal to.

Let's use the same products table as the previous example.

product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL

Enter the following SQL statement:


SELECT *
FROM products
WHERE product_id <= 5;

There will be 5 records selected. These are the results that you should see:

product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75

In this example, the SELECT statement would return all rows from the products table where the product_id is less than or equal to 5. In this case, the product_id equal to 5 would be included in the result set.

Example - Advanced Operators

To learn more about the advanced comparison operators, we've written tutorials to discuss each one individually. These topics will be covered later, or you can jump to one of these tutorials now.

No comments:

Post a Comment

Using SQL*Plus to Unlock Accounts and Reset Passwords

  Using SQL*Plus to Unlock Accounts and Reset Passwords Use this SQL*Plus procedure to unlock and reset user account passwords. Log in as th...