Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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.4k 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.5k points)
0 votes
1 answer
0 votes
1 answer
asked May 5, 2020 in SQL by Sudhir_1997 (55.5k points)
0 votes
1 answer
asked May 4, 2020 in SQL by Sudhir_1997 (55.5k points)
0 votes
1 answer
asked Apr 28, 2020 in SQL by Sudhir_1997 (55.5k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...