{"id":129,"date":"2015-11-29T18:33:31","date_gmt":"2015-11-29T11:33:31","guid":{"rendered":"http:\/\/www.sqlitetutorial.net\/?page_id=129"},"modified":"2024-05-05T15:20:18","modified_gmt":"2024-05-05T08:20:18","slug":"sqlite-order-by","status":"publish","type":"page","link":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/","title":{"rendered":"SQLite Order By"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to sort a result set of a query using SQLite <code>ORDER BY<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to SQLite ORDER BY clause<\/h2>\n\n\n\n<p>SQLite stores rows in a table in an unspecified order. It means that the rows in the table may or may not be in the order they were inserted.<\/p>\n\n\n\n<p>If you use the&nbsp; <code><a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-select\/\">SELECT<\/a><\/code> statement to retrieve rows from a table, the order of rows in the result set is unspecified.<\/p>\n\n\n\n<p>To sort the rows in a result set, you add the <code>ORDER BY<\/code> clause to the&nbsp; <code>SELECT<\/code> statement as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n   select_list\n<span class=\"hljs-keyword\">FROM<\/span>\n   <span class=\"hljs-keyword\">table<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n    column_1 <span class=\"hljs-keyword\">ASC<\/span>,\n    column_2 <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>ORDER BY<\/code> clause comes after the <code>FROM<\/code> clause. It allows you to sort the result set based on one or more columns in ascending or descending order.<\/p>\n\n\n\n<p>In this syntax, you place the column name by which you want to sort after the <code>ORDER BY<\/code> clause followed by the <code>ASC<\/code> or <code>DESC<\/code> keyword.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>ASC<\/code> keyword means ascending.<\/li>\n\n\n\n<li>The <code>DESC<\/code> keyword means descending.<\/li>\n<\/ul>\n\n\n\n<p>If you don&#8217;t specify the <code>ASC<\/code> or <code>DESC<\/code> keyword, SQLite sorts the result set using the <code>ASC<\/code> option. In other words, it sorts the result set in ascending order by default.<\/p>\n\n\n\n<p>If you want to sort the result set by multiple columns, you use a comma (,) to separate two columns. <\/p>\n\n\n\n<p>The <code>ORDER BY<\/code> clause sorts rows using columns or expressions from left to right. In other words, the <code>ORDER BY<\/code> clause sorts the rows using the first column in the list. Then, it sorts the sorted rows using the second column, and so on.<\/p>\n\n\n\n<p>SQLite allows you to sort the result set using columns that do not appear in the select list of the <code>SELECT<\/code> clause.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SQLite ORDER BY clause examples<\/h2>\n\n\n\n<p>Let&#8217;s take the <code>tracks<\/code> table in the <a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-sample-database\/\">sample database<\/a> for the demonstration.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"174\" height=\"224\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png\" alt=\"\" class=\"wp-image-1558\"\/><\/figure>\n\n\n\n<p>Suppose, you want to get data from name, milliseconds, and album id columns, you use the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">Name<\/span>,\n  Milliseconds,\n  AlbumId\n<span class=\"hljs-keyword\">FROM<\/span>\n  tracks;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-order-by\/#1\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/tracks-table-data-without-sorting.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"413\" height=\"160\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/tracks-table-data-without-sorting.jpg\" alt=\"tracks table data without sorting\" class=\"wp-image-132\" srcset=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/tracks-table-data-without-sorting.jpg 413w, https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/tracks-table-data-without-sorting-300x116.jpg 300w\" sizes=\"auto, (max-width: 413px) 100vw, 413px\" \/><\/a><\/figure>\n\n\n\n<p>The <code>SELECT<\/code> statement that does not use <code>ORDER BY<\/code> clause returns rows that are in an unspecified order.<\/p>\n\n\n\n<p>Suppose you want to sort the result set based on <code>AlbumId<\/code> column in ascending order, you use the following statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">name<\/span>,\n  milliseconds,\n  albumid\n<span class=\"hljs-keyword\">FROM<\/span>\n  tracks\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  albumid <span class=\"hljs-keyword\">ASC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-order-by\/#2\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-example.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"404\" height=\"340\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-example.jpg\" alt=\"SQLite ORDER BY example\" class=\"wp-image-134\" srcset=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-example.jpg 404w, https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-example-300x252.jpg 300w\" sizes=\"auto, (max-width: 404px) 100vw, 404px\" \/><\/a><\/figure>\n\n\n\n<p>The result set now is sorted by the <code>AlbumId<\/code> column in ascending order as shown in the screenshot.<\/p>\n\n\n\n<p>SQLite uses <code>ASC<\/code> by default so you can omit it in the above statement as follows:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">SELECT\n  name,\n  milliseconds,\n  albumid\nFROM\n  tracks\nORDER BY\n  albumid;<\/code><\/span><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-order-by\/#3\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<p>Suppose you want to sort the sorted result (by <code>AlbumId<\/code>) above by the <code>Milliseconds<\/code> column in descending order. In this case, you need to add the <code>Milliseconds<\/code> column to the <code>ORDER BY<\/code> clause as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">name<\/span>,\n  milliseconds,\n  albumid\n<span class=\"hljs-keyword\">FROM<\/span>\n  tracks\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  albumid <span class=\"hljs-keyword\">ASC<\/span>,\n  milliseconds <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-order-by\/#4\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-multiple-columns-example.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"402\" height=\"340\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-multiple-columns-example.jpg\" alt=\"SQLite ORDER BY multiple columns example\" class=\"wp-image-135\" srcset=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-multiple-columns-example.jpg 402w, https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-multiple-columns-example-300x254.jpg 300w\" sizes=\"auto, (max-width: 402px) 100vw, 402px\" \/><\/a><\/figure>\n\n\n\n<p>SQLite sorts rows by <code>AlbumId<\/code> column in ascending order first. Then, it sorts the sorted result set by the <code>Milliseconds<\/code> column in descending order.<\/p>\n\n\n\n<p>If you look at the tracks of the album with <code>AlbumId<\/code> 1, you&#8217;ll find that the order of tracks changes between the two statements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">SQLite ORDER BY with the column position<\/h2>\n\n\n\n<p>Instead of specifying the names of columns, you can use the column&#8217;s position in the <code>ORDER BY<\/code> clause.<\/p>\n\n\n\n<p>For example, the following statement sorts the tracks by both <code>AlbumId<\/code> (3rd column) and <code>Milliseconds<\/code> (2nd column) in ascending order.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  <span class=\"hljs-keyword\">name<\/span>,\n  milliseconds,\n  albumid\n<span class=\"hljs-keyword\">FROM<\/span>\n  tracks\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  <span class=\"hljs-number\">3<\/span>,\n  <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a class=\"sql\" href=\"https:\/\/www.sqlitetutorial.net\/tryit\/query\/sqlite-order-by\/#5\" target=\"_blank\" rel=\"noopener noreferrer\">Try It<\/a><\/p>\n\n\n\n<p>The number 3 and 2 refers to the <code>AlbumId<\/code> and <code>Milliseconds<\/code> in the column list that appears in the <code>SELECT<\/code> clause.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-multiple-columns-by-positions.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"415\" height=\"340\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-multiple-columns-by-positions.jpg\" alt=\"SQLite ORDER BY multiple columns by positions\" class=\"wp-image-136\" srcset=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-multiple-columns-by-positions.jpg 415w, https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2015\/11\/SQLite-ORDER-BY-multiple-columns-by-positions-300x246.jpg 300w\" sizes=\"auto, (max-width: 415px) 100vw, 415px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Sorting NULLs<\/h2>\n\n\n\n<p>In the database world, NULL is special. It denotes that the information is missing or the data is not applicable.<\/p>\n\n\n\n<p>Suppose you want to store the birthday of an artist in a table. At the time of saving the artist&#8217;s record, you don&#8217;t have the birthday information.<\/p>\n\n\n\n<p>To represent the unknown birthday information in the database, you may use a special date like <code>01.01.1900<\/code> or an <code>''<\/code> empty string. However, these values do not clearly show that the birthday is unknown.<\/p>\n\n\n\n<p>NULL was invented to resolve this issue. Instead of using a special value to indicate that the information is missing, NULL is used.<\/p>\n\n\n\n<p>NULL is special because you cannot compare it with another value. Simply put, if the two pieces of information are unknown, you cannot compare them.<\/p>\n\n\n\n<p>NULL is even cannot be compared with itself; NULL is not equal to itself so <code>NULL = NULL<\/code> always results in false.<\/p>\n\n\n\n<p>When it comes to sorting, SQLite considers NULL to be smaller than any other value.<\/p>\n\n\n\n<p>It means that NULLs will appear at the beginning of the result set if you use ASC or at the end of the result set when you use DESC.<\/p>\n\n\n\n<p>SQLite 3.30.0 added the <code>NULLS FIRST<\/code> and <code>NULLS LAST<\/code> options to the ORDER BY clause. The NULLS FIRST option specifies that the NULLs will appear at the beginning of the result set while the NULLS LAST option places NULLs at the end of the result set.<\/p>\n\n\n\n<p>The following example uses the <code>ORDER BY<\/code> clause to sort tracks by composers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    TrackId, \n    <span class=\"hljs-keyword\">Name<\/span>, \n    Composer \n<span class=\"hljs-keyword\">FROM<\/span> \n   tracks\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n   Composer;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>First,  NULLs appear at the beginning of the result set because SQLite treats them as the lowest values. When you scroll down the result, you will see other values:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"575\" height=\"321\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2020\/03\/SQLite-ORDER-BY-NULLS-FIRST.png\" alt=\"SQLite ORDER BY NULLS FIRST\" class=\"wp-image-2250\" srcset=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2020\/03\/SQLite-ORDER-BY-NULLS-FIRST.png 575w, https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2020\/03\/SQLite-ORDER-BY-NULLS-FIRST-300x167.png 300w\" sizes=\"auto, (max-width: 575px) 100vw, 575px\" \/><\/figure>\n\n\n\n<p>The following example uses the <code>NULLS LAST<\/code> option to place NULLs after other values:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    TrackId, \n    <span class=\"hljs-keyword\">Name<\/span>, \n    Composer \n<span class=\"hljs-keyword\">FROM<\/span> \n    tracks\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span> \n    Composer <span class=\"hljs-keyword\">NULLS<\/span> <span class=\"hljs-keyword\">LAST<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you scroll down the output, you will see that NULLs are placed at the end of the result set:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"578\" height=\"407\" src=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2020\/03\/SQLite-ORDER-BY-NULLS-LAST.png\" alt=\"\" class=\"wp-image-2251\" srcset=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2020\/03\/SQLite-ORDER-BY-NULLS-LAST.png 578w, https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2020\/03\/SQLite-ORDER-BY-NULLS-LAST-300x211.png 300w\" sizes=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the SQLite <code>ORDER BY<\/code> clause to sort rows by one or more columns in ascending and descending orders.<\/li>\n\n\n\n<li>Use <code>ASC<\/code> to sort rows in ascending order.<\/li>\n\n\n\n<li>Use <code>DESC<\/code> to sort rows in descending order.<\/li>\n\n\n\n<li>Use the <code>NULLS FIRST<\/code> option to place NULLs before other non-null values.<\/li>\n\n\n\n<li>Use the <code>NULLS LAST<\/code> option to place NULL after other non-null values.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"129\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\"\n\t\t\t\tdata-post-title=\"SQLite Order By\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"129\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\"\n\t\t\t\tdata-post-title=\"SQLite Order By\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to sort a result set of a query using SQLite ORDER BY clause.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":2,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-129","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQLite Order By - Sorting Result Set in Various Orders<\/title>\n<meta name=\"description\" content=\"Show you how to use SQLite ORDER BY clause to sort the result set using a single column, multiple columns in ascending and descending order.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQLite Order By - Sorting Result Set in Various Orders\" \/>\n<meta property=\"og:description\" content=\"Show you how to use SQLite ORDER BY clause to sort the result set using a single column, multiple columns in ascending and descending order.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\" \/>\n<meta property=\"og:site_name\" content=\"SQLite Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-05-05T08:20:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427\"},\"headline\":\"SQLite Order By\",\"datePublished\":\"2015-11-29T11:33:31+00:00\",\"dateModified\":\"2024-05-05T08:20:18+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\"},\"wordCount\":864,\"image\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png\",\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\",\"url\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\",\"name\":\"SQLite Order By - Sorting Result Set in Various Orders\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png\",\"datePublished\":\"2015-11-29T11:33:31+00:00\",\"dateModified\":\"2024-05-05T08:20:18+00:00\",\"description\":\"Show you how to use SQLite ORDER BY clause to sort the result set using a single column, multiple columns in ascending and descending order.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#primaryimage\",\"url\":\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png\",\"contentUrl\":\"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png\",\"width\":174,\"height\":224,\"caption\":\"SQLite trunc() function - sample table\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlitetutorial.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQLite Tutorial\",\"item\":\"https:\/\/www.sqlitetutorial.net\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQLite Order By\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/#website\",\"url\":\"https:\/\/www.sqlitetutorial.net\/\",\"name\":\"SQLite Tutorial\",\"description\":\"A Step-by-step SQLite Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlitetutorial.net\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427\",\"name\":\"admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQLite Order By - Sorting Result Set in Various Orders","description":"Show you how to use SQLite ORDER BY clause to sort the result set using a single column, multiple columns in ascending and descending order.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/","og_locale":"en_US","og_type":"article","og_title":"SQLite Order By - Sorting Result Set in Various Orders","og_description":"Show you how to use SQLite ORDER BY clause to sort the result set using a single column, multiple columns in ascending and descending order.","og_url":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/","og_site_name":"SQLite Tutorial","article_modified_time":"2024-05-05T08:20:18+00:00","og_image":[{"url":"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#article","isPartOf":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/"},"author":{"name":"admin","@id":"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427"},"headline":"SQLite Order By","datePublished":"2015-11-29T11:33:31+00:00","dateModified":"2024-05-05T08:20:18+00:00","mainEntityOfPage":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/"},"wordCount":864,"image":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png","inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/","url":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/","name":"SQLite Order By - Sorting Result Set in Various Orders","isPartOf":{"@id":"https:\/\/www.sqlitetutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png","datePublished":"2015-11-29T11:33:31+00:00","dateModified":"2024-05-05T08:20:18+00:00","description":"Show you how to use SQLite ORDER BY clause to sort the result set using a single column, multiple columns in ascending and descending order.","breadcrumb":{"@id":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#primaryimage","url":"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png","contentUrl":"https:\/\/www.sqlitetutorial.net\/wp-content\/uploads\/2018\/11\/tracks.png","width":174,"height":224,"caption":"SQLite trunc() function - sample table"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlitetutorial.net\/sqlite-order-by\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlitetutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQLite Tutorial","item":"https:\/\/www.sqlitetutorial.net\/"},{"@type":"ListItem","position":3,"name":"SQLite Order By"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlitetutorial.net\/#website","url":"https:\/\/www.sqlitetutorial.net\/","name":"SQLite Tutorial","description":"A Step-by-step SQLite Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlitetutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlitetutorial.net\/#\/schema\/person\/6d69b968cad0102e30d6694ed8dc6427","name":"admin"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/129","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/comments?post=129"}],"version-history":[{"count":5,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/129\/revisions"}],"predecessor-version":[{"id":3489,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/129\/revisions\/3489"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/pages\/2"}],"wp:attachment":[{"href":"https:\/\/www.sqlitetutorial.net\/wp-json\/wp\/v2\/media?parent=129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}