Skip to content

Day 45: Deleting data from tables

Utilizing the DELETE Statement in SQL: Removing Data from Your Database

At some point, you may need to remove data from your database. For instance, if you have a book in your bookstore that you no longer want to sell, you can delete it from your database. In this context, we use the DELETE keyword to remove data from the books table where the title of the book is 'The Great Gatsby'.

Deleting Data with SQL

Here's how you can delete a book titled 'The Great Gatsby' from the books table using SQL and Python:

import sqlite3

# Connect to the bookstore.db database
conn = sqlite3.connect("bookstore.db")

# Create a cursor to execute SQL commands
cursor = conn.cursor()

# Delete a book from the table
cursor.execute("""
DELETE FROM books
WHERE title = 'The Great Gatsby'
""")

# Close the database connection
conn.close()

In this Python script, we connect to the bookstore.db database and create a cursor to execute SQL commands. We then use the DELETE statement to remove the book titled 'The Great Gatsby' from the books table. Finally, we close the database connection.

By using the DELETE statement, we can efficiently remove data from our SQL database, enhancing our data management capabilities and improving the readability of our code.

Learn More

Want to learn more about SQL for Machine Learning? Check out the full course HERE.


Need help mastering Machine Learning?

Don't just follow along — join me! Get exclusive access to me, your instructor, who can help answer any of your questions. Additionally, get access to a private learning group where you can learn together and support each other on your AI journey.