MySQL Conditions
MySQL AND Condition
The MySQL AND condition is used with SELECT, INSERT, UPDATE or DELETE statements to test two or more conditions in an individual query.
Syntax:
- WHERE condition1
- AND condition2
- …
- AND condition_n;
Parameter explanation:
condition1, condition2, … condition_n: Specifies all conditions that must be fulfilled for the records to be selected.
MySQL AND Example
The following example specifies how to use the AND condition in MySQL with SELECT statement.
Consider a table “cus_tbl”, having the following data:
Execute the following query:
MySQL OR Condition
The MySQL OR condition specifies that if you take two or more conditions then one of the conditions must be fulfilled to get the records as result.
Syntax:
- WHERE condition1
- OR condition2
- …
- OR condition_n;
Parameter explanation
condition1, condition2, … condition_n: Specifies all conditions that must be fulfilled for the records to be selected.
MySQL OR Example
The following example specifies how to use the OR condition in MySQL with SELECT statement.
Consider a table “cus_tbl”, having the following data:
Execute the following query:
- SELECT *
- FROM cus_tbl
- WHERE cus_firstname = ‘Ajeet’
- OR cus_id > 100;
Output:
MySQL AND & OR condition
In MySQL, you can use AND & OR condition both together with the SELECT, INSERT, UPDATE and DELETE statement. While combine these conditions, you must be aware where to use round brackets so that the database know the order to evaluate each condition.
Syntax:
- WHERE condition1
- AND condition2
- …
- OR condition_n;
Parameter
condition1, condition2, … condition_n: It specifies the conditions that are evaluated to determine if the records will be selected.
MySQL AND OR Example
Consider a table “students”, having the following data.
Execute the following query:
- SELECT *
- FROM students
- WHERE (course_name = ‘Java’ AND student_name = ‘Aryan’)
- OR (student_id < 2);
Output:
MySQL LIKE condition
In MySQL, LIKE condition is used to perform pattern matching to find the correct result. It is used in SELECT, INSERT, UPDATE and DELETE statement with the combination of WHERE clause.
Syntax:
- expression LIKE pattern [ ESCAPE ‘escape_character’ ]
Parameters
expression: It specifies a column or field.
pattern: It is a character expression that contains pattern matching.
escape_character: It is optional. It allows you to test for literal instances of a wildcard character such as % or _. If you do not provide the escape_character, MySQL assumes that “\” is the escape_character.
MySQL LIKE Examples
1) Using % (percent) Wildcard:
Consider a table “officers” having the following data.
Execute the following query:
- SELECT officer_name
- FROM officers
- WHERE address LIKE ‘Luck%’;
Output:
2) Using _ (Underscore) Wildcard:
We are using the same table “officers” in this example too.
Execute the following query:
- SELECT officer_name
- FROM officers
- WHERE address LIKE ‘Luc_now’;
Output:
3) Using NOT Operator:
You can also use NOT operator with MySQL LIKE condition. This example shows the use of % wildcard with the NOT Operator.
Consider a table “officers” having the following data.
Execute the following query:
- SELECT officer_name
- FROM officers
- WHERE address NOT LIKE ‘Luck%’;
Output:
MySQL IN Condition
The MySQL IN condition is used to reduce the use of multiple OR conditions in a SELECT, INSERT, UPDATE and DELETE statement.
Syntax:
- expression IN (value1, value2, …. value_n);
Parameters
expression: It specifies a value to test.
value1, value2, … or value_n: These are the values to test against expression. If any of these values matches expression, then the IN condition will evaluate to true. This is a quick method to test if any one of the values matches expression.
MySQL IN Example
Consider a table “officers”, having the following data.
Execute the following query:
- SELECT *
- FROM officers
- WHERE officer_name IN (‘Ajeet’, ‘Vimal’, ‘Deepika’);
Output:
Let’s see why it is preferred over OR condition:
Execute the following query:
- SELECT *
- FROM officers
- WHERE officer_name = ‘Ajeet’
- OR officer_name = ‘Vimal’
- OR officer_name = ‘Deepika’;
Output:
MySQL NOT Condition
The MySQL NOT condition is opposite of MySQL IN condition. It is used to negate a condition in a SELECT, INSERT, UPDATE or DELETE statement.
Syntax:
- NOT condition
Parameter
condition: It specifies the conditions that you want to negate.
MySQL NOT Operator with IN condition
Consider a table “officers”, having the following data.
Execute the following query:
- SELECT *
- FROM officers
- WHERE officer_name NOT IN (‘Ajeet’,‘Vimal’,‘Deepika’);
Output:
MySQL NOT Operator with IS NULL condition:
Execute the following query:
- SELECT *
- FROM officers
- WHERE officer_name IS NOT NULL;
Output:
MySQL NOT Operator with LIKE condition:
We are taking the same table “officer” for this operation also:
Execute the following query:
- SELECT *
- FROM officers
- WHERE officer_name NOT LIKE ‘A%’;
Output:
MySQL NOT Operator with BETWEEN condition:
We are taking the same table “officer” for this operation also:
Execute the following query:
- SELECT *
- FROM officers
- WHERE officer_id NOT BETWEEN 3 AND 5;
Output:
MySQL IS NULL Condition
MySQL IS NULL condition is used to check if there is a NULL value in the expression. It is used with SELECT, INSERT, UPDATE and DELETE statement.
Syntax:
- expression IS NULL
Parameter
expression: It specifies a value to test if it is NULL value.
Consider a table “officers” having the following data.
Execute the following query:
- SELECT *
- FROM officers
- WHERE officer_name IS NULL;
Output:
MySQL IS NOT NULL Condition
MySQL IS NOT NULL condition is used to check the NOT NULL value in the expression. It is used with SELECT, INSERT, UPDATE and DELETE statements.
Syntax:
- expression IS NOT NULL
Parameter
expression: It specifies a value to test if it is not NULL value.
MySQL IS NOT NULL Example
Consider a table “officers” having the following data.
Execute the following query:
- SELECT *
- FROM officers
- WHERE officer_name IS NOT NULL;
Output:
MySQL BETWEEN Condition
The MYSQL BETWEEN condition specifies how to retrieve values from an expression within a specific range. It is used with SELECT, INSERT, UPDATE and DELETE statement.
Syntax:
- expression BETWEEN value1 AND value2;
Parameters
expression: It specifies a column.
value1 and value2: These values define an inclusive range that expression is compared to.
Let’s take some examples:
(i) MySQL BETWEEN condition with numeric value:
Consider a table “officers” having the following data.
Execute the following query:
- SELECT *
- FROM officers
- WHERE officer_id BETWEEN 1 AND 3;
Output:
Note: In the above example, you can see that only three rows are returned between 1 and 3.
(ii) MySQL BETWEEN condition with date:
MySQL BETWEEN condition also facilitates you to retrieve records according to date.
See this example:
Consider a table “employees”, having the following data.
Execute the following query:
- SELECT *
- FROM employees
- WHERE working_date BETWEEN CAST (‘2015-01-24’ AS DATE) AND CAST (‘2015-01-25’ AS DATE);
Output: