0
0
MySQLquery~30 mins

ASC and DESC direction in MySQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Sorting Data with ASC and DESC in SQL
📖 Scenario: You are managing a small bookstore database. You want to organize the list of books by their prices to help customers find the cheapest or most expensive books easily.
🎯 Goal: Build SQL queries that sort the books by price in ascending and descending order using ASC and DESC.
📋 What You'll Learn
Create a table called books with columns id, title, and price.
Insert exactly three books with specified titles and prices.
Write a query to select all books sorted by price in ascending order using ASC.
Write a query to select all books sorted by price in descending order using DESC.
💡 Why This Matters
🌍 Real World
Sorting data is essential in databases to organize information for reports, user views, or analytics. For example, customers often want to see products from cheapest to most expensive.
💼 Career
Database developers and analysts frequently write queries with sorting to prepare data for applications, dashboards, or decision-making.
Progress0 / 4 steps
1
Create the books table and insert data
Create a table called books with columns id (integer), title (text), and price (decimal). Then insert these three books exactly: (1, 'The Alchemist', 10.99), (2, '1984', 8.99), and (3, 'Clean Code', 25.50).
MySQL
Need a hint?

Use CREATE TABLE to make the table and INSERT INTO to add the three books with exact values.

2
Set up the base SELECT query
Write a SQL query that selects all columns from the books table. Store this query in a variable called base_query as a string.
MySQL
Need a hint?

Use SET @base_query = 'SELECT * FROM books' to store the query string.

3
Write the ascending order query using ASC
Write a SQL query that selects all columns from books and sorts the results by price in ascending order using ORDER BY price ASC. Store this query in a variable called asc_query as a string.
MySQL
Need a hint?

Use ORDER BY price ASC to sort prices from lowest to highest.

4
Write the descending order query using DESC
Write a SQL query that selects all columns from books and sorts the results by price in descending order using ORDER BY price DESC. Store this query in a variable called desc_query as a string.
MySQL
Need a hint?

Use ORDER BY price DESC to sort prices from highest to lowest.