1

Let's say I have 2 tables

People (id, name)
Dogs (id, name)

Can a view be created that contains all the dog names and all the people names so that for instance if I wanted to count how many entities are called Toby I'd have to query just the view?

Ideally it'd look something like

  1. John - where John came from the People table
  2. Toby - and Toby came from the dogs table

I'm using a PostgreSQL database but I'd be interested to know if any is able to do this.

1
  • 2
    CREATE VIEW w AS SELECT name FROM People UNION ALL SELECT name FROM dogs could be used Commented Nov 28, 2020 at 16:13

1 Answer 1

1

You could use the union all set operator to unify results from both tables:

CREATE VIEW all_names AS 
SELECT name FROM people
UNION ALL
SELECT name FROM dogs
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.