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 TRUE) and 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:
- Limit Rows with the WHERE clause – MySQL Beginner Series
- SELECT clause queries – MySQL Beginner Basics Series.
I appreciate you reading my content. Support me today with a Tip if you would like. Thank you! 🙏🏻
Thank you for reading this post. Please share it with someone else who would enjoy it as well.
Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.
Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.
More ways I can help
- Need hosting for your next web application or WordPress site? I highly recommend Hostinger and use them to host my niche bass fishing site. The service is second to none.
- 🔒5 Truths I’ve Come To Realize As a Self-taught Developer
- Desktop and mobile wallpapers, digital downloads, photography services, Shopify and WooCommerce customizations, and content writing – all in one E-commerce Shop. Find your next digital purchase today!
- Take your Laravel applications next level with Battle Ready Laravel by Ash Allen. Learn how to improve the performance, maintainability, and security of your Laravel projects in this e-book.
Disclosure: Some of this blog post’s services and product links 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.
One thought on “Multiple WHERE Clause Conditionals Using the MySQL AND Logical Operator”