MySQL ORDER BY Clause
Filtering the records
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | DB | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec) mysql> USE DB; Database changed mysql> SHOW TABLES; +--------------+ | Tables_in_DB | +--------------+ | holders | +--------------+ 1 row in set (0.01 sec) mysql> SELECT * FROM holders ORDER BY name; +-------------+---------------+---------+------------+----------+--------+ | account_no | name | city | dob | bank | amount | +-------------+---------------+---------+------------+----------+--------+ | 25622348994 | Aanya | Delhi | 1975-08-18 | SBI | 105000 | | 25622348990 | Donald Taylor | Irvine | 1990-08-20 | Citi | 7000 | | 25622348991 | Edward Parkar | Irvine | 1994-01-29 | ICICI | 95000 | | 25622348989 | James Moore | Phoenix | 1985-05-26 | Barclays | 5000 | | 25622348995 | James Moore | NULL | 1978-06-26 | Citi | 97800 | | 25622348993 | Marie Peters | Ribe | 1967-01-05 | DZBank | 12250 | | 25622348992 | Ryan Bakshi | Mumbai | 1982-01-14 | Citi | 50000 | +-------------+---------------+---------+------------+----------+--------+ 7 rows in set (0.00 sec) mysql> SELECT * FROM holders ORDER BY amount; +-------------+---------------+---------+------------+----------+--------+ | account_no | name | city | dob | bank | amount | +-------------+---------------+---------+------------+----------+--------+ | 25622348989 | James Moore | Phoenix | 1985-05-26 | Barclays | 5000 | | 25622348990 | Donald Taylor | Irvine | 1990-08-20 | Citi | 7000 | | 25622348993 | Marie Peters | Ribe | 1967-01-05 | DZBank | 12250 | | 25622348992 | Ryan Bakshi | Mumbai | 1982-01-14 | Citi | 50000 | | 25622348991 | Edward Parkar | Irvine | 1994-01-29 | ICICI | 95000 | | 25622348995 | James Moore | NULL | 1978-06-26 | Citi | 97800 | | 25622348994 | Aanya | Delhi | 1975-08-18 | SBI | 105000 | +-------------+---------------+---------+------------+----------+--------+ 7 rows in set (0.00 sec)
Using DESC
mysql> SELECT * FROM holders ORDER BY name DESC; +-------------+---------------+---------+------------+----------+--------+ | account_no | name | city | dob | bank | amount | +-------------+---------------+---------+------------+----------+--------+ | 25622348992 | Ryan Bakshi | Mumbai | 1982-01-14 | Citi | 50000 | | 25622348993 | Marie Peters | Ribe | 1967-01-05 | DZBank | 12250 | | 25622348989 | James Moore | Phoenix | 1985-05-26 | Barclays | 5000 | | 25622348995 | James Moore | NULL | 1978-06-26 | Citi | 97800 | | 25622348991 | Edward Parkar | Irvine | 1994-01-29 | ICICI | 95000 | | 25622348990 | Donald Taylor | Irvine | 1990-08-20 | Citi | 7000 | | 25622348994 | Aanya | Delhi | 1975-08-18 | SBI | 105000 | +-------------+---------------+---------+------------+----------+--------+ 7 rows in set (0.00 sec) mysql> SELECT * FROM holders ORDER BY amount DESC; +-------------+---------------+---------+------------+----------+--------+ | account_no | name | city | dob | bank | amount | +-------------+---------------+---------+------------+----------+--------+ | 25622348994 | Aanya | Delhi | 1975-08-18 | SBI | 105000 | | 25622348995 | James Moore | NULL | 1978-06-26 | Citi | 97800 | | 25622348991 | Edward Parkar | Irvine | 1994-01-29 | ICICI | 95000 | | 25622348992 | Ryan Bakshi | Mumbai | 1982-01-14 | Citi | 50000 | | 25622348993 | Marie Peters | Ribe | 1967-01-05 | DZBank | 12250 | | 25622348990 | Donald Taylor | Irvine | 1990-08-20 | Citi | 7000 | | 25622348989 | James Moore | Phoenix | 1985-05-26 | Barclays | 5000 | +-------------+---------------+---------+------------+----------+--------+ 7 rows in set (0.00 sec)
Using ASC and DESC
mysql> SELECT * FROM holders ORDER BY city DESC, amount ASC; +-------------+---------------+---------+------------+----------+--------+ | account_no | name | city | dob | bank | amount | +-------------+---------------+---------+------------+----------+--------+ | 25622348993 | Marie Peters | Ribe | 1967-01-05 | DZBank | 12250 | | 25622348989 | James Moore | Phoenix | 1985-05-26 | Barclays | 5000 | | 25622348992 | Ryan Bakshi | Mumbai | 1982-01-14 | Citi | 50000 | | 25622348990 | Donald Taylor | Irvine | 1990-08-20 | Citi | 7000 | | 25622348991 | Edward Parkar | Irvine | 1994-01-29 | ICICI | 95000 | | 25622348994 | Aanya | Delhi | 1975-08-18 | SBI | 105000 | | 25622348995 | James Moore | NULL | 1978-06-26 | Citi | 97800 | +-------------+---------------+---------+------------+----------+--------+ 7 rows in set (0.00 sec) mysql> SELECT * FROM holders ORDER BY name ASC, amount DESC; +-------------+---------------+---------+------------+----------+--------+ | account_no | name | city | dob | bank | amount | +-------------+---------------+---------+------------+----------+--------+ | 25622348994 | Aanya | Delhi | 1975-08-18 | SBI | 105000 | | 25622348990 | Donald Taylor | Irvine | 1990-08-20 | Citi | 7000 | | 25622348991 | Edward Parkar | Irvine | 1994-01-29 | ICICI | 95000 | | 25622348995 | James Moore | NULL | 1978-06-26 | Citi | 97800 | | 25622348989 | James Moore | Phoenix | 1985-05-26 | Barclays | 5000 | | 25622348993 | Marie Peters | Ribe | 1967-01-05 | DZBank | 12250 | | 25622348992 | Ryan Bakshi | Mumbai | 1982-01-14 | Citi | 50000 | +-------------+---------------+---------+------------+----------+--------+ 7 rows in set (0.00 sec) mysql> SELECT * FROM holders ORDER BY name ASC, bank DESC; +-------------+---------------+---------+------------+----------+--------+ | account_no | name | city | dob | bank | amount | +-------------+---------------+---------+------------+----------+--------+ | 25622348994 | Aanya | Delhi | 1975-08-18 | SBI | 105000 | | 25622348990 | Donald Taylor | Irvine | 1990-08-20 | Citi | 7000 | | 25622348991 | Edward Parkar | Irvine | 1994-01-29 | ICICI | 95000 | | 25622348995 | James Moore | NULL | 1978-06-26 | Citi | 97800 | | 25622348989 | James Moore | Phoenix | 1985-05-26 | Barclays | 5000 | | 25622348993 | Marie Peters | Ribe | 1967-01-05 | DZBank | 12250 | | 25622348992 | Ryan Bakshi | Mumbai | 1982-01-14 | Citi | 50000 | +-------------+---------------+---------+------------+----------+--------+ 7 rows in set (0.00 sec)
Advertisement