In this lab, you will practice filtering data in a MySQL database using the WHERE
clause and related operators. You will learn how to filter data with comparison operators, logical operators, pattern matching, and ranges. By the end of this lab, you will have a solid foundation in querying and filtering data to retrieve precise results.
By the end of this lab, you will be able to:
WHERE
clause to filter rows based on conditions.LIKE
operator.IN
, BETWEEN
, and NULL
operators.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 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 NOT NULL
);
Verify the table structure:
DESCRIBE products;
Insert the following sample 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);
Verify the data by querying the table:
SELECT * FROM products;
Task 1: Filter by a Single Condition
SELECT * FROM products
WHERE category = 'Electronics';
Task 2: Filter by Multiple Conditions
SELECT * FROM products
WHERE category = 'Furniture' AND price > 100;
Task 3: Filter Using Comparison Operators
SELECT name, stock
FROM products
WHERE stock < 20;
Task 4: Filter with OR
SELECT * FROM products
WHERE category = 'Stationery' OR category = 'Home Appliances';
Task 1: Match Names Starting with "C"
SELECT * FROM products
WHERE name LIKE 'C%';
Task 2: Match Names Ending with "e"
SELECT * FROM products
WHERE name LIKE '%e';
Task 3: Match Names Containing "top"
SELECT * FROM products
WHERE name LIKE '%top%';
Task 1: Filter by Multiple Categories
SELECT * FROM products
WHERE category IN ('Electronics', 'Furniture');
Task 1: Filter by Price Range
SELECT * FROM products
WHERE price BETWEEN 50 AND 200;
Task 2: Filter by Stock Range
SELECT name, stock
FROM products
WHERE stock BETWEEN 10 AND 30;
Task 1: Handle NULL Values
Add a product with a NULL value for stock:
INSERT INTO products (name, category, price, stock)
VALUES ('Tablet', 'Electronics', 300.00, NULL);
Retrieve products with NULL stock:
SELECT * FROM products
WHERE stock IS NULL;
Retrieve products with non-NULL stock:
SELECT * FROM products
WHERE stock IS NOT NULL;
Task 1: Retrieve Electronics Under Specific Conditions
SELECT * FROM products
WHERE category = 'Electronics' AND price > 100 AND stock > 5;
Task 2: Retrieve Top Priced Items
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 3;
Exit the MySQL command line:
EXIT;
Close your terminal or command prompt.
In this lab, you practiced filtering data in MySQL using the WHERE
clause and related operators. You learned how to:
LIKE
.IN
and BETWEEN
operators for efficient filtering.NULL
values in queries.These skills are essential for querying large datasets and retrieving only the information you need. In the next lab, we will explore sorting and grouping data for better organization and analysis.