Multiple WHERE Clause Conditionals Using the MySQL AND Logical Operator - Tuto Startup
info@tutostartup.com | tutostartup.com is for sale!

Multiple WHERE Clause Conditionals Using the MySQL AND Logical Operator

ZERO LIMITS

Multiple WHERE Clause Conditionals Using the MySQL AND Logical Operator

TutoSartup excerpt from this article:

Practice Data

For the practice queries in this post, I’m using the ‘category’ table from the well-known Sakila database:

SELECT *FROM category;

MySQL AND Logical Operator

How you use the AND logical operator in the WHERE clause is quite easy…

See this example query:

SELECT *FROM categoryWHERE category_id = 2ANDname = ‘Comedy’;

Knowing what data is present in the ‘category’ table, how many rows do you think this query will return?

Let’s see…

No rows are returned… Zero…

Here is why…

There are 2 conditionals separated by the AND keyword in this WHERE clause:

category_id = 2name = ‘Comedy’In order for any row to be returned in this SELECT query, the ‘category_id’ column value must equal 2, and the ‘name’ column value must equal ‘Comedy’… Yes, there is a row with a ‘category_id’ column value of 2…

Let’s see what that looks like with this example SELECT query:

SELECT *FROM categoryWHERE category_id = 2ANDname = ‘Animation’;

This is the only row in the ‘category’ table in which the ‘category_id’ column value is 2 (evaluates to TRUE) and the ‘name’ column value is ‘Animation’ (also evaluates to TRUE)… Shown in this next query example, I now have 3 conditionals being tested:

SELECT *FROM categoryWHERE category_id = 2ANDname = ‘Animation’ANDDATE(last_update) = ‘2006-02-15’;

Again, each conditional test must evaluate to TRUE for a row to be included in the returned query results:

Should any one of the conditional tests evaluate as FALSE, the row will not be included:

SELECT *FROM categoryWHERE category_id = 2ANDname = ‘Animation’ANDDATE(last_update) = ‘2006-02-14’; — oops, different day

In this particular query, the conditionals category_id = 2 and name = ‘Animation’ evaluate to TRUE… You have access to exclusive content unpublished anywhere else with this membership… With new content being added regularly, continue learning MySQL at any level…I appreciate you reading my content…Oftentimes, you need multiple filter conditionals in a WHERE clause in order to target specific rows of data… Continue reading this blog post and learn how to use the AND logical operator in WHERE clause queries…

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter…

Multiple AND logical operators in the WHERE clause

You can absolutely include multiple AND logical operators in the WHERE clause…

Learn MySQL!

Using the AND logical operator in the WHERE clause enables you to place super-specific constraints on which row(s) are returned or targeted in a query…

Recommended Reading

Visit any of these blog posts to learn more about how to use MySQL:

Limit Rows with the WHERE clause – MySQL Beginner SeriesSELECT clause queries – MySQL Beginner Basics Series…

Oftentimes, you need multiple filter conditionals in a WHERE clause in order to target specific rows of data. Continue reading this blog post and learn how to use the AND logical operator in WHERE clause queries…

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

Practice Data

For the practice queries in this post, I’m using the ‘category’ table from the well-known Sakila database:

SELECT *
FROM category;

MySQL AND Logical Operator

How you use the AND logical operator in the WHERE clause is quite easy. Simply separate each individual conditional you want to test with the AND keyword.

See this example query:

SELECT *
FROM category
WHERE category_id = 2
AND
name = 'Comedy';

Knowing what data is present in the ‘category’ table, how many rows do you think this query will return?

Let’s see…

No rows are returned. Zero…

Here is why…

There are 2 conditionals separated by the AND keyword in this WHERE clause:

  • category_id = 2
  • name = ‘Comedy’

In order for any row to be returned in this SELECT query, the ‘category_id’ column value must equal 2, and the ‘name’ column value must equal ‘Comedy’. Yes, there is a row with a ‘category_id’ column value of 2. There is also a row with a ‘name’ column value of ‘Comedy’.

However, they are not one in the same (or same row).

Therefore, you should understand when using the AND logical operator, each conditional separated by AND must evaluate to TRUE in order for any row(s) to be included in the returned result set.

Let’s see what that looks like with this example SELECT query:

SELECT *
FROM category
WHERE category_id = 2
AND
name = 'Animation';

This is the only row in the ‘category’ table in which the ‘category_id’ column value is 2 (evaluates to TRUEand the ‘name’ column value is ‘Animation’ (also evaluates to TRUE).

Multiple AND logical operators in the WHERE clause

You can absolutely include multiple AND logical operators in the WHERE clause. Shown in this next query example, I now have 3 conditionals being tested:

SELECT *
FROM category
WHERE category_id = 2
AND
name = 'Animation'
AND
DATE(last_update) = '2006-02-15';

Again, each conditional test must evaluate to TRUE for a row to be included in the returned query results:

Should any one of the conditional tests evaluate as FALSE, the row will not be included:

SELECT *
FROM category
WHERE category_id = 2
AND
name = 'Animation'
AND
DATE(last_update) = '2006-02-14'; -- oops, different day

In this particular query, the conditionals category_id = 2 and name = ‘Animation’ evaluate to TRUE.

But, the conditional, DATE(last_update) = ‘2006-02-14’ evaluates to FALSE. Therefore, no rows are returned from this query.


Discover premium articles, in-depth guides, instructional videos, and much more by joining the “MySQL Learning Tier” membership. You have access to exclusive content unpublished anywhere else with this membership. With new content being added regularly, continue learning MySQL at any level.


Using the AND logical operator in the WHERE clause enables you to place super-specific constraints on which row(s) are returned or targeted in a query.

Recommended Reading

Visit any of these blog posts to learn more about how to use MySQL:


I appreciate you reading my content. Support me today with a Tip if you would like. Thank you! 🙏🏻


Like what you have read? See anything incorrect? Please comment below and thank you for reading!!!

A Call To Action!

Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.

Visit the Portfolio-Projects page to see blog posts/technical writing I have completed for clients.

To receive email notifications (Never Spam) from this blog (“Digital Owl’s Prose”) for the latest blog posts as they are published, please subscribe (of your own volition) by clicking the ‘Click To Subscribe!’ button in the sidebar on the homepage! (Feel free at any time to review the Digital Owl’s Prose Privacy Policy Page for any questions you may have about: email updates, opt-in, opt-out, contact forms, etc…)

Be sure and visit the “Best Of” page for a collection of my best blog posts.


Josh Otwell has a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.

Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided, are performed on a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.

How can I help you?

Disclosure: Some of the services and product links in this post are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.

The Newsletter for PHP and MySQL Developers

Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.

The post Multiple WHERE Clause Conditionals Using the MySQL AND Logical Operator appeared first on Digital Owl’s Prose.

Multiple WHERE Clause Conditionals Using the MySQL AND Logical Operator
Author: Joshua Otwell

 

No Comments

Add your comment