diff --git a/tech_docs/database/sql_guide.md b/tech_docs/database/sql_guide.md new file mode 100644 index 0000000..ca1fdde --- /dev/null +++ b/tech_docs/database/sql_guide.md @@ -0,0 +1,231 @@ +# Complete Guide to Getting Started with SQL and SQLite3 + +## Introduction to SQL + +SQL (Structured Query Language) is the standard language for managing and manipulating relational databases. It is essential for anyone working in data-related fields. This guide will cover the basics of SQL and provide a comprehensive introduction to using SQLite3, a lightweight database engine. + +### What You Need to Know About SQL + +#### 1. Basic Concepts +- **Relational Databases**: Understand what a relational database is and how data is organized into tables (relations). +- **Tables and Schemas**: Know how to define and understand the schema of a database, including tables, columns, and data types. + +#### 2. SQL Syntax and Commands +- **Data Definition Language (DDL)**: Commands used to define the database structure: + - `CREATE`: Create tables and databases. + - `ALTER`: Modify existing database objects. + - `DROP`: Delete tables or databases. +- **Data Manipulation Language (DML)**: Commands for data manipulation: + - `SELECT`: Retrieve data from the database. + - `INSERT`: Add new data to the database. + - `UPDATE`: Modify existing data. + - `DELETE`: Remove data from the database. +- **Data Control Language (DCL)**: Commands for controlling access to data: + - `GRANT`: Give user access privileges. + - `REVOKE`: Remove user access privileges. + +#### 3. Querying Data +- **Basic Queries**: Writing simple queries to retrieve data using `SELECT` statements. +- **Filtering Data**: Using `WHERE` clauses to filter data. +- **Sorting Data**: Using `ORDER BY` to sort data. +- **Aggregate Functions**: Using functions like `COUNT()`, `SUM()`, `AVG()`, `MIN()`, and `MAX()` to perform calculations on data. + +#### 4. Advanced Querying +- **Joins**: Combining data from multiple tables using various types of joins (`INNER JOIN`, `LEFT JOIN`, `RIGHT JOIN`, `FULL JOIN`). +- **Subqueries**: Writing queries within queries to perform complex operations. +- **Grouping and Aggregation**: Using `GROUP BY` to group data and aggregate functions to summarize data. +- **Window Functions**: Performing calculations across a set of table rows related to the current row. + +#### 5. Database Design +- **Normalization**: Understanding normalization rules to design efficient and consistent databases. +- **Indexes**: Creating and using indexes to improve query performance. +- **Constraints**: Applying constraints like `PRIMARY KEY`, `FOREIGN KEY`, `UNIQUE`, `NOT NULL`, and `CHECK` to maintain data integrity. + +#### 6. Performance Tuning +- **Query Optimization**: Techniques for writing efficient queries and understanding execution plans. +- **Indexes and Partitions**: Using indexes and partitions to optimize database performance. +- **Database Maintenance**: Regular tasks like backups, indexing, and updating statistics to keep the database running smoothly. + +#### 7. SQL in Practice +- **Real-World Scenarios**: Applying SQL skills to real-world scenarios, such as reporting, data analysis, and integrating with applications. +- **SQL Tools**: Familiarity with SQL-based tools and environments (e.g., MySQL Workbench, pgAdmin, SQL Server Management Studio). + +#### 8. Continuous Learning +- **Advanced SQL**: Exploring advanced topics like stored procedures, triggers, and advanced data types. +- **New SQL Technologies**: Keeping up with new developments in SQL databases and related technologies. + +#### Practical Tips +- **Practice Regularly**: Regular practice is crucial. Use platforms like LeetCode, HackerRank, and SQLZoo for SQL exercises. +- **Project-Based Learning**: Work on real projects that involve database design, querying, and optimization. +- **Stay Updated**: Follow SQL-related blogs, forums, and documentation to stay updated on best practices and new features. + +By mastering these aspects of SQL, you'll be well-equipped to handle data-related tasks and challenges in various roles within the data industry. + +## Getting Started with SQLite3 + +SQLite3 is a lightweight, self-contained, serverless database engine that is easy to set up and use. Here’s a guide to get you started with SQLite3: + +### 1. Installation + +SQLite3 often comes pre-installed on many systems. You can check if it is installed by running: + +```sh +sqlite3 --version +``` + +If it’s not installed, you can install it using the following commands: + +- **For Debian/Ubuntu:** + +```sh +sudo apt-get update +sudo apt-get install sqlite3 +``` + +- **For macOS:** + +```sh +brew install sqlite3 +``` + +### 2. Creating a Database + +You can create a new SQLite database by simply opening SQLite with a file name: + +```sh +sqlite3 mydatabase.db +``` + +This command creates a new file named `mydatabase.db` if it does not exist and opens the SQLite prompt. + +### 3. Basic Commands + +Here are some basic commands to get you started: + +- **Creating a Table:** + +```sql +CREATE TABLE users ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + age INTEGER +); +``` + +- **Inserting Data:** + +```sql +INSERT INTO users (name, age) VALUES ('Alice', 30); +INSERT INTO users (name, age) VALUES ('Bob', 25); +``` + +- **Querying Data:** + +```sql +SELECT * FROM users; +``` + +- **Updating Data:** + +```sql +UPDATE users SET age = 31 WHERE name = 'Alice'; +``` + +- **Deleting Data:** + +```sql +DELETE FROM users WHERE name = 'Bob'; +``` + +### 4. Using SQLite3 with Python + +SQLite3 can be used directly within Python using the `sqlite3` module. Here’s a quick example: + +1. **Connecting to the Database:** + +```python +import sqlite3 + +# Connect to database (or create it if it doesn't exist) +conn = sqlite3.connect('mydatabase.db') + +# Create a cursor object +cur = conn.cursor() +``` + +2. **Creating a Table:** + +```python +cur.execute(''' +CREATE TABLE users ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + age INTEGER +) +''') + +# Commit the changes +conn.commit() +``` + +3. **Inserting Data:** + +```python +cur.execute(''' +INSERT INTO users (name, age) VALUES (?, ?) +''', ('Alice', 30)) + +cur.execute(''' +INSERT INTO users (name, age) VALUES (?, ?) +''', ('Bob', 25)) + +# Commit the changes +conn.commit() +``` + +4. **Querying Data:** + +```python +cur.execute('SELECT * FROM users') +rows = cur.fetchall() + +for row in rows: + print(row) +``` + +5. **Updating Data:** + +```python +cur.execute(''' +UPDATE users SET age = ? WHERE name = ? +''', (31, 'Alice')) + +# Commit the changes +conn.commit() +``` + +6. **Deleting Data:** + +```python +cur.execute(''' +DELETE FROM users WHERE name = ? +''', ('Bob',)) + +# Commit the changes +conn.commit() +``` + +7. **Closing the Connection:** + +```python +# Close the cursor and connection +cur.close() +conn.close() +``` + +### 5. Additional Resources + +- **SQLite Official Documentation**: [https://sqlite.org/docs.html](https://sqlite.org/docs.html) +- **SQLite Tutorial**: [https://www.sqlitetutorial.net/](https://www.sqlitetutorial.net/) + +By following these steps and utilizing the resources mentioned, you'll be well on your way to mastering SQLite3 for your data projects. \ No newline at end of file