0
0
MySQLquery~30 mins

DISTINCT for unique values in MySQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Using DISTINCT to Find Unique Values in MySQL
📖 Scenario: You work at a small bookstore that keeps track of all book sales in a database. Sometimes, you want to find out which unique book titles have been sold, without counting duplicates.
🎯 Goal: Build a simple MySQL query that uses DISTINCT to list unique book titles sold from the sales table.
📋 What You'll Learn
Create a table called sales with columns id (integer), title (text), and price (decimal).
Insert the exact sales data with duplicate book titles as specified.
Write a query using DISTINCT to select unique book titles from the sales table.
Complete the final query to return only the unique titles.
💡 Why This Matters
🌍 Real World
Bookstores and many businesses use DISTINCT to find unique items sold, unique customers, or unique product categories from their sales data.
💼 Career
Knowing how to use DISTINCT is essential for data analysts and database administrators to clean data and generate accurate reports.
Progress0 / 4 steps
1
Create the sales table and insert data
Create a table called sales with columns id as INT, title as VARCHAR(100), and price as DECIMAL(5,2). Then insert these rows exactly: (1, 'The Great Gatsby', 10.99), (2, '1984', 8.99), (3, 'The Great Gatsby', 10.99), (4, 'To Kill a Mockingbird', 7.99), (5, '1984', 8.99).
MySQL
Need a hint?

Use CREATE TABLE to define the table and INSERT INTO to add rows.

2
Set up the SELECT statement to get book titles
Write a SELECT statement that selects the title column from the sales table. Assign this query to a variable called query.
MySQL
Need a hint?

Use SET @query = 'SELECT title FROM sales' to store the query as a string.

3
Modify the SELECT statement to use DISTINCT
Change the @query variable to select only unique title values using DISTINCT from the sales table.
MySQL
Need a hint?

Add the keyword DISTINCT right after SELECT to get unique titles.

4
Complete the query to return unique book titles
Finalize the query by writing a SELECT statement that returns unique title values from the sales table using DISTINCT. This is the final query you will run.
MySQL
Need a hint?

Use SELECT DISTINCT title FROM sales; to get the unique book titles.