Databases Querying Relational Databases Set Operations Set Operations

Troy Erby
Troy Erby
6,332 Points

Trouble with Set Operations

Here is the challenge question:

There are two tables Fruit and Vegetable table. The Fruit table has a FruitID and a Name column and the Vegetable table has a VegetableID and Name column.

Create a list of all fruits and vegetables starting with the letters A through K . In other words all fruit and vegetables that don't start with the letter L to Z.

The code I used as an answer was:

SELECT name FROM fruit UNION SELECT name FROM vegetable WHERE name < "L"

The error message I got back is that my result did not return values for A-K

I apologize if this question is repetitive. I saw that another student posted the same question using the same line of code I did two days ago, but they didn't have a posted answer and I'm running into the same wall

1 Answer

Steven Parker
Steven Parker
203,270 Points

You're close, but the UNION combines complete queries. So just as each one has a SELECT clause and a FROM, they each also need their own WHERE clause.

Troy Erby
Troy Erby
6,332 Points

Wow! That did the trick. Thanks.

I could swear in the video Andrew distinctly said that only 1 WHERE clause was needed when using UNION statements but clearly using WHERE twice worked. Maybe I'm confusing it with a statement he made about JOIN functions. Either way, that you greatly for the help.

Steven Parker
Steven Parker
203,270 Points

A "JOIN" is essentially an extension of the "FROM" clause, so a query with one (or more) of them would still only need one WHERE.