AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

SQL ORDER BY clause


<< Previous Page
SQL tutorial
Next Page >>
SQL tutorial

SQL Tutorial » SQL ORDER BY clause

How does ORDER BY work in SQL ?
ORDER BY clause is used to sort the result in ascending or descending order as you need for your web page.
By default ORDER BY sorts the data in ascending order. To sort the records in descending order, use the DESC keyword.

Syntax:

SELECT column_1, column_2, ...
FROM table_name
ORDER BY column_1, column_2, ... ASC|DESC;

Let's look at the "Users" table: We will have five columns as: "id", "first_name", "last_name", "age", "email":

ID First Name Last Name Age Email
1 Clark Kent 24 clarkkent@mail.com
2 Pire Joveau 31 pierej@mail.com
3 Nicky Anderson 19 nickya@mail.com
4 Pepe Martínez 38 martinezpeper@mail.com
5 Peter Parker 21 peterparker@mail.com

 

SQL ORDER BY Example

The following SQL statement selects all users from the "Users" table, sorted by the "Age" column - Example:

SELECT * FROM Users
ORDER BY age;

Output displayed:

ID First Name Last Name Age Email
3 Nicky Anderson 19 nickya@mail.com
5 Peter Parker 21 peterparker@mail.com
1 Clark Kent 24 clarkkent@mail.com
2 Pire Joveau 31 pierej@mail.com
4 Pepe Martínez 38 martinezpeper@mail.com

Related subjects:
SQL add columns to a table SQL in operator SQL where clause Conect to MySQLi PHP MySQLi create table

Tags: sql order by: date, multiple columns, of operations, two 2 columns, by group by, by then by, alphabetically, by 1 How does ORDER BY work in SQL?, What is ORDER BY clause of SQL statement?, What is ORDER BY 2 desc in SQL?, Where do I put DESC in SQL?

 

SQL ORDER BY Several Columns Example

The next SQL statement selects all users from the "Users" table, sorted by the "last_name" and "age" column. This means that it orders by last_name, but if some rows have the same age, it orders them by last_name:

SELECT * FROM Users
ORDER BY last_name, age;

SQL ORDER BY Several Columns The following SQL statement selects all Users from the "Users" table, sorted ascending by the "last_name" and descending by the "age" column:

SELECT * FROM Users
ORDER BY last_name ASC, age DESC;

 

SQL ORDER BY Several Columns Example

The next SQL statement selects all users from the "Users" table, sorted by the "last_name" and "age" column. This means that it orders by last_name, but if some rows have the same age, it orders them by last_name:

SELECT * FROM Users
ORDER BY last_name, age;

SQL ORDER BY Several Columns The following SQL statement selects all Users from the "Users" table, sorted ascending by the "last_name" and descending by the "age" column:

SELECT * FROM Users
ORDER BY last_name ASC, age DESC;

 



sql order by: date, multiple columns, of operations, two 2 columns, by group by, by then by, alphabetically, by 1 How does ORDER BY work in SQL?, What is ORDER BY clause of SQL statement?, What is ORDER BY 2 desc in SQL?, Where do I put DESC in SQL?
SQL ORDER BY clause - sql tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home - Database
SQL - Database
SQL Syntax
SQL Select Data
SQL Insert Data
SQL Update
SQL Delete Row
PHP mysqli_query()
SQL order
SQL Create Database
SQL Display Data
SQL Count()
SQL mysqli_connect
SQL create table
SQL Where And & Or
SQL like Syntax
SQL Between Operator
SQL IN Operator
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...