JIYIK CN >

Current Location:Home > Learning > DATABASE > MySQL >

Selecting multiple values using WHERE in MySQL

Author:JIYIK Last Updated:2025/04/22 Views:

This article is about using MySQL query to get data from a specific table or relation that satisfies a specific condition. To do this, the WHERE clause is used in the SQL query.


WHERE clause in SQL query

WHEREThe clause specifies the conditions for retrieving data from a single table or joining multiple tables. If the provided conditions are met, the query returns the corresponding values ​​from the table.

You can use WHEREthe clause to restrict the records and get only the records you need.

WHEREThe clause is used not only in SELECT statements, but also in UPDATE , DELETE , and other statements.

Syntax of the WHERE clause

In SQL, the following syntax is used in the WHERE clause to filter records based on a specific condition.

SELECT column1, column2, ...
FROM table_name
WHERE [condition];

You can use different relational (<, >, <=, >=, ==, !=) or logical (AND, OR, NOT) operators to create conditions.

Assume we have an Employees table which stores the employee data of an organization. The table data is as follows:

Employees Table

The above table shows the data of 6 employees from the Employees table. Suppose we need to select employees whose age is greater than 40, then we will use the following query:

SELECT * from Employees
WHERE Emp_Age > 40

Output:

Employees aged 40 and above

请注意, we have used relational operators in the WHERE clause. Similarly, we can use logical and relational operators in the WHERE clause.


Using WHERE clause in MySQL to select records based on multiple conditions

We can also filter the records from the table based on multiple conditions. For this, we can use logical operators like AND and OR based on the conditions.

For example, we need to get the name and salary of employees who are under 40 years old and have a salary greater than $3000. The query for this condition would be:

SELECT Emp_Name, Emp_Salary FROM `Employees`
WHERE Emp_Age < 40 AND Emp_Salary > 3000

The result of this query will be:

Employees under 40 years old with salary over 3,000

As can be seen from the result, only the employees who meet both the conditions specified in the query will be selected. If we need to meet any one of the conditions, then we can use the OR operator instead of the AND operator as shown below:

SELECT Emp_Name, Emp_Salary FROM `Employees`
WHERE Emp_Age < 40 OR Emp_Salary > 3000

Now the result set will be like this:

Employees under 40 years old or with salary over 3000

You can see that the results contain more rows than the previous results. This is because all employees are either younger than 40 or have a salary greater than $3000.

You can achieve the same result by using the IN operator. The IN operator works the same as the OR operator, but the query is better structured.

SELECT Emp_ID,Emp_Name, Emp_Age FROM `Employees`
WHERE Emp_Name IN ("John","David")

The result set will be something like this:

Selecting multiple values ​​using the IN operator - Output

In SQL query, you can see multiple ways to select multiple values ​​from a table. You can choose any one of them based on your need and desired data output.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Changing max_allowed_packet Size in MySQL Server

Publish Date:2025/04/22 Views:192 Category:MySQL

This article explains how to change the max_allowed_packet size in MySQL server. To understand this, we will use two operating systems, Windows 10 and Linux (Ubuntu). Changing max_allowed_packet Size in MySQL Server If we try to upload a fi

Zerofill usage, advantages and alternatives in MySQL

Publish Date:2025/04/22 Views:195 Category:MySQL

In this article we will understand the uses, advantages and alternatives of ZEROFILL attribute in MySQL. Use and benefits of the ZEROFILL attribute in MySQL The benefit of using the ZEROFILL attribute is that it has nothing to do with input

Compare only MySQL timestamp dates to date parameters

Publish Date:2025/04/22 Views:64 Category:MySQL

In this article we will use the DATE() , CAST() , and CONVERT() functions to compare MySQL timestamp dates with only the date parameter. DATE() vs. CAST() vs. CONVERT() in MySQL Below is a brief description of each function. You can also fi

Calculating Percentages in MySQL

Publish Date:2025/04/22 Views:66 Category:MySQL

We will use one or more columns to calculate percentages in MySQL. There are different ways to do this, and for each method we will use an example table. Calculate percentage using a column in MySQL We have a table called sales where ID, Re

Installing MySQL Client in Linux

Publish Date:2025/04/22 Views:186 Category:MySQL

MySQL is an abbreviation for Structured Query Language which is a relational database management system. It is an open source database which is freely available and used for both large and small applications. The use cases are in school app

Subtraction in MySQL

Publish Date:2025/04/21 Views:140 Category:MySQL

MINUS Operator is used in SQL to find unique elements in table A that are not present in table B. Through this tutorial, we will see how to emulate the operator in MySQL MINUS to get the desired result. We will understand it by using NOT IN

INTERSECT Operator in MySQL

Publish Date:2025/04/21 Views:99 Category:MySQL

This article will help you understand INTERSECT the operator. Although MySQL does not support INTERSECT and MINUS / EXCEPT , there are other ways to simulate this functionality. We will see INTERSECT what is , its benefits, and learn variou

Deleting using Join in MySQL

Publish Date:2025/04/21 Views:158 Category:MySQL

This tutorial article will show you how to use MySQL JOIN methods to delete data from multiple tables. This is useful when you are simultaneously deleting records from one table that are related to a specific record in another table. DELETE

MySQL using LIKE in case insensitive search

Publish Date:2025/04/21 Views:131 Category:MySQL

This tutorial article will show you how to use LIKE the operator to search a column. We will show you how to use it correctly to include all results that meet certain criteria, regardless of whether the value is uppercase or lowercase. LIKE

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial