Sorting data is essential for organizing and analyzing query results. In this lab, you will practice sorting data using the ORDER BY
clause in MySQL. You will explore sorting by single and multiple columns, using ascending and descending order, handling NULL
values, and combining sorting with the LIMIT
clause. This lab provides hands-on experience with sorting data effectively from the command line.
By the end of this lab, you will be able to:
NULL
values during sorting.LIMIT
clause to retrieve a specific number of rows.Open your terminal or command prompt.
Log in to the MySQL server:
mysql -u root -p
root
with your username if necessary.Use the lab_database
(or create it if it doesn’t exist):
CREATE DATABASE IF NOT EXISTS lab_database;
USE lab_database;
Create a table named products
with the following structure:
id
: Integer, auto-increment, primary key.name
: Text, stores the product name.category
: Text, stores the product category.price
: Decimal(10, 2), stores the product price.stock
: Integer, stores the number of items in stock.CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name TEXT NOT NULL,
category TEXT NOT NULL,
price DECIMAL(10, 2) NOT NULL,
stock INT
);
Verify the table structure:
DESCRIBE products;
Insert the following data into the products
table:
INSERT INTO products (name, category, price, stock) VALUES
('Laptop', 'Electronics', 1200.00, 10),
('Desk Chair', 'Furniture', 150.00, 15),
('Notebook', 'Stationery', 5.00, 50),
('Coffee Maker', 'Home Appliances', 80.00, 20),
('Headphones', 'Electronics', 200.00, 30),
('Tablet', 'Electronics', 300.00, NULL);
Verify the data:
SELECT * FROM products;
Task 1: Sort by a Single Column
Sort products by price in ascending order:
SELECT name, price
FROM products
ORDER BY price ASC;
Sort products by price in descending order:
SELECT name, price
FROM products
ORDER BY price DESC;
Task 2: Sort by Multiple Columns
SELECT name, category, price
FROM products
ORDER BY category ASC, price DESC;
Task 3: Handle NULL
Values
Sort by stock in ascending order (note that NULL
values appear first by default):
SELECT name, stock
FROM products
ORDER BY stock ASC;
Sort by stock in descending order (note that NULL
values appear last by default):
SELECT name, stock
FROM products
ORDER BY stock DESC;
Explicitly place NULL
values at the end in ascending order:
SELECT name, stock
FROM products
ORDER BY stock IS NULL ASC, stock ASC;
Task 4: Combine Sorting with Filtering
SELECT name, price
FROM products
WHERE category = 'Electronics'
ORDER BY price DESC;
Task 5: Limit the Number of Rows
Retrieve the top 3 most expensive products:
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;
Retrieve the bottom 2 cheapest products:
SELECT name, price
FROM products
ORDER BY price ASC
LIMIT 2;
Task 1: Sort by Expressions
price * stock
) in descending order:SELECT name, price, stock, price * stock AS total_value
FROM products
ORDER BY total_value DESC;
Task 2: Sort by Column Aliases
SELECT name, price, price * 0.9 AS discounted_price
FROM products
ORDER BY discounted_price ASC;
Exit the MySQL command line:
EXIT;
Close your terminal or command prompt.
In this lab, you practiced sorting data in MySQL using the ORDER BY
clause. You learned how to:
NULL
values explicitly in sorting.Sorting data is a critical skill for presenting query results in a clear and organized manner. In the next lab, you will explore grouping data to summarize and analyze information efficiently.