W3. SQL Order By Flashcards
Q: What is the purpose of the SQL ORDER BY keyword?
A: To sort the result set in ascending or descending order.
Q: Write a SQL query to sort products by price in ascending order.
SELECT * FROM Products ORDER BY Price;
Q: What is the default order for sorting when using ORDER BY?
A: Ascending order.
Q: How do you modify ORDER BY to sort results in descending order?
A: Add the DESC keyword after the column name.
Q: Write a query to sort products from highest to lowest price.
SELECT * FROM Products ORDER BY Price DESC;
Q: How does ORDER BY sort string values?
A: Alphabetically by default.
Q: Write a query to sort products alphabetically by ProductName.
SELECT * FROM Products
ORDER BY ProductName;
Q: How do you sort a column in reverse alphabetical order?
A: Use the DESC keyword after the column name.
Q: Write a query to sort products by ProductName in reverse alphabetical order.
SELECT * FROM Products ORDER BY ProductName DESC;
Q: How can you sort by multiple columns in SQL?
A: List the columns in ORDER BY, separated by commas.
Q: Write a query to sort customers by Country and then by CustomerName.
SELECT * FROM Customers
ORDER BY Country, CustomerName;
Q: How do you specify different sort orders for multiple columns?
A: Use ASC or DESC for each column in the ORDER BY clause.
Q: Write a query to sort customers by Country in ascending order and CustomerName in descending order.
SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;