Comparison Operator in MariaDB
Last Updated :
27 Feb, 2024
In the world of database management, precise comparisons are essential for accurate data retrieval and manipulation. MariaDB, a powerful open-source relational database system, offers a range of comparison operators to help us filter and query our data effectively. In this article, We will learn about what are Comparison operators and How to use them to get efficient output with the help of various examples and so on.
What are Comparison Operators?
Comparison operators in MariaDB are used to compare values in expressions and return a boolean result (TRUE or FALSE) based on the comparison. These operators allow us to perform various types of comparisons, such as equal to, not equal to, greater than, less than, etc.
Comparison operators are fundamental in SQL queries as they enable you to filter data based on specific conditions, making your queries more selective and powerful. Below are the most widely used Comparison operators in MariaDB to perform various operations and queries.
The following are the comparison operators used in MariaDB:
Operators
| Description
|
---|
`=`
| Selects rows where the specified column is equal to a given value.
|
`!=` or `<>`
| Excludes rows where the specified column is equal to a given value.
|
`>`
| Retrieves rows where the specified column is greater than a given value.
|
`<`
| Retrieves rows where the specified column is less than a given value.
|
`>=`
| Retrieves rows where the specified column is greater than or equal to a given value.
|
`<=`
| Retrieves rows where the specified column is less than or equal to a given value.
|
`IS NULL`
| Retrieves rows where the specified column contains NULL values.
|
`IS NOT NULL`
| Retrieves rows where the specified column does not contain NULL values.
|
`LIKE`
| Retrieves rows where the specified column matches a specific pattern.
|
`NOT LIKE`
| Retrieves rows where the specified column does not match a pattern.
|
`IN`
| Retrieves rows where the specified column has values present in a given list.
|
`NOT IN`
| Retrieves rows where the specified column does not have values present in a given list.
|
How to Use Comparison Operator?
We can use Comparison Operator to perform various operations and queries on the tables Let saw some examples for better understanding.
To understand How to use Comparison Operator in MariaDB we need a table on which we will perform various operations and queries. Here we will consider a table called example_table which contains id, first_name and age as Columns.
CREATE TABLE example_table (
id INT PRIMARY KEY,
first_name VARCHAR(50),
age INT
);
INSERT INTO example_table (id, first_name, age) VALUES
(1, 'Aarav', 25),
(2, 'Aisha', 30),
(3, 'Aditya', 22),
(4, 'Esha', 28),
(5, 'Chetan', 35),
(6, 'Lata', 26),
(7, 'Deepak', 32),
(8, 'Sanya', 29),
(9, 'Mayank', 31),
(10, 'Pooja', 24);
Our table example_table looks like:
Showing Contents Of example_table1. Using Equal to Operator
Syntax:
SELECT * FROM table_name WHERE column_name = value;
Here replace table_name, column_name, and value with your actual table name, column name, and desired value for the condition.
Example:
SELECT * FROM example_table WHERE age = 30;
Output:
Output of Equal to operatorExplanation: The above query Retrieve rows where the age is exactly 30.
2. Using Not equal to (!= or <>) Operator
Syntax :
SELECT * FROM table_name WHERE column_name <> value;
Here replace table_name, column_name, and value with your actual table name, column name, and desired value for the condition.
Example :
SELECT * FROM example_table WHERE age <> 28;
Output:
Output of Not equal to operatorExplanation: The above query Exclude rows where the age is 28.
3. Using Greater than (>) Operator
Syntax:
SELECT * FROM table_name WHERE column_name > value;
Here replace table_name, column_name, and value with your actual table name, column name, and desired value for the condition.
Example:
SELECT * FROM example_table WHERE age > 30;
Output:
Output of Greater than operatorExplanation: The above query Retrieve rows where the age is greater than 30.
4. Using Less than (<) Operator
Syntax:
SELECT * FROM table_name WHERE column_name < value;
Here replace table_name, column_name, and value with your actual table name, column name, and desired value for the condition.
Example:
SELECT * FROM example_table WHERE age < 25;
Output:
Output of Less than operatorExplanation : The above query Retrieve rows where the age is less than 25.
5. Using Greater than or equal to (>=) Operator:
Syntax:
SELECT * FROM table_name WHERE column_name >= value;
Here replace table_name, column_name, and value with your actual table name, column name, and desired value for the condition.
Example:
SELECT * FROM example_table WHERE age >= 28;
Output:
Output of Greater than or equal to operatorExplanation: The above query Retrieve rows where the age is 28 or greater.
6. Less than or equal to (<=) :
Syntax:
SELECT * FROM table_name WHERE column_name <= value;
Here replace table_name, column_name, and value with your actual table name, column name, and desired value for the condition.
Example:
SELECT * FROM example_table WHERE age <= 25;
Output:
Output of Less than or equal to operatorExplanation: The above query Retrieve rows where the age is 25 or less.
7. Using LIKE Operator
Syntax:
SELECT * FROM table_name WHERE column_name LIKE 'pattern%';
Here replace table_name, column_name, and pattern with your actual table name, column name, and desired pattern for the condition.
Example:
SELECT * FROM example_table WHERE first_name LIKE 'E%';
Output:
Output of Like operatorExplanation: The above query Retrieve rows where the name starts with 'E'.
8. Using IN Operator
Syntax:
SELECT * FROM table_name WHERE column_name IN (value1, value2, ...);
Here replace table_name, column_name, and values with your actual table name, column name, and desired values for the IN condition.
Example:
SELECT * FROM example_table WHERE age IN (26, 29, 32);
Output:
Output for IN operatorExplanation: The above query Retrieve rows where the age is in the specified list (26, 29, 32).
Conclusion
Overall, Writing accurate and productive SQL queries in MariaDB requires a thorough understanding of and proficiency with comparison operators. These operators enable you to filter and obtain data according to certain criteria. These users include database administrators, developers, and beginner in data administration. Now after reading this article you have much knowledge about Comparison Operator and you can perform as many examples.
Similar Reads
Intersect Operator in MariaDB
MariaDB, a popular open-source relational database management system (RDBMS), offers a plethora of powerful features for data manipulation and querying. Among these features is the Intersect operator, a valuable tool for performing set operations on query results. In this article, We will learn bout
4 min read
MongoDB - Comparison Query Operators
MongoDB provides powerful comparison query operators to filter and retrieve documents based on field values. These operators help developers perform precise queries, enabling efficient data retrieval and manipulation. MongoDB uses various comparison query operators to compare the values of the docum
4 min read
Comparison Operators in Solidity
Comparison Operators are used to compare two values. Solidity has the following types of comparison operators: Greater than: Greater than operator compares two operands. It evaluates to true when the left operand is greater than the right operand otherwise false. It is denoted by >.Lesser than: L
3 min read
SQL Comparison Operators
SQL Comparison Operators are used to compare two values and check if they meet the specific criteria. Some comparison operators are = Equal to, > Greater than , < Less than, etc. Comparison Operators in SQLThe below table shows all comparison operators in SQL : OperatorDescription=The SQL Equa
3 min read
Union Operator in MariaDB
MariaDB is an Open-Source Database system and MariaDB offers similar security features to MySQL, including access control, user authentication, and encryption. UNION operator is a fundamental part of MariaDB, a well-known database systeÂm. The UNION operator mergeÂs results from different SELECT que
5 min read
Declaring Variable in MariaDB
Variables serve as essential components in database management systems like MariaDB facilitating the storage and manipulation of data within the database environment. Variable declaration in MariaDB is a fundamental concept, allowing users to store values temporarily, perform calculations, and strea
4 min read
MongoDB $cmp Operator
The MongoDB $cmp operator is a powerful tool for comparing two values within documents, commonly used in aggregation pipelines for sorting or conditional operations. It plays a crucial role in sorting, conditional operations, and advanced comparisons inside MongoDB queriesIn this article, We will le
4 min read
Unique Constraint in MariaDB
MariaDB uses relational databases similar to SQL. SQL is the precedence of MariaDB similar to the SQL mariaDB also uses some of the querying language to process through the databases. The main advantage of the MariaDB is that it runs on various operating systems and supports many programming languag
7 min read
How to Rename a Column Name in MariaDB?
MariaDB is an open-source relational database management system that can be used to create databases, and tables and to query the tables. Anyone who knows how to write SQL queries can use any of the databases like MariaDB or Mysql for the execution of SQL queries. In this article, we will learn abou
4 min read
MariaDB - Regular Expression
MariaDB is also a relational database language that is similar to SQL. However, the introduction of MariaDB took place as it is an extension to SQL and contains some more advanced operators rather than SQL. MariaDB contains operators similar to SQL like CRUD operations and between operators and othe
8 min read