Introduction
We use ORDER BY to sort data in a table. ASC sorts from smallest to largest, DESC sorts from largest to smallest.
We use ORDER BY to sort data in a table. ASC sorts from smallest to largest, DESC sorts from largest to smallest.
SELECT column1, column2 FROM table_name ORDER BY column1 ASC | DESC;
ASC means ascending order (smallest to largest).
DESC means descending order (largest to smallest).
SELECT name, age FROM people ORDER BY age ASC;
SELECT product, price FROM products ORDER BY price DESC;
SELECT city, population FROM cities ORDER BY city ASC;
This creates a table of employees and sorts them by salary from highest to lowest.
CREATE TABLE employees (id INT, name VARCHAR(20), salary INT); INSERT INTO employees VALUES (1, 'Alice', 5000), (2, 'Bob', 7000), (3, 'Charlie', 6000); SELECT name, salary FROM employees ORDER BY salary DESC;
If you do not specify ASC or DESC, ASC is the default.
You can order by multiple columns by separating them with commas.
ORDER BY sorts query results.
Use ASC for ascending order, DESC for descending order.
Sorting helps you organize data to find what you need quickly.
ORDER BY clause do in an SQL query?ORDER BY clause is used to arrange the rows returned by a query in a specific order.WHERE, grouping by GROUP BY, and limiting rows by LIMIT. Only ORDER BY sorts the results.Employees by the column Salary in descending order?ORDER BY column_name ASC|DESC. Here, DESC means descending order.SORT BY, missing BY, or incorrect direction word.Products with columns ProductID and Price, what will be the order of ProductID after running this query?SELECT ProductID FROM Products ORDER BY Price ASC;
Price in ascending order, meaning from smallest to largest price.ProductID but orders by Price. So the ProductID values will appear sorted according to their prices ascending.SELECT Name FROM Customers ORDER BY Age DESCENDING;
ASC for ascending and DESC for descending order. DESCENDING is not a valid keyword.Age exists as no schema given.Students with columns Name, Grade, and Age. You want to list students sorted first by Grade descending (highest grade first), and then by Age ascending (youngest first) within the same grade. Which query achieves this?Grade descending first, so highest grades come first.Age ascending, so younger students appear first.ORDER BY Grade DESC, Age ASC. Other options reverse order or directions.Pass the test and this pattern moves unknown