SQL WHERE Clause

Tiempo de lectura: 2 minutos

Reading time: 2 minutes

Good morning, let’s continue with SQL

SQL Where Clause

The SQL WHERE clause is used to filter the results of a database query. It allows you to use various operators and functions to specify filtering conditions.

For example, if we want to select all customers from a table who are older than 30 years, we can use the following query:

SELECT * 
FROM customers 
WHERE age > 30;

In this case, we are selecting all columns (*) from the table “customers” and filtering only those records where the “age” column is greater than 30.

It is also possible to use multiple conditions in a single WHERE clause. For example, if we want to select only customers who are older than 30 years and live in California, we can use the following query:

SELECT * 
FROM customers 
WHERE age > 30 AND state = 'California';

In this case, we are using the “AND” operator to combine two conditions: age > 30 and state = ‘California’. Only the records that meet both conditions will be selected.

Another common operator is “OR“, which is used to combine multiple conditions where at least one must be fulfilled. For example, if we want to select customers who are older than 30 years or live in California, we can use the following query:

SELECT * 
FROM customers 
WHERE age > 30 OR state = 'California';

In this case, all records that meet at least one of the two conditions will be selected.

Functions can also be used in the WHERE clause. For example, if we want to select customers whose name starts with the letter ‘S’, we can use the “LIKE” function and the following query:

SELECT * 
FROM customers 
WHERE name LIKE 'S%';

In this case, we are using the “LIKE” function with the pattern ‘S%’, which means that all records where the “name” column starts with the letter ‘S’ will be selected.

To be continued…

Leave a Comment