Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Databases The Solutions

Compound Queries

Is it possible to form a query where table1.first_name + " " + table1.last_name matches table2.customer_name?

IE: customers.first_name = "John" customers.last_name = "Doe"

orders.customer_name = "John Doe"

It seems to me this would be a common query, but just can't come up with the syntax intuitively...

2 Answers

Steven Parker
Steven Parker
231,128 Points

You nearly wrote it yourself! But remember that the SQL concatenation operator is "||" (not "+"):

... WHERE table1.first_name || " " || table1.last_name = table2.customer_name

Stephen,

Evidently I'm a wee bit rusty on SQL syntax, but your solution worked like a champ, so a 1000 thanks!!!

I went with:

SELECT * 
FROM customers, sales
WHERE customers.first_name || " " || customers.last_name = sales.customer_name
ORDER BY sales.id;

It worked as I intended!

Thanks again!

-Pete