Skip to content
Advertisement

Combining 2 Tables in SQL and add a new column to a Table

I have 2 Tables. CustomerDetails table has columns ID, UUID, Name, Age. FavoriteCustomers table has columns ID, UUID

CustomerDetails Table will have a lot of entries say 10000 rows. while FavoriteCustomers Table has very few entries. In CustomerDetails Table, ID and UUID form the combined unique ID.

Now I want the result to be combined row columns like below with a where clause.

ID, UUID, Name, Age, isFavorite WHERE Age is > 30

Advertisement

Answer

In DBMS that support some kind of a boolean type you can use an EXISTS clause or an IN clause.

select
  id, uuid, name, age,
  (id, uuid) in (select id, uuid from favoritecustomers) as is_favorite
from customerdetails
where age > 30;
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement