MariaDB WHERE
To filter the results, the MariaDB WHERE clause is used with SELECT, INSERT, UPDATE and DELETE statements.
Syntax:
WHERE conditions; |
Parameters:
conditions: It is used to specify the conditions to be strictly followed for selection.
Example: Selecting specific fields from a table.
Students table:
ID NAME AGE 1 Joy 5 2 Smiley 13 3 Happy 11 4 Tom 15 5 Jerry 10 6 Bruno 6 7 David 8 8 Kim 9 9 Crystal 6 10 Don 4
Query:
SELECT name, age FROM students WHERE age > 10 ORDER BY name; |
Output:
ID NAME AGE 3 Happy 11 2 Smiley 13 4 Tom 15
Example: Using WHERE clause with AND condition.
Students table:
ID NAME AGE 1 Joy 5 2 Smiley 13 3 Happy 11 4 Tom 15 5 Jerry 10 6 Bruno 6 7 David 8 8 Kim 9 9 Crystal 6 10 Don 4
Query:
SELECT name, age FROM students WHERE age > 10 AND age < 15 ORDER BY name; |
Output:
ID NAME AGE 3 Happy 11 2 Smiley 13
Example: Using WHERE clause with OR condition.
Students table:
ID NAME AGE 1 Joy 5 2 Smiley 13 3 Happy 11 4 Tom 15 5 Jerry 10 6 Bruno 6 7 David 8 8 Kim 9 9 Crystal 6 10 Don 4
Query:
SELECT name, age FROM students WHERE age = 10 OR age = 15 ORDER BY name; |
Output:
ID NAME AGE 5 Jerry 10 4 Tom 15
Example: Using WHERE clause with both AND and OR condition.
Students table:
ID NAME AGE 1 Joy 5 2 Smiley 13 3 Happy 11 4 Tom 15 5 Jerry 10 6 Bruno 6 7 David 8 8 Kim 9 9 Crystal 6 10 Don 4
Query:
SELECT name, age FROM students WHERE ( age > 10 AND id = 2 ) OR age = 15 ORDER BY name; |
Output:
ID NAME AGE 2 Smiley 13 4 Tom 15