LIMIT, ORDER BY
LIMIT
The LIMIT statement is useful when you want to see just the first few rows of a table. This can be much faster for loading than if we load the entire dataset.
The LIMIT command is always the very last part of a query. An example of showing just the first 10 rows of the orders table with all of the columns might look like the following:
SELECT * FROM orders LIMIT 10;
We could also change the number of rows by changing the 10 to any other number of rows.
ORDER BY
The ORDER BY statement allows us to sort our results using the data in any column. If you are familiar with Excel or Google Sheets, using ORDER BY is similar to sorting a sheet using a column. A key difference, however, is that using ORDER BY in a SQL query only has temporary effects, for the results of that query, unlike sorting a sheet by column in Excel or Sheets.
The ORDER BY statement always comes in a query after the SELECT and FROM statements, but before the LIMIT statement. If you are using the LIMIT statement, it will always appear last. As you learn additional commands, the order of these statements will matter more.
Solutions to previous ORDER BY questions
- Write a query to return the 10 earliest orders in the orders table. Include the
id
,occurred_at
, andtotal_amt_usd
.
SELECT id, occurred_at, total_amt_usd FROM orders ORDER BY occurred_at LIMIT 10;
- Write a query to return the top 5 orders in terms of largest
total_amt_usd
. Include theid
,account_id
, andtotal_amt_usd
.
SELECT id, account_id, total_amt_usd FROM orders ORDER BY total_amt_usd DESC LIMIT 5;
- Write a query to return the lowest 20 orders in terms of smallest
total_amt_usd
. Include theid
,account_id
, andtotal_amt_usd
.
SELECT id, account_id, total_amt_usd FROM orders ORDER BY total_amt_usd LIMIT 20;
When you provide a list of columns in an ORDER BY command, the sorting occurs using the leftmost column in your list first, then the next column from the left, and so on. We still have the ability to flip the way we order using DESC.
- Write a query that displays the order ID, account ID, and total dollar amount for all the orders, sorted first by the account ID (in ascending order), and then by the total dollar amount (in descending order).
SELECT id, account_id, total_amt_usd FROM orders ORDER BY account_id, total_amt_usd DESC;
- Now write a query that again displays order ID, account ID, and total dollar amount for each order, but this time sorted first by total dollar amount (in descending order), and then by account ID (in ascending order).
SELECT id, account_id, total_amt_usd FROM orders ORDER BY total_amt_usd DESC, account_id;
- Compare the results of these two queries above. How are the results different when you switch the column you sort on first?
Comments
Post a Comment