The following text has been converted to Markdown format. This format has been optimized using headings, bullet points, bold text, code blocks, and tables to be neater and easier to read.
In managing and manipulating databases using MySQL, operators play an important role in performing various calculations, comparisons, and condition evaluations. Below are the types of operators in MySQL syntax along with their explanations:
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical calculations on values (operands).
- Addition (
+): Operator used to add values of both operands. For example,30 + 20yields50. - Subtraction (
-): Operator used to subtract the right operand from the left operand. For example,30 - 10yields20. - Multiplication (
*): Operator used to multiply both operands. For example,3 * 3yields9. - Division (
/): Operator used to divide the left operand by the right operand. For example,20 / 2yields10. - Modulus (
%): Operator used to find the remainder of division of the left operand by the right operand. For example,10 % 2yields0.
2. Comparison Operators
Comparison operators are used to compare two values. The result of this operation is a boolean value (true or false).
| Operator | Description | Example |
|---|---|---|
= |
Compares whether both operands are equal. If equal, it returns true. | 10 = 10 (True) |
!= |
Compares whether operands are not equal. If not equal, it returns true. | 5 != 10 (True) |
<> |
Functions same as !=, comparing whether operands are not equal. |
5 <> 10 (True) |
> |
Compares whether the left operand is greater than the right operand. | 10 > 5 (True) |
< |
Compares whether the left operand is less than the right operand. | 5 < 10 (True) |
>= |
Compares whether the left operand is greater than or equal to the right operand. | 10 >= 10 (True) |
<= |
Compares whether the left operand is less than or equal to the right operand. | 5 <= 10 (True) |
!< |
Compares whether the left operand is not less than the right operand. | 10 !< 5 (True) |
!> |
Compares whether the left operand is not greater than the right operand. | 9 !> 10 (True) |
3. Logical Operators
Logical operators are used to combine multiple conditions or perform logical evaluations on certain clauses.
- ALL: Compares a value to all values in another set of values.
- AND: Combines two or more boolean expressions. Returns
trueif all conditions are true. - ANY: Compares a value to any applicable value in a list according to the condition.
- BETWEEN: Searches for values within a specified minimum and maximum range.
- EXISTS: Checks for the existence of certain rows in a table that meet specific criteria.
- IN: Compares a value to a predefined list of literal values.
- LIKE: Compares a value to a similar value using wildcard operators.
- NOT: Reverses the meaning of a logical operator being used (negation).
- OR: Adds a condition to a clause and returns
trueif either or both conditions are true. - IS NULL: Compares a value to check if it is empty (NULL).
- UNIQUE: Checks for the uniqueness of each row in a table that has no duplicates.
4. Using the BETWEEN Operator
The BETWEEN operator is used to search for values within a specified range. The range consists of a minimum and maximum value to be displayed. The BETWEEN operator can also be combined with other comparison operators.
Basic Syntax:
SELECT * FROM table_name WHERE column_name BETWEEN min_value AND max_value;
Example Usage:
SELECT * FROM phone_list WHERE price BETWEEN 1000000 AND 5000000;
- Meaning: This command displays data from the
phone_listtable where thepricecolumn has values between1,000,000and5,000,000.
5. Using the LIKE Clause
The LIKE clause is an operator used to search by comparing a value to a search pattern. This clause can use wildcard operators as placeholders, such as:
- (
_): A placeholder that matches exactly one arbitrary character. - (
%): A placeholder that matches an arbitrary character string of length 0 to unlimited.
Basic Syntax:
SELECT display_columns FROM table_name WHERE search_column LIKE 'search_keyword';
Example Usage Without Wildcard:
SELECT * FROM students WHERE student_name LIKE 'Wilantara';
- Meaning: Displays data from the
studentstable where thestudent_namecolumn value is exactly "Wilantara".
Example Usage With Wildcard:
SELECT * FROM students WHERE student_name LIKE 'W%';
- Meaning: Displays data from the
studentstable where thestudent_namecolumn value starts with the letter "W" followed by any characters.
Written by
Wilan
A regular contributor to Bali Island Tekno who actively shares knowledge about technology, programming, and the world of software engineering.