0
0
SQLquery~5 mins

WHERE with comparison operators in SQL

Choose your learning style9 modes available
Introduction

The WHERE clause helps you pick only the rows you want from a table by checking conditions.

When you want to find all customers older than 30 years.
When you need to list products cheaper than $20.
When you want to see orders made after a certain date.
When you want to filter employees with a salary greater than or equal to $50,000.
When you want to find students with grades less than 70.
Syntax
SQL
SELECT column1, column2
FROM table_name
WHERE column_name comparison_operator value;

Comparison operators include =, <, >, <=, >=, and <> (not equal).

The WHERE clause filters rows before showing results.

Examples
Selects all employees older than 30 years.
SQL
SELECT * FROM Employees
WHERE Age > 30;
Shows products that cost $20 or less.
SQL
SELECT Name, Price FROM Products
WHERE Price <= 20;
Finds orders made on or after January 1, 2024.
SQL
SELECT * FROM Orders
WHERE OrderDate >= '2024-01-01';
Sample Program

This creates a Students table, adds four students with grades, then selects those with grades less than 70.

SQL
CREATE TABLE Students (
  ID INT,
  Name VARCHAR(50),
  Grade INT
);

INSERT INTO Students (ID, Name, Grade) VALUES
(1, 'Alice', 85),
(2, 'Bob', 65),
(3, 'Charlie', 70),
(4, 'Diana', 90);

SELECT Name, Grade FROM Students
WHERE Grade < 70;
OutputSuccess
Important Notes

Use single quotes for text or date values in the WHERE clause.

Comparison operators work with numbers, text, and dates.

Be careful with <> which means 'not equal'.

Summary

The WHERE clause filters rows based on conditions.

Comparison operators help compare values like numbers or dates.

Use WHERE to get only the data you need from a table.