Learning to Sort and Limit Results Using SQL (ORDER BY, LIMIT)

Details
Title | Learning to Sort and Limit Results Using SQL (ORDER BY, LIMIT) |
Author | Database Dive |
Duration | 4:22 |
File Format | MP3 / MP4 |
Original URL | https://youtube.com/watch?v=qB7po70KR2Y |
Description
This SQL course will help you understand how the SQL ORDER BY and SQL LIMIT SQL queries work.
This full SQL course will tell you that to sort data, we use the ORDER BY clause because it helps us sort data in a specific way after we instruct the database.
This SQL course full will tell you that with an SQL query like so:
SELECT * FROM `employees` WHERE `department` = 'Marketing' ORDER BY `id` DESC;
Our MySQL database management system will return every employee in the marketing department ordered by an ID in a descending manner.
This SQL course with MySQL will also tell you that if we want to limit the amount of returned data, we use the LIMIT SQL clause like so:
SELECT * FROM `employees` LIMIT 5;
This SQL query will return the same result set but will stop at the fifth result from the top.
From this SQL course, you will also know that the LIMIT SQL clause is very useful for database pagination applications.
Imagine we have a page where we only want to see the first 5 results on every page. This is easy to do by using the LIMIT clause like so:
SELECT * FROM `employees` LIMIT 0,5;
Note the "0" before 5: this tells our database that we want to start counting from the first row (in this case, databases consider "0" to be equal to "1" in human speech.)
Then, once we want to turn to the second page, we may start from the fifth row with our SQL query looking like so:
SELECT * FROM `employees` LIMIT 5,5;
The third page may have five results starting from the tenth result by us issuing an SQL clause like LIMIT 10,5, etc.
Finally, this SQL course will tell you that in the real world, ORDER BY and LIMIT are often combined. If we issue an SQL query like so (we use MySQL Server for this example):
SELECT `id`, `employee`, `salary` FROM `employees` ORDER BY `salary` DESC LIMIT 5;
Our relational database (MySQL Server) will return 3 columns (id, employee, salary) from the employees table sorting them (ORDER BY SQL clause) by salary in a descending manner and limiting itself to the first 5 rows (LIMIT 5.)
This SQL tutorial will also tell you that we can order data by multiple columns by using ORDER BY on multiple columns one after another:
SELECT `id`, `employee`, `salary` FROM `employees` ORDER BY `id` DESC, `salary` DESC LIMIT 5;
If you've enjoyed this SQL course, make sure to subscribe to this YouTube channel for more full SQL course videos, SQL tutorials on SQL indexes, database performance, database security, and other things concerning database management systems!
Music:
Balearic Love by tubebackr: https://soundcloud.com/tubebackr
License: Creative Commons. Attribution-NoDerivs 3.0 Unported. CC BY-ND 3.0
Free Download / Stream: https://www.audiolibrary.com.co/tubebackr/balearic-love
Music promoted by Audio Library: https://www.youtube.com/watch?v=-kEFWcvXsZA
#database #sql #developer #programming #webdevelopment