SQL Less Than Operator
The SQL < (Less Than) operator is used to compare values in a query. It returns true if the left operand is less than the right operand. This operator is commonly used in the WHERE clause to filter records based on numerical or date values.
In this tutorial, we will explore the SQL < operator, its syntax, and how to use it with examples.
Syntax of SQL Less Than Operator
The basic syntax of the SQL < operator is:
</>
Copy
SELECT column1, column2, ...
FROM table_name
WHERE column_name < value;
Explanation:
- SELECT: Specifies the columns to retrieve from the table.
- FROM: Defines the table from which to retrieve data.
- WHERE: Filters the records based on the condition.
- <: Ensures only records where
column_nameis less than the specifiedvalueare included.
Step-by-Step Examples with MySQL
Let’s create a sample employees table to demonstrate the usage of the < operator.
1 Creating the Table and Inserting Data
We will create an employees table with columns id, name, age, and salary.
</>
Copy
CREATE TABLE employees (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
age INT,
salary DECIMAL(10,2)
);
Inserting sample data into the table:
</>
Copy
INSERT INTO employees (name, age, salary)
VALUES
('Arjun', 30, 50000.00),
('Ram', 25, 45000.00),
('John', 28, 48000.00),
('Priya', 23, 40000.00);
Examples: Using the Less Than Operator in Queries
Now, let’s explore different scenarios using the < operator in SQL queries.
Example 1: Selecting Employees Younger Than 28
To retrieve employees who are younger than 28 years:
</>
Copy
SELECT * FROM employees
WHERE age < 28;
