Introduction
When you ask a database a question, it needs to figure out the best way to find the answer quickly and correctly. This process involves several steps that turn your question into a plan the database can follow to get the data you want.
Imagine ordering a meal at a restaurant. First, the waiter listens carefully to your order to understand it. Then, the chef plans the best way to prepare your meal quickly. Next, the kitchen cooks the food following the plan. Finally, the waiter brings the meal to your table nicely presented.
┌───────────────┐
│ Parsing and │
│ Translation │
└──────┬────────┘
│
▼
┌───────────────┐
│ Query │
│ Optimization │
└──────┬────────┘
│
▼
┌───────────────┐
│ Query │
│ Execution │
└──────┬────────┘
│
▼
┌───────────────┐
│ Result │
│ Formatting & │
│ Return │
└───────────────┘import sqlite3 conn = sqlite3.connect(':memory:') cur = conn.cursor() # Create a sample table cur.execute('CREATE TABLE students (id INTEGER, name TEXT, age INTEGER)') cur.execute("INSERT INTO students VALUES (1, 'Alice', 21)") cur.execute("INSERT INTO students VALUES (2, 'Bob', 22)") # Sample query query = 'SELECT name FROM students WHERE age > 20' # Execute query cur.execute(query) results = cur.fetchall() for row in results: print(row[0])