SQL query about database

I have some trouble with SQL. I have database structure like below enter image description here I should write a query that return: All moviestar's name and address, where they are woman(G='F') and address="Berlin" + all studio's name and address, where address="Berlin" too. Ordered by address.

My suggestion is:

SELECT * FROM
   (SELECT name, address 
   FROM MovieStar 
   WHERE gender = 'F'
   UNION 
   SELECT name, address FROM Studio) T 
WHERE T.address LIKE '%Berlin%' ORDER BY T.address;

Upvotes: 0

Views: 63

Answers (2)

Christof Hullaert
Christof Hullaert

Reputation: 311

You can just do this

SELECT name, address from MovieStar WHERE gender='F' AND address LIKE '%Berlin%'
UNION 
SELECT name, address from Studio WHERE address LIKE '%Berlin'

and if you want use subquery like you do in your example, I think you have to add "as" keyword for sub query

SELECT * FROM
   (SELECT name, address 
   FROM MovieStar 
   WHERE gender = 'F'
   UNION 
   SELECT name, address FROM Studio) AS T 
WHERE T.address LIKE '%Berlin%' ORDER BY T.address;

but I'm not sure, that's a good deal for performance since you will have a memory table created and then projected

Upvotes: 0

Amit Kumar Singh
Amit Kumar Singh

Reputation: 4475

This should work for you.

SELECT name, address 
   FROM MovieStar 
   WHERE gender = 'F' AND address LIKE '%Berlin%'
   UNION 
   SELECT name, address FROM Studio WHERE address LIKE '%Berlin%' 
ORDER BY address;

Upvotes: 1

Related Questions