SQL practice exercise
Chapter 4: Practice Exercise
Client Requirements:
You’ve been approached by a client who runs a small chai store and wants to set up a simple database to manage their chai offerings. The client has provided the following requirements:
-
Database Setup:
- Create a new database named
chai_store_db
for managing chai products.
- Create a new database named
-
Chai Table:
-
Create a table called
chai_store
with the following columns:id
: A unique identifier for each chai (auto-incrementing).chai_name
: The name of the chai (e.g., “Masala Chai”, “Green Chai”).price
: The price of each chai.chai_type
: Type of chai (e.g., “Spiced”, “Herbal”, “Cold”).available
: Boolean value indicating if the chai is currently available or not.
-
-
Initial Data Insertion:
-
The client provides a list of their chai offerings and wants you to insert the following data:
- Masala Chai - ₹30 - Spiced - Available
- Green Chai - ₹25 - Herbal - Available
- Black Chai - ₹20 - Classic - Available
- Iced Chai - ₹35 - Cold - Not Available
- Oolong Chai - ₹40 - Specialty - Available
-
-
Data Queries:
-
The client needs some reports:
- Display all chai names and prices, using column aliases like “Chai Name” and “Cost in INR”.
- Find all chai varieties that have the word “Chai” in their name.
- List all chai varieties that cost less than ₹30.
- Show chai varieties sorted by price from highest to lowest.
-
-
Data Updates:
- The client wants to update the price of “Iced Chai” to ₹38 and mark it as available.
-
Data Deletions:
- The client decides to discontinue “Black Chai” and requests its removal from the database.
SQL solution:
- Create a new Database for the chai store:
- Create a new table for the chai store:
- Insert the initial data into the chai store table:
- Display all chai names and prices, using column aliases:
- Find all chai varieties that have the word “Chai” in their name:
- List all chai varieties that cost less than ₹30:
- Show chai varieties sorted by price from highest to lowest:
- Update the price of “Iced Chai” to ₹38 and mark it as available:
- Delete “Black Chai” from the database:
Additional Resources:
To delete entire table:
Also, you can use the TRUNCATE
command to delete all rows from a table:
Most of the time you will see that DROP
command is used with IF Exists
clause to avoid accidental errors:
Summary
In this chapter, we have learned about the basics of SQL and how to use it to manage and manipulate relational databases. We have also learned about the different types of relationships in SQL and how to use them. By the end of this chapter, you should have a good understanding of how to use SQL to manage and manipulate relational databases effectively.