
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to use union and order by clause in MySQL?
Union is a type of operator in MySQL. We can use ORDER BY with this to filter records. Use UNION if you want to select rows one after the other from several tables or several sets of rows from a single table all as a single result set.
Let us see an example.
Creating first table
mysql> create table UnionDemo1 -> ( -> id int -> ); Query OK, 0 rows affected (0.59 sec)
Inserting records into first table.
mysql> insert into UnionDemo1 values(1),(4),(10); Query OK, 3 rows affected (0.21 sec) Records: 3 Duplicates: 0 Warnings: 0
To display all records.
mysql> select *from UnionDemo1;
The following is the output that displays all the records.
+------+ | id | +------+ | 1 | | 4 | | 10 | +------+ 3 rows in set (0.00 sec)
Let us create second table.
mysql> create table UnionDemo2 -> ( -> id int -> ); Query OK, 0 rows affected (0.54 sec)
Inserting records into second table.
mysql> insert into UnionDemo2 values(2),(3),(5),(6),(7),(8),(9); Query OK, 7 rows affected (0.21 sec) Records: 7 Duplicates: 0 Warnings: 0
To display all the records.
mysql> select *from UnionDemo2;
Here is the output.
+------+ | id | +------+ | 2 | | 3 | | 5 | | 6 | | 7 | | 8 | | 9 | +------+ 7 rows in set (0.00 sec)
Let us now see the query to apply UNION with Order by.
mysql> select id from UnionDemo1 -> union -> select id from UnionDemo2 -> order by id desc;
The following is the output.
+------+ | id | +------+ | 10 | | 9 | | 8 | | 7 | | 6 | | 5 | | 4 | | 3 | | 2 | | 1 | +------+ 10 rows in set (0.05 sec)
- Related Articles
- What is the use of ORDER BY clause in MySQL?
- How to order or choose rows in MySQL GROUP BY clause?
- How can I use RAND() function in an ORDER BY clause to shuffle MySQL set of rows?
- MySQL UNION SELECT and IN clause in a single query
- How can we use ORDER BY clause while calculating the Date?
- How to get MySQL query result in same order as given by IN clause?
- How can we create the MySQL view with ORDER BY clause?
- How to use ORDER BY field and sort by id in a single MySQL field?
- Preserve select order within MySQL UNION?
- How Can we use MySQL DISTINCT clause with WHERE and LIMIT clause?
- How to select MySQL rows in the order of IN clause?
- How to use user variables in MySQL LIKE clause?
- While using the ROLLUP modifier, is it possible to use a MySQL ORDER BY clause to sort the result?
- Sort by order of values in a MySQL select statement IN clause?
- How to use MySQL VIEW with WHERE clause?

Advertisements