Update tech_docs/database/sql_guide.md

This commit is contained in:
2024-06-16 20:21:19 +00:00
parent 94e66b95b1
commit b8238d3d6d

View File

@@ -1,3 +1,257 @@
### SQL Basics
#### 1. **Database and Table Structure**
- **Database:** A collection of related data organized for easy access and management.
- **Table:** A structured set of data organized in rows and columns within a database.
#### 2. **Basic SQL Commands**
- **SELECT:** Used to fetch data from a database.
- **INSERT:** Used to add new data into a table.
- **UPDATE:** Used to modify existing data in a table.
- **DELETE:** Used to remove data from a table.
### Writing SQL Queries
#### 1. **SELECT Statement**
The `SELECT` statement is used to retrieve data from a database.
```sql
SELECT column1, column2, ...
FROM table_name;
```
- **Example:**
```sql
SELECT Name, Composer
FROM Track;
```
This query retrieves the `Name` and `Composer` columns from the `Track` table.
#### 2. **WHERE Clause**
The `WHERE` clause is used to filter records.
```sql
SELECT column1, column2, ...
FROM table_name
WHERE condition;
```
- **Example:**
```sql
SELECT Name, Composer
FROM Track
WHERE GenreId = 1;
```
This query retrieves the names and composers of tracks in the genre with `GenreId` 1.
#### 3. **INSERT INTO Statement**
The `INSERT INTO` statement is used to add new records to a table.
```sql
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
```
- **Example:**
```sql
INSERT INTO Artist (Name)
VALUES ('New Artist');
```
This query adds a new artist named 'New Artist' to the `Artist` table.
#### 4. **UPDATE Statement**
The `UPDATE` statement is used to modify existing records.
```sql
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
```
- **Example:**
```sql
UPDATE Track
SET Composer = 'New Composer'
WHERE TrackId = 1;
```
This query updates the composer of the track with `TrackId` 1 to 'New Composer'.
#### 5. **DELETE Statement**
The `DELETE` statement is used to remove existing records from a table.
```sql
DELETE FROM table_name
WHERE condition;
```
- **Example:**
```sql
DELETE FROM Artist
WHERE ArtistId = 1;
```
This query removes the artist with `ArtistId` 1.
### Advanced SQL Concepts
#### 1. **JOINs**
JOINs are used to combine rows from two or more tables based on a related column.
- **INNER JOIN:** Returns records with matching values in both tables.
```sql
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
```
- **Example:**
```sql
SELECT a.Name, t.Title
FROM Album a
INNER JOIN Artist ar ON a.ArtistId = ar.ArtistId
WHERE ar.Name = 'AC/DC';
```
This query returns the names of albums by the artist 'AC/DC'.
- **LEFT JOIN:** Returns all records from the left table and the matched records from the right table.
```sql
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
```
- **RIGHT JOIN:** Returns all records from the right table and the matched records from the left table.
```sql
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
```
- **FULL OUTER JOIN:** Returns all records when there is a match in either left or right table.
```sql
SELECT columns
FROM table1
FULL OUTER JOIN table2
ON table1.common_column = table2.common_column;
```
#### 2. **GROUP BY and HAVING**
- **GROUP BY:** Used to arrange identical data into groups.
```sql
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1;
```
- **Example:**
```sql
SELECT GenreId, COUNT(*)
FROM Track
GROUP BY GenreId;
```
This query counts the number of tracks in each genre.
- **HAVING:** Used to filter groups.
```sql
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1
HAVING COUNT(*) > 1;
```
- **Example:**
```sql
SELECT GenreId, COUNT(*)
FROM Track
GROUP BY GenreId
HAVING COUNT(*) > 10;
```
This query returns genres with more than 10 tracks.
#### 3. **ORDER BY**
The `ORDER BY` statement is used to sort the result set.
```sql
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 ASC|DESC;
```
- **Example:**
```sql
SELECT Name, Composer
FROM Track
ORDER BY Name ASC;
```
This query retrieves track names and composers sorted by the track name in ascending order.
### Practical Example
Consider the following database schema in the Chinook database:
- **Artist Table:**
| ArtistId | Name |
| -------- | ---------- |
| 1 | AC/DC |
| 2 | Accept |
- **Album Table:**
| AlbumId | Title | ArtistId |
| ------- | ------------- | -------- |
| 1 | For Those... | 1 |
| 2 | Balls to... | 2 |
#### Query Examples
- **Fetch all tracks in the 'Rock' genre:**
```sql
SELECT Name, Composer
FROM Track
WHERE GenreId = 1;
```
- **Fetch all artists and the number of albums they have:**
```sql
SELECT ar.Name, COUNT(al.AlbumId) AS album_count
FROM Artist ar
LEFT JOIN Album al ON ar.ArtistId = al.ArtistId
GROUP BY ar.Name;
```
This should give you a solid foundation to start with SQL using the Chinook database.
---
# Complete Guide to Getting Started with SQL and SQLite3 # Complete Guide to Getting Started with SQL and SQLite3
## Introduction to SQL ## Introduction to SQL