I2 · Working with Data: NumPy, pandas, SQL and Visualisation

SQL Essentials

SQL is the standard language for working with relational databases, which store data in tables. SQLite is a small database that lives in one file and comes built into Python through the sqlite3 module.

15 min Intermediate

What you need: The earlier topics of this module.

Definition

SQL is the standard language for working with relational databases, which store data in tables. SQLite is a small database that lives in one file and comes built into Python through the sqlite3 module.

In simple words

A database is a set of very strict spreadsheets that many programs can use safely. SQL is how you ask it questions: show me all students with marks above 80, sorted highest first.

Why it matters

Most of the world's business data sits in SQL databases, so it's required for nearly every data job, and often the first round in interviews.

How it works

SQLDoes
CREATE TABLEMakes a table
INSERTAdds rows
SELECT ... WHEREReads matching rows
ORDER BYSorts
GROUP BYGroups for totals
JOINCombines tables
UPDATE, DELETEChanges or removes rows

In code

sqlite_demo.py
import sqlite3

db = sqlite3.connect(":memory:")
db.execute("CREATE TABLE students (name TEXT, city TEXT, mark INT)")
db.executemany("INSERT INTO students VALUES (?, ?, ?)", [
    ("Riya", "Pune", 91), ("Aman", "Delhi", 72), ("Sara", "Pune", 85),
])
db.execute("CREATE TABLE cities (city TEXT, state TEXT)")
db.execute("INSERT INTO cities VALUES ('Pune', 'Maharashtra'), ('Delhi', 'Delhi')")

print(db.execute("SELECT name FROM students WHERE mark > ? ORDER BY mark DESC", (80,)).fetchall())
print(db.execute("SELECT city, COUNT(*), AVG(mark) FROM students GROUP BY city").fetchall())
print(db.execute("""SELECT s.name, c.state FROM students s
                    JOIN cities c ON s.city = c.city""").fetchall())

Output

[('Riya',), ('Sara',)]
[('Delhi', 1, 72.0), ('Pune', 2, 88.0)]
[('Riya', 'Maharashtra'), ('Aman', 'Delhi'), ('Sara', 'Maharashtra')]

Common mistakes

  • Using f-strings to build SQL with user data, which opens the door to SQL injection.
  • Forgetting commit() on a real file database, so changes aren't saved.

Check yourself

Question 1

1. Which keyword filters rows?

Question 2

2. How should user input go into SQL?

Summary

  • SQL reads and changes tables.
  • SQLite comes built into Python.
  • Always use placeholders for user input.

Finished reading?

Sources: Python docs: sqlite3