Mariadb Where Clause

MariaDB: WHERE Clause

This MariaDB tutorial explains how to use the MariaDB WHERE clause with syntax and examples.

Description

The MariaDB WHERE clause is used to filter the results from a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax

The syntax for the WHERE clause in MariaDB is:

WHERE conditions;

Parameters or Arguments

conditions

The conditions that must be met for records to be selected.

Example - With Single condition

Let's look at how to use the WHERE clause to filter results based on one condition in MariaDB.

For example:

SELECT *
FROM sites
WHERE site_name = 'AODBA.com';

In this WHERE clause example, we've used the WHERE clause to filter our results from the sites table. The SELECT statement above would return all rows from the sites table where the site_name is 'AODBA.com'. Because the * is used in the SELECT, all columns from the sites table would appear in the result set.

Example - Using AND condition

Let's look at how to use the AND condition within the WHERE clause in MariaDB.

For example:

SELECT *
FROM sites
WHERE site_name = 'AODBA.com';
AND site_id = 500;

This MariaDB WHERE example uses the WHERE clause to define multiple conditions. In this example, the SELECT statement uses the AND condition to return all sites that where the site_name is 'AODBA.com' and the site_id is less than or equal to 500.

Example - Using OR condition

Let's look at how to use the OR condition within the WHERE clause in MariaDB.

For example:

SELECT site_id, site_name
FROM sites
WHERE site_name = 'mySite.com'
OR site_name = 'AODBA.com';

This WHERE clause example uses the WHERE clause to define multiple conditions, but instead of using the AND condition, it uses the OR condition. In this case, this SELECT statement would return all site_id and site_name values where the site_name is 'mySite.com' or 'AODBA.com'.

Example - Combining AND & OR conditions

Let's look at how to combine the AND condition and the OR condition within the WHERE clause in MariaDB.

For example:

SELECT *
FROM sites
WHERE (site_name = 'mySite.com' AND site_id  100)
OR (site_name = 'AODBA.com');

This WHERE clause example uses the WHERE clause to define multiple conditions, but it combines the AND condition and the OR condition. This example would return all sites that have a site_name of 'mySite.com' and the site_id is less than 100 as well as all sites that have a site_name of 'AODBA.com'.

The parentheses determine the order that the AND and OR conditions are evaluated. Just like you learned in the order of operations in Math class!