Mysql order by two columns – 10 MySQL Order By Multiple columns DESC & ASC Query with Best EXAMPLE

Today, We want to share with you mysql order by two columns.In this post we will show you mysql order by multiple columns, hear for DESC & ASC Query with Best EXAMPLE we will give you demo and example for implement.In this post, we will learn about MySQL ORDER BY DESC & ASC with an example.

mysql order by two columns

Default sorting is ascending, you need to add the keyword DESC to both your orders: mysql order by two columns

ORDER BY fruits_rating DESC, fruits_time DESC

OR

ORDER BY fruits_rating, fruits_time DESC

MySQL Ascending Order

SELECT * FROM fruits ORDER BY fruit_name ASC;

MySQL Descending Order

SELECT * FROM fruits ORDER BY fruit_name DESC;

MySQL ORDER BY clause : Syntax

SELECT 
   fruit_name,fruit_price,qty
FROM 
   fruits
ORDER BY 
   fruit_name [ASC|DESC], 
   fruit_price [ASC|DESC],
   ...;

Using ORDER BY to sort on two columns

INSERT statement

INSERT INTO fruits
    (
        fruits_id, fruits_name, vendor_name, fruits_size
    )
VALUES
    (1, 'First fruits', 'Pakainfo', 100),
    (2, 'Second fruitsI', 'Infinityknow', 1000),
    (2, 'Second fruitsII', 'Infinityknow', 9000),
    (3, 'Third fruitsI', 'Pakainfo', 400),
    (3, 'Third fruitsII', 'Pakainfo', 800),
    (4, 'Fourth fruits', 'Infinityknwo', 4000);

mysql order by two columns – Example

SELECT 
    fruits_id, fruits_name, vendor_name, fruits_size
FROM
    fruits
ORDER BY vendor_name ASC, fruits_size DESC;

mysql order by

To get the sorted result from MySQL select statement we use order by clause with SELECT statement.

mysql order by two columns – Syntax:

SELECT column_list FROM fruits ORDER BY column_name1 [ASC][DESC],column_name2 [ASC][DESC],...    

ascending order mysql

  
 SELECT 
   column_name1, column_name2, column_name3
FROM 
   table_name
ORDER BY 
   column_name1 [ASC|DESC], 
   column_name2 [ASC|DESC],
   ...;

Order two columns with different orders

in mysql order by two columns Example

//step 1
Drop table fruits;

//step 2
CREATE TABLE fruits (
    id int unsigned not null auto_increment primary key,
    fruitname varchar(20),
    lastname varchar(20),
    title varchar(30),
    qty int,
    organic int,
    price int,
    perks int,
    type varchar(60)
); 


//step 3
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Banana", "Chen", "New kids", 31, 3, 120000, 25000, "tamil.com");

INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Orange", "Pillai", "New kids", 32, 4, 110000, 20000, "fruits.com");

INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Grephs", "Pandit", "Web Men", 24, 3, 90000, 15000, "kanada.com");

INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Mary", "Anchor", "Web Men", 27, 2, 85000, 15000, "mail.com");

INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Charrey", "King", "kids", 32, 3, 75000, 15000, "net.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Banana", "Mac", "kids", 32, 4, 80000, 16000, "tamil.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Apple small", "Sam", "kids", 28, 2, 75000, 14000, "fruits.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Bulk", "Nanda", "kids", 32, 3, 70000, 10000, "fruits.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("MangoNew", "Ra", "Latest kids", 32, 4, 90000, 15000, "kanada.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Orange", "Simon", "Latest kids", 23, 1, 85000, 12000, "kanada.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Tamil", "Parhar", "Latest kids", 30, 2, 75000, 15000, "tamil.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("pinapale", "Hunter", "New Web Men", 32, 4, 110000, 20000, "coolmail.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Kivi", "Lewis", "System Administrator", 32, 3, 100000, 13000, "mail.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Danny", "Gibson", "System Administrator", 31, 2, 90000, 12000, "tamil.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("banana", "Harper", "New Girl childern", 36, 1, 120000, 28000, "kanada.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Apple1", "Sunday", "Girl childern", 31, 5, 90000, 25000, "bikanada.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Mango", "Sim", "Girl childern", 27, 1, 70000, 18000, "kanada.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Appo", "Irvine", "Girl childern", 27, 1, 72000, 18000, "tamil.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("heny", "Ali", "Customer Service Manqtyr", 32, 3, 70000, 9000, "tamil.com");
INSERT INTO fruits (fruitname, fruitType, title, qty, organic, price, perks, type) values ("Apple", "Champion", "Finance Manqtyr", 32, 2, 120000, 25000, "fruits.com");

//step 4
select * from fruits;
  
//step 5 : The statement displays the ids, last names, titles and qtys of all fruitss sorted on titles in descending order and qty in ascending order.
//mysql order by two columns
SELECT id, fruitsName, title, qty
       from fruits ORDER BY
       title DESC, qty ASC;

mysql order by multiple columns

mysql multiple order by

Read Also:  How to Guide on Scraping Social Media Data with PHP?

   ORDER BY col1, col2 DESC, col3;

SELECT S.*, B.book_id, B.issue_date
	FROM fruits S JOIN books B ON S.id = B.fruits_id ORDER BY B.book_id, S.class;

How to Order By Two Columns in SQL?

mysql order by multiple columns

SELECT * FROM fruits ORDER BY type ASC;				-- ASCending is default
SELECT * FROM fruits ORDER BY type DESC;			-- DESCending
SELECT * FROM fruits ORDER BY type DESC, col2 ASC;	-- type DESC then col2 ASC

Using with multiple columns

SELECT * FROM emp_price ORDER BY qty ASC, price DESC

SQL SORTING ON MULTIPLE COLUMNS

Let’s take an example of fruits table which has mysql order by two columns, the following SQL statement selects all fruits from the table named “fruits”, stored by the “type” and “fruits-Name” columns:

Read Also:  mysql left join multiple tables - MySQL LEFT JOIN 3 tables Best 5 Examples

SELECT * FROM fruits  
ORDER BY type, fruits-Name;  

Web Programming Tutorials Example with Demo

Summary


I hope you get an idea about mysql order by two columns.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.