devxlogo

Problem with Using the AND Condition

Question:
In the WHERE condition, I have not been able to use the AND condition when the two conditions on either side of the AND command relate to data the same column.

For instance, print out all trips made to New York and San Francisco. The query returns blank data.

Can you help me?

Answer:
As you haven’t attached the actual SQL you are using, I will have to make an educated guess based on your question.

Let’s assume that you have a table named “trips” with a field called “destination.” It sounds like you might have literally translated your English statement into something like this:

select * from tripswheredestination = 'NEW YORK'  anddestination = 'SAN FRANCISCO'

However, SQL follows the rules of Boolean logic, not English. The above statement actually tells the server only to return rows where the destination is ‘NEW YORK’ and ‘SAN FRANCISCO’. That is an impossibility.

Instead, rephrase your request as such. You want to return all rows where the destination is either ‘NEW YORK’ or ‘SAN FRANCISCO’. In SQL that would be:

select * from trips wheredestination = 'NEW YORK' ordestination = 'SAN FRANCISCO'

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.