Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

Is it possible to order when the data comes from many select and union them together? Such as

Select id,name,age

From Student

Where age < 15

Union

Select id,name,age

From Student

Where Name like "%a%"

How can I order this query by name?

Some said you can query look like this.

Select id,name,age

From Student

Where age < 15 or name like "%a%"

Order by name

But in this case, I just ignore that solution.

Thank you in advance.

1 Answer

0 votes
by (40.7k points)

You can try below code:

Select id,name,age

From Student

Where age < 15

Union

Select id,name,age

From Student

Where Name like "%a%"

Order by name

Note: The order by clause will be applied to the complete resultset

Related questions

0 votes
1 answer
asked May 4, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer
0 votes
1 answer
asked May 5, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked May 4, 2020 in SQL by Sudhir_1997 (55.6k points)
0 votes
1 answer
asked Apr 28, 2020 in SQL by Sudhir_1997 (55.6k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...