MySQL WHERE Clause

MySQL | WHERE Clause: Learn about the MySQL WHERE Clause, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 05, 2022

WHERE Clause

In MySQL we have some clauses; these clauses help us to filter the data as per our requirements. The clauses play an important part when you write queries. We have many clauses in MySQL.

WHERE Clause is used in the situation when you want some specific record. We can use this clause with INSERT, UPDATE, SELECT, or DELETE any of these statements. We just have to write a few conditions and we'll get the result according to our applied condition.

WHERE Clause Syntax

WHERE (conditions)

Let us understand this by taking an example, Imagine we have a table named "student_details", and inside this database we have the following columns.

WHERE Clause (Example 1)

Now, we'll learn about this clause by taking various cases:

Case 1: If you want to know all the details of ID=1

SELECT * FROM Student_details WHERE ID=1;

This query will give you the following result:

WHERE Clause (Example 2)

Note: Whenever you want all the values of a particular row or column or entire data then in that case we use *

Case 2: Using AND with where clause

If I want to know the name of the student whose department should be "CSE" and gender should be "male" so in this case we can use AND with the WHERE clause. We know that AND gives true value when all the conditions are true.

In this case, "Student_name" is the column name which is my target value, "Student_details" is my table name, WHERE Student_department="CSE" AND Gender="Male" is my condition.

SELECT Student_name FROM Student_details 
WHERE Student_department="CSE" AND Gender="Male";

So we'll get the following result:

WHERE Clause (Example 3)

Only a student named "Shiva" satisfies the condition as he is the only "male" in the "CSE" department.

Case 3: Using OR condition with where clause

We use OR when any one of the value satisfies the condition.

Suppose we want to know all the details of the table "Student_Details" where the department is either "CSE" or "IT".

SELECT * FROM Student_details 
WHERE Student_department="CSE" OR Student_department="IT";
WHERE Clause (Example 4)



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.