ORDER BY

This lesson discusses how to use the ORDER BY clause.

We'll cover the following...

ORDER BY Clause

In the previous couple of sections, we have seen how to query data from a table. The retrieved rows aren’t printed in any particular order. The ORDER BY clause allows us to print the retrieved rows in an orderly fashion based on the criteria we specify.

Example Syntax

SELECT col1, col2, … coln

FROM table

WHERE col3 LIKE "%some-string%"

ORDER BY col3

Connect to the terminal below by clicking in the widget. Once connected, the command line prompt will show up. Enter or copy and paste the command ./DataJek/Lessons/11lesson.sh and wait for the mysql prompt to start-up.

Press + to interact
-- The lesson queries are reproduced below for convenient copy/paste into the terminal.
-- Query 1
SELECT * FROM Actors ORDER BY FirstName;
-- Query 2
SELECT * FROM Actors ORDER BY FirstName DESC;
-- Query 3
SELECT * FROM Actors ORDER BY NetWorthInMillions, FirstName;
-- Query 4
SELECT * FROM Actors ORDER BY NetWorthInMillions, SecondName;
-- Query 5
SELECT * FROM Actors ORDER BY NetWorthInMillions DESC, FirstName ASC;
-- Query 6
SELECT * FROM Actors ORDER BY NetWorthInMillions DESC, FirstName DESC;
-- Query 7
SELECT * FROM Actors ORDER BY BINARY FirstName;
-- Query 8
SELECT * FROM Actors ORDER BY NetWorthInMillions;
-- Query 9
SELECT * FROM Actors ORDER BY CAST(NetWorthInMillions AS CHAR);
Terminal 1
Terminal
Loading...
  1. Suppose we want to print the names of all the actors sorted in alphabetical order. We can do so using the ...

Access this course and 1400+ top-rated courses and projects.