MySQL Operators Basic Introduction with Example

MySQL Operators Basic Introduction with Example

PROGRAMMING

4/4/20242 min read

MacBook Pro with images of computer language codes
MacBook Pro with images of computer language codes

MySQL Operator Basic Overview:

One of the key features of MySQL is its operators, which help us to perform various operations on the database. In this post we will get a basic overview about some of the most commonly used MySQL operators, along with its usage and examples.

Arithmetic Operators

It is used to perform mathematical calculations on numeric data in MySQL.

Arithmetic operators supported by MySQL:

  • Addition ( +): Adds two values together

  • Subtraction (-): Subtracts one value from another

  • Multiplication (*): Multiplies two values

  • Division (/): Divides one value by another

  • Modulus (%): Returns the remainder of a division operation

Example:

SELECT 10 + 5;

This query will result: 15, i.e. the sum of 10 and 5.

Comparison Operators

It is used to compare two values in MySQL.

Comparison operators supported by MySQL:

  • Equal to (=): Checks if two values are equal

  • Not equal to (!= or <>): Checks if two values are not equal

  • Greater than (>): Checks if one value is greater than another

  • Less than (<): Checks if one value is less than another

  • Greater than or equal to (>=): Checks if one value is greater than or equal to another

  • Less than or equal to (<=): Checks if one value is less than or equal to another

Example:

SELECT * FROM customers WHERE age > 18;

Result: All customers whose age is greater than 18.

Logical Operators

It is used to combine multiple conditions in MySQL.

Logical operators supported by MySQL:

  • AND: Returns true if both conditions are true

  • OR: Returns true if either condition is true

  • NOT: Returns true if the condition is false

Example:

SELECT * FROM products WHERE price > 1000 AND category = 'Motor';

Result : All products with a price greater than 1000 and belonging to the 'Motor' category.

Real-Life Scenario

One real-life scenario example for the topic discussed above(MySQL operators):

Let's take the example of an e-commerce website where we have 1 table with name "products" which stores details about the product. Now here using MySQL operators discussed we can perform many operations here to get the data based on requirements:

  • Getting all data with any specific requirement for example filtering all data where price is more than 1000.

  • Finding all products or multiple products using AND/OR or related operators based on requirements.

  • Calculating of total price for specific products based on requirement.

And many more such operation we can perform.

MySQL operators are important for performing various operations on MySQL database tables. By using these operators effectively, we can efficiently utilize the power of MySQL to manipulate and retrieve data according to our specific requirements.