📖 Scenario: You work at a small 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: Create 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 three products with exact values: (1, 'Pen', 1.20), (2, 'Notebook', 2.50), (3, 'Eraser', 0.80).
Write a SQL query to select all columns from products.
Use ORDER BY price to sort the results by the price column in ascending order.
💡 Why This Matters
🌍 Real World
Sorting products by price helps customers find cheaper or more expensive items easily on online stores.
💼 Career
Knowing how to use ORDER BY is essential for database querying and reporting in many jobs like data analysis, backend development, and database administration.
Progress0 / 4 steps
1
Create the products table and insert data
Create a table called products with columns id (integer), name (text), and price (decimal). Then insert these exact rows: (1, 'Pen', 1.20), (2, 'Notebook', 2.50), and (3, 'Eraser', 0.80).
SQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add rows.
2
Write a basic SELECT query
Write a SQL query to select all columns from the products table using SELECT * FROM products.
SQL
Hint
Use SELECT * FROM products; to get all rows and columns.
3
Add ORDER BY clause to sort by price
Modify the SELECT query to add ORDER BY price so the products are sorted by the price column in ascending order.
SQL
Hint
Use ORDER BY price after the FROM clause to sort by price.
4
Complete the query with explicit ASC keyword
Add the keyword ASC after ORDER BY price to explicitly specify ascending order.
SQL
Hint
Use ORDER BY price ASC to sort ascending explicitly.
Practice
(1/5)
1. What does the SQL clause ORDER BY do when used with a single column?
easy
A. It deletes rows that have duplicate values in that column.
B. It filters the rows to only show those with unique values in that column.
C. It groups the rows by the values in that column.
D. It sorts the query results based on the values in that column.
Solution
Step 1: Understand the purpose of ORDER BY
The ORDER BY clause is used to arrange the rows returned by a query in a specific order based on one or more columns.
Step 2: Focus on single column sorting
When used with a single column, it sorts the results either ascending (default) or descending based on that column's values.
Final Answer:
It sorts the query results based on the values in that column. -> Option D
Quick Check:
ORDER BY = Sort results [OK]
Hint: ORDER BY arranges rows by column values [OK]
Common Mistakes:
Confusing ORDER BY with filtering or grouping
Thinking ORDER BY removes duplicates
Assuming ORDER BY changes data in the table
2. Which of the following is the correct syntax to sort the table Employees by the column LastName in ascending order?
easy
A. SELECT * FROM Employees ORDER BY LastName DESC;
B. SELECT * FROM Employees SORT BY LastName;
C. SELECT * FROM Employees ORDER BY LastName ASC;
D. SELECT * FROM Employees ORDER LastName;
Solution
Step 1: Recall correct ORDER BY syntax
The correct syntax to sort results is ORDER BY column_name. To specify ascending order, use ASC (which is also default).
Step 2: Check each option
SELECT * FROM Employees ORDER BY LastName ASC; uses correct syntax with ORDER BY LastName ASC. SELECT * FROM Employees SORT BY LastName; uses invalid keyword SORT BY. SELECT * FROM Employees ORDER LastName; misses BY. SELECT * FROM Employees ORDER BY LastName DESC; sorts descending, not ascending.
Final Answer:
SELECT * FROM Employees ORDER BY LastName ASC; -> Option C
Quick Check:
ORDER BY + column + ASC = correct syntax [OK]
Hint: Use ORDER BY column ASC for ascending sort [OK]
Common Mistakes:
Using SORT BY instead of ORDER BY
Omitting BY keyword
Confusing ASC with DESC
3. Given the table Products with columns ProductID and Price, what will be the order of ProductID after running this query?
SELECT ProductID FROM Products ORDER BY Price DESC;
Hint: Sort by column DESC to get highest values first [OK]
Common Mistakes:
Mixing ascending and descending order
Sorting by ProductID instead of Price
Assuming default ascending order
4. Identify the error in this SQL query:
SELECT Name FROM Customers ORDER LastName;
medium
A. Missing BY keyword after ORDER
B. ORDER BY cannot be used with SELECT
C. Name column does not exist
D. ORDER BY requires two columns
Solution
Step 1: Check ORDER BY syntax
The correct syntax requires the keyword BY after ORDER. The query uses ORDER LastName which is invalid.
Step 2: Verify other parts
The SELECT statement is valid, and Name is assumed to exist. ORDER BY can be used with SELECT. Only one column is needed for ORDER BY.
Final Answer:
Missing BY keyword after ORDER -> Option A
Quick Check:
ORDER BY requires BY keyword [OK]
Hint: ORDER BY must include BY keyword [OK]
Common Mistakes:
Omitting BY after ORDER
Thinking ORDER BY needs multiple columns
Assuming ORDER BY can't be used with SELECT
5. You have a table Students with columns StudentID, Name, and Score. You want to list all students ordered by their Score from highest to lowest, but if two students have the same score, order them alphabetically by Name. Which query correctly 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: Analyze each option
SELECT * FROM Students ORDER BY Score DESC, Name ASC; orders by Score DESC then Name ASC, matching requirements. 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, wrong directions. SELECT * FROM Students ORDER BY Score DESC; only sorts by Score DESC, no tie-breaker.
Final Answer:
SELECT * FROM Students ORDER BY Score DESC, Name ASC; -> Option A
Quick Check:
ORDER BY primary DESC, secondary ASC for tie-break [OK]
Hint: Use ORDER BY primary DESC, secondary ASC for tie-breaks [OK]
Common Mistakes:
Reversing sort order directions
Not adding secondary column for tie-break
Using only one column when two are needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.