Problem with Using the AND Condition

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'

Share the Post:
data observability

Data Observability Explained

Data is the lifeblood of any successful business, as it is the driving force behind critical decision-making, insight generation, and strategic development. However, due to its intricate nature, ensuring the

Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular