WHERE clause in MySQL

Página 13

The WHERE clause in MySQL is a powerful and essential tool for anyone working with databases. It is through it that we can filter the data that we want to manipulate, whether for consultation, alteration or deletion. The WHERE clause is used in conjunction with SQL statements such as SELECT, UPDATE, DELETE, and others to specify the condition that must be met for the action to be performed.

To understand better, let's consider an example table called "Employees", which contains information about the employees of a company, such as ID, name, position, salary, department, among others. If we want to fetch all employees working in the sales department, we could use the WHERE clause like this:

SELECT * FROM Employees WHERE Department = 'Sales';

In this case, the WHERE clause is being used to filter only the rows where the value in the column "Department" equals 'Sales'. The '=' operator is one of many comparison operators that can be used in a WHERE clause. Other operators include '>', '<', '>=', '<=', '<>', '!=', 'LIKE', 'IN', and more.

In addition, the WHERE clause can be used with logical operators like 'AND', 'OR' and 'NOT' to combine or negate conditions. For example, if we want to find all employees in the sales department who earn more than 5000, we could write:

SELECT * FROM Employees WHERE Department = 'Sales' AND Salary > 5000;

Here, the WHERE clause is being used to filter only those rows that meet both conditions: the column "Department" must equal 'Sales' and the column "Salary" must be greater than 5000.

>

Similarly, we could use the OR operator to find all employees who work in the sales department or earn more than 5000, regardless of which department they work in:

SELECT * FROM Employees WHERE Department = 'Sales' OR Salary > 5000;

The WHERE clause can also be used with the NOT operator to negate a condition. For example, to find all employees who do not work in the sales department, we could write:

SELECT * FROM Employees WHERE NOT Department = 'Sales';

In addition, the WHERE clause can be used with SQL functions to perform complex operations. For example, if we wanted to find all employees who were hired last year, we could use the YEAR() function like this:

SELECT * FROM Employees WHERE YEAR(DataContratacao) = YEAR(CURDATE()) - 1;

Here, the YEAR() function is being used to extract the year from the "DataContratacao" column and the CURDATE() function is being used to get the current date. The WHERE clause then compares the hire year to the current year minus one.

In summary, the WHERE clause in MySQL is a powerful tool that allows you to filter data based on specific conditions. It can be used with a variety of logical and comparison operators, and can be combined with SQL functions to perform complex operations. Mastering the WHERE clause is essential for anyone working with MySQL databases.

Now answer the exercise about the content:

What is the main function of the WHERE clause in MySQL and how can it be used?

You are right! Congratulations, now go to the next page

You missed! Try again.

Next page of the Free Ebook:

14Sorting results with ORDER BY

Earn your Certificate for this Course for Free! by downloading the Cursa app and reading the ebook there. Available on Google Play or App Store!

Get it on Google Play Get it on App Store

+ 6.5 million
students

Free and Valid
Certificate with QR Code

48 thousand free
exercises

4.8/5 rating in
app stores

Free courses in
video, audio and text