CodeBlanks Logo Icon CodeBlanks

Comparison operators

Learn =, <>, <, >, <=, >= operators.

Comparison operators

SQL has six basic comparison operators: =, <>, <, >, <=, >=. You compare columns with constants in conditions.

SELECT name FROM products
WHERE stock <= 5;

This returns products with stock of 5 or fewer.

name is selected from products. WHERE keeps only the rows where stock <= 5 holds.

Comparison operators — details

There are two ways to write not-equal: <> and !=. Both are valid ANSI SQL; <> is more common. Text values go in single quotes, numbers without quotes.

For example, WHERE price >= 100 returns products costing 100 or more, and WHERE stock <> 0 returns those whose stock isn't zero.

Practice Comparison operators

Select name and stock from products where stock is less than 5.

Starter code

query.sql
-- pick stock below 5
SELECT name, stock
FROM products
WHERE

Sample solution

query.sql
SELECT name, stock
FROM products
WHERE stock < 5

Expected output

name   stock
Silgi  2
Cetvel 3

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Less than or equal

Question
SELECT name FROM products
WHERE stock ____ 5;
Answer
SELECT name FROM products
WHERE stock <= 5;

Explanation: <= means less than or equal; it returns products with stock of 5 or less.

2 What does <> mean?

  • Equal to
  • Less than
  • Greater than
  • Not equal to Correct answer

Explanation: <> means not equal.

3 Comparison operators: match the terms

Term Meaning
= equal
<> not equal
<= less or equal
>= greater or equal

All lessons in Beginner