Recall & Review
beginner
What does the
ORDER BY clause do in SQL?It sorts the rows returned by a query based on one or more columns, either in ascending (default) or descending order.
Click to reveal answer
beginner
How do you sort query results by multiple columns in SQL?
List the columns separated by commas in the
ORDER BY clause, like ORDER BY column1, column2. The sorting happens first by column1, then by column2 within the same values of column1.Click to reveal answer
beginner
What is the default sorting order when using
ORDER BY without specifying ASC or DESC?The default sorting order is ascending (
ASC), which means from smallest to largest or alphabetically A to Z.Click to reveal answer
intermediate
Write a query to sort employees first by department ascending, then by salary descending.
Example query: <br>
SELECT * FROM employees ORDER BY department ASC, salary DESC;Click to reveal answer
beginner
Why would you use multiple columns in an
ORDER BY clause?To organize data more precisely. For example, sorting by last name and then by first name helps list people alphabetically by full name.
Click to reveal answer
What happens if you write
ORDER BY age, name in a query?✗ Incorrect
The
ORDER BY clause sorts by the first column listed (age), then by the second (name) for rows with the same age.Which keyword sorts data in descending order?
✗ Incorrect
DESC sorts data from largest to smallest or Z to A.If you want to sort by two columns, which is the correct syntax?
✗ Incorrect
Use commas to separate columns in
ORDER BY to sort by multiple columns.What is the default sort order if none is specified?
✗ Incorrect
The default sort order is ascending (
ASC).Which of these queries sorts employees by salary descending and then by name ascending?
✗ Incorrect
The first column sorts by salary descending, the second by name ascending.
Explain how to use the
ORDER BY clause to sort data by multiple columns.Think about sorting a list by last name, then first name.
You got /4 concepts.
Describe a real-life example where sorting by multiple columns is useful.
Imagine organizing a phone book or a store inventory.
You got /3 concepts.