📖 Scenario: You work at an online store. You have a table of products with their prices. You want to see the products sorted by their price from lowest to highest.
🎯 Goal: Build a SQL query that selects all products and sorts them by the price column in ascending order using ORDER BY.
📋 What You'll Learn
Create a table called products with columns id, name, and price.
Insert exactly three products with given names and prices.
Write a query to select all columns from products.
Use ORDER BY price to sort the results by price ascending.
💡 Why This Matters
🌍 Real World
Sorting products by price is common in online stores to help customers find affordable or premium items easily.
💼 Career
Knowing how to use ORDER BY is essential for database querying jobs, data analysis, and backend development.
Progress0 / 4 steps
1
Create the products table and insert data
Write SQL statements to create a table called products with columns id (integer), name (text), and price (decimal). Then insert these three products exactly: (1, 'Pen', 1.20), (2, 'Notebook', 2.50), and (3, 'Pencil', 0.80).
MySQL
Hint
Use CREATE TABLE to make the table. Use INSERT INTO with multiple rows to add the products.
2
Write a basic SELECT query
Write a SQL query to select all columns from the products table. Use SELECT * FROM products.
MySQL
Hint
Use SELECT * FROM products; to get all rows and columns.
3
Add ORDER BY to sort by price ascending
Modify the SELECT query to add ORDER BY price so the products are sorted by the price column in ascending order.
MySQL
Hint
Add ORDER BY price after the FROM clause to sort by price ascending.
4
Complete the query with explicit ASC keyword
Add the keyword ASC after ORDER BY price to explicitly specify ascending order.
MySQL
Hint
Write ORDER BY price ASC to show ascending order explicitly.
Practice
(1/5)
1. What does the ORDER BY clause do in a MySQL query?
easy
A. It sorts the query results by one or more columns.
B. It filters rows based on a condition.
C. It groups rows with the same values.
D. It limits the number of rows returned.
Solution
Step 1: Understand the purpose of ORDER BY
The ORDER BY clause is used to sort the results of a query based on one or more columns.
Step 2: Compare with other clauses
Filtering rows is done by WHERE, grouping by GROUP BY, and limiting rows by LIMIT. So, only sorting matches ORDER BY.
Final Answer:
It sorts the query results by one or more columns. -> Option A
Quick Check:
ORDER BY = Sort results [OK]
Hint: ORDER BY sorts rows; WHERE filters rows [OK]
Common Mistakes:
Confusing ORDER BY with WHERE clause
Thinking ORDER BY filters rows
Mixing ORDER BY with GROUP BY
2. Which of the following is the correct syntax to sort the table employees by the column salary in descending order?
easy
A. SELECT * FROM employees ORDER BY salary DOWN;
B. SELECT * FROM employees SORT BY salary DESC;
C. SELECT * FROM employees ORDER salary DESC;
D. SELECT * FROM employees ORDER BY salary DESC;
Solution
Step 1: Recall correct ORDER BY syntax
The correct syntax to sort results is ORDER BY column_name ASC|DESC. Here, DESC means descending order.
Step 2: Check each option
SELECT * FROM employees ORDER BY salary DESC; uses correct syntax. Options B, C, and D use invalid keywords or syntax.
Final Answer:
SELECT * FROM employees ORDER BY salary DESC; -> Option D
Quick Check:
ORDER BY + DESC = correct syntax [OK]
Hint: Use ORDER BY column DESC for descending sort [OK]
Common Mistakes:
Using SORT BY instead of ORDER BY
Omitting BY after ORDER
Using DOWN instead of DESC
3. Given the table products with columns id and price, what will be the order of id after running this query?
The query sorts rows by price in ascending order (lowest to highest).
Step 2: Sort the data by price
Prices sorted ascending: 20 (id 2), 30 (id 3), 50 (id 1). So the id order is [2, 3, 1].
Final Answer:
[2, 3, 1] -> Option B
Quick Check:
Sort by price ASC = [2, 3, 1] [OK]
Hint: Sort by column values ascending to find order [OK]
Common Mistakes:
Confusing ASC with DESC order
Sorting by id instead of price
Ignoring the ORDER BY clause
4. Identify the error in this query:
SELECT name FROM customers ORDER salary DESC;
medium
A. ORDER BY must come before SELECT
B. DESC cannot be used with ORDER
C. Missing BY keyword after ORDER
D. Missing WHERE clause
Solution
Step 1: Check ORDER BY syntax
The correct syntax requires the keyword BY after ORDER. The query uses ORDER salary DESC which is invalid.
Step 2: Confirm other parts are correct
DESC is valid with ORDER BY. No WHERE clause is needed for sorting. ORDER BY comes after SELECT.
Final Answer:
Missing BY keyword after ORDER -> Option C
Quick Check:
ORDER BY needs BY keyword [OK]
Hint: Always write ORDER BY, not just ORDER [OK]
Common Mistakes:
Omitting BY after ORDER
Using ORDER before SELECT
Confusing DESC usage
5. You have a table students with columns name and score. You want to list all students sorted by their score from highest to lowest, but if two students have the same score, sort those students alphabetically by name ascending. Which query achieves this?
hard
A. SELECT * FROM students ORDER BY score DESC, name ASC;
B. SELECT * FROM students ORDER BY name ASC, score DESC;
C. SELECT * FROM students ORDER BY score ASC, name DESC;
D. SELECT * FROM students ORDER BY score DESC;
Solution
Step 1: Understand sorting requirements
Primary sort is by score descending (highest first). Secondary sort for ties is by name ascending (alphabetical).
Step 2: Match query with requirements
SELECT * FROM students ORDER BY score DESC, name ASC; orders by score DESC then name ASC, matching the requirement. SELECT * FROM students ORDER BY name ASC, score DESC; reverses order priorities. SELECT * FROM students ORDER BY score ASC, name DESC; sorts score ascending and name descending, which is wrong. SELECT * FROM students ORDER BY score DESC; misses secondary sort.
Final Answer:
SELECT * FROM students ORDER BY score DESC, name ASC; -> Option A
Quick Check:
ORDER BY score DESC, name ASC = correct [OK]
Hint: Use multiple columns in ORDER BY for tie-breakers [OK]
Common Mistakes:
Reversing sort order priorities
Missing secondary sort column
Using ASC when DESC needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.