Order By Clause in MySQL
Hello and
welcome everyone. In this article, we will learn about the Order By Clause in
MySQL. Let’s go.
MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/
C# basic course click on: https://www.udemy.com/learn-csharp-with-sagar-jaybhay/
Web API 2- .Net Core In depth In 5 Hours
click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/
MySQL
Order by clause is used to sort the records or data in ascending or descending
order. Order by clause allows us to sort records or data by a single column or
multiple columns in ascending or descending order. There are two key using in
Order by clause:
ASC: - sorting data or records by
ascending order.
DESC: - sorting data or records by
descending order.
Syntax:
SELECT column_name_1, column_name_2, …
FROM table_name
WHERE condition
ORDER BY column_name_1 [
ASC | DESC ], column_name_2 [ ASC | DESC ]
Here is,
SELECT this statement using retrieve data from database
table in ascending or descending order.
WHERE condition is the optional used to condition on the query.
ORDER BY clause is performing sorting the records or data.
[
ASC | DESC ] this keyword used to sort data or records in ascending or
descending order.
Note: - ASC or ascending order is used as a default
on the SELECT statement.
For example
Here is
the, we will create a table for an employee in the database.
CREATE
TABLE IF NOT EXISTS employee (
e_id
int (10) PRIMARY KEY,
e_firstname
varchar (50) NOT NULL,
e_lastname
varchar (50) NOT NULL,
e_age
int (10),
e_city
varchar (250)
e_sex
char (1)
e_phnumber
int (15) NOT NULL
)
Here we
insert record in the employee table.
INSERT INTO employee (
‘e_id’, ’e_firstname’, ’e_lastname’,
’e_age’, ’e_city’, ’e_sex’,
’e_phnumber’
)
VALUES
(
1, ‘Hone’, ‘Reus’, 26, ‘San Jose’, ‘M’, ‘ +
1 555 555 1111’
),
(
2, ‘Cries’, ’Fence’, 23, ’Fresno’, ’F’, ’
+1 555 555 1111’
);
Final
Result
‘e_id’ ’e_firstname’ ’e_lastname’ ’e_age’ ’e_city’ ’e_sex’ ’e_phnumber’
1
Hone Reus 26 San Jose M
+1 555 555 1111
2 Cries Fence 23 Fresno F
+1 555 555 1111
Example
1:
Now I
want to get employee list sorted by employee age.
SELECT * FROM employee
ORDER BY e_age;
Example
2:
Now I
want to get only female employee list sorted by employee city with descending
order.
SELECT * FROM employee
WHERE e_sex = ‘F’
ORDER BY e_city DESC;
Example
3:
Now I
want to get employee list sorted by multiple columns like employee city and
employee sex.
SELECT * FROM employee
ORDER BY e_city, e_sex;
Example
4:
Now I
want to get employee list in city Fresno sorted by employee first name with
ascending order.
SELECT * FROM employee
WHERE e_city = ‘Fresno’
ORDER BY e_firstname ASC;
Example
5;
Now I
want to get employee list sorted by employee first name with descending order
and employee last name with ascending order.
SELECT * FROM employee
ORDER BY e_firstname DESC, e_lastname
ASC;
In this
article, we learned about ORDER BY clause with example.
MongoDB with .Net Core course link => https://www.udemy.com/mongodb-with-net-core-sagar-jaybhay/
C# basic course click on: https://www.udemy.com/learn-csharp-with-sagar-jaybhay/
Web API 2- .Net Core In depth In 5 Hours
click on: https://www.udemy.com/web-api-2-net-core-in-depth-in-5-hrs-with-sagar-jaybhay/
Comments
Post a Comment