
Python
Learn the core SQL concepts you need to work with databases effectively.
SQL (Structured Query Language) is the standard language for working with databases. It lets you:
import sqlite3
conn = sqlite3.connect("app.db")
cursor = conn.cursor()
# SQL is database-agnostic (works with SQLite, MySQL, PostgreSQL, etc.)
cursor.execute("SELECT * FROM users")
results = cursor.fetchall()
conn.close()cursor.execute("""
INSERT INTO users (name, age, email)
VALUES ('Alice', 30, 'alice@example.com')
""")
conn.commit()cursor.execute("SELECT name, email FROM users WHERE age > 25")
for row in cursor.fetchall():
print(row)cursor.execute("""
UPDATE users
SET age = 31
WHERE name = 'Alice'
""")
conn.commit()cursor.execute("DELETE FROM users WHERE age < 18")
conn.commit()# Find all users over 25
cursor.execute("SELECT * FROM users WHERE age > 25")
# Find a specific user by name
cursor.execute("SELECT * FROM users WHERE name = 'Alice'")
# Multiple conditions
cursor.execute("""
SELECT * FROM users
WHERE age > 25 AND city = 'New York'
""")# Sort results by age (ascending)
cursor.execute("SELECT * FROM users ORDER BY age ASC")
# Sort by age descending
cursor.execute("SELECT * FROM users ORDER BY age DESC")# Get first 10 users
cursor.execute("SELECT * FROM users LIMIT 10")
# Skip first 5, get next 10 (pagination)
cursor.execute("SELECT * FROM users LIMIT 10 OFFSET 5")| Keyword | Purpose |
|---|---|
| SELECT | Retrieve data |
| FROM | Specify which table |
| WHERE | Filter results |
| ORDER BY | Sort results |
| LIMIT | Limit number of results |
| INSERT | Add new row |
| UPDATE | Modify existing row |
| DELETE | Remove row |
| AND, OR | Combine conditions |
| LIKE | Pattern matching |
ā Forgetting to commit changes
cursor.execute("INSERT INTO users VALUES ...")
# Changes are lost unless you conn.commit()!ā Always commit after modifications
cursor.execute("INSERT INTO users VALUES ...")
conn.commit() # Now the change is permanentā Using untyped comparisons
cursor.execute("SELECT * FROM users WHERE age = '30'") # Wrong!ā Type-safe queries
cursor.execute("SELECT * FROM users WHERE age = 30") # CorrectReady to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep ⢠Build Real ⢠Verify Skills ⢠Launch Forward