In this lab, you will learn how to use the SELECT
statement to retrieve data from a MySQL database. This lab provides hands-on practice with various features of the SELECT
statement, including filtering data with WHERE
, sorting results using ORDER BY
, limiting rows with LIMIT
, and performing basic aggregation with functions like COUNT
and AVG
.
By the end of this lab, you will be able to:
SELECT
statement to query data from a table.WHERE
clause.ORDER BY
clause.LIMIT
.COUNT
, SUM
, and AVG
.Open your terminal or command prompt.
Log in to the MySQL server with the following command:
mysql -u root -p
root
with your MySQL username if necessary.Use the lab_database
(or create it if it does not 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 NOT NULL
);
Verify the table creation:
DESCRIBE products;
Insert the following sample data into the products
table:
INSERT INTO products (name, category, price, stock) VALUES
('Laptop', 'Electronics', 1200.00, 10),
('Headphones', 'Electronics', 200.00, 30),
('Coffee Maker', 'Home Appliances', 80.00, 20),
('Desk Chair', 'Furniture', 150.00, 15),
('Notebook', 'Stationery', 5.00, 50);
Confirm the data insertion by querying all rows:
SELECT * FROM products;
Task 1: Retrieve All Data
SELECT * FROM products;
products
table.Task 2: Retrieve Specific Columns
SELECT name, price FROM products;
name
and price
columns.Task 3: Filter Data with WHERE
SELECT name, price FROM products WHERE category = 'Electronics';
Task 4: Filter Data with Multiple Conditions
SELECT name, price FROM products WHERE price > 100 AND stock < 20;
Task 1: Sort by Price in Ascending Order
SELECT name, price FROM products ORDER BY price ASC;
Task 2: Sort by Price in Descending Order
SELECT name, price FROM products ORDER BY price DESC;
Task 3: Limit the Number of Rows
SELECT name, price FROM products LIMIT 3;
Task 1: Count the Number of Products
SELECT COUNT(*) AS total_products FROM products;
Task 2: Calculate the Average Price of Products
SELECT AVG(price) AS average_price FROM products;
Task 3: Calculate the Total Stock
SELECT SUM(stock) AS total_stock FROM products;
Task 4: Find the Maximum and Minimum Prices
SELECT MAX(price) AS highest_price, MIN(price) AS lowest_price FROM products;
Exit the MySQL command line:
EXIT;
Close your terminal or command prompt.
In this lab, you practiced using the SELECT
statement in MySQL to query data from a table. You learned how to:
WHERE
clause.ORDER BY
.LIMIT
.COUNT
, SUM
, and AVG
.These skills form the foundation for interacting with relational databases and are essential for writing effective SQL queries. In the next lab, we will explore how to filter, sort, and join data across multiple tables.