Add first chapter of SQL

feat/course
Kamran Ahmed 4 weeks ago
parent c3186b14db
commit b54eff9ef7
  1. 2
      .astro/settings.json
  2. 4
      src/components/Course/CourseView.tsx
  3. 5
      src/data/courses/sql/chapters/ddl/ddl.md
  4. 45
      src/data/courses/sql/chapters/introduction/lessons/basics-of-databases.md
  5. 20
      src/data/courses/sql/chapters/introduction/lessons/intro-to-sql.md
  6. 72
      src/data/courses/sql/chapters/introduction/lessons/types-of-queries.md
  7. 71
      src/data/courses/sql/chapters/introduction/lessons/welcome-quiz.md
  8. 72
      src/data/courses/sql/chapters/introduction/lessons/what-is-sql.md

@ -3,6 +3,6 @@
"enabled": false
},
"_variables": {
"lastUpdateCheck": 1729575132842
"lastUpdateCheck": 1734954299535
}
}

@ -90,8 +90,8 @@ export function CourseView(props: CourseViewProps) {
>
<div className="relative h-full">
<div className="absolute inset-0 overflow-y-auto [scrollbar-color:#3f3f46_#27272a;]">
<div className="mx-auto max-w-xl p-4">
<h3 className="mt-10 text-4xl font-semibold">
<div className="mx-auto max-w-2xl p-4">
<h3 className="mt-10 text-4xl font-bold">
{lesson.frontmatter.title}
</h3>
<div className="course-content prose prose-lg mt-8 text-black prose-headings:mb-3 prose-headings:mt-8 prose-pre:rounded-2xl prose-pre:text-lg prose-li:my-1 prose-thead:border-zinc-800 prose-tr:border-zinc-800">

@ -1,5 +0,0 @@
---
title: Data Definition Language (DDL)
description: Learn how to create, modify, and delete database objects using SQL Data Definition Language (DDL) statements.
order: 200
---

@ -0,0 +1,45 @@
---
title: Basics of Databases
description: Learn the basics of SQL, the language for querying databases.
order: 100
type: lesson
---
Before we dive into everything SQL, let's first get the basics out. This chapter covers the basics of databases, different types of databases, SQL, it's different flavors, and the types of queries.
## What is a Database?
Every application deals with data in one way or another. Whether it's a social media app storing your posts and friend connections, an e-commerce platform managing product inventories and customer orders, or a banking application handling transactions and account balances - all these applications need somewhere to store their data. This is where databases come in - they provide a systematic way to store, organize, manage and retrieve data efficiently.
To put it simply, a database is a structured collection of data stored in a way that allows for easy retrieval and manipulation. For example, a library database stores information about books, members, and borrowing records - tracking details like book titles, authors, ISBN numbers, member information, due dates, and lending history. This allows librarians to quickly look up books, check availability, manage memberships, and ensure timely returns.
## Database Management Systems (DBMS)
To work with databases, we need specialized software called Database Management Systems (DBMS). A DBMS allows users and application programs to create, read, update, and delete data in the database while handling tasks like security, backup, and performance optimization. Some popular database management systems include MySQL, PostgreSQL, Oracle, Microsoft SQL Server, MongoDB, and more.
## Types of Databases
There are several types of databases, but the two most common ones are:
- Relational Databases
- Non-Relational Databases
### Relational Databases
Data in a relational database is organized into tables, where each table consists of rows and columns. Each row represents a unique record, and each column represents a specific attribute or characteristic of that record. Rows have unique identifiers normally called primary keys. These primary keys are used to link tables together. We will learn more about properly defining tables and relationships in the coming chapters.
Relational databases use SQL (Structured Query Language, pronounced as "sequel" or "S-Q-L") to retrieve, insert, update, and delete data from the database. Both pronunciations are widely accepted in the industry, though "sequel" is more common. The name comes from its purpose as a Structured language for Querying data.
### Non-Relational Databases
Non-relational databases, also known as NoSQL databases, do not use tables to store data. Instead, they use various data models like key-value pairs, documents, graphs, or columnar stores. Some popular NoSQL databases include MongoDB, Cassandra, Couchbase, and Redis.
---
## Takeaways
- A database is a structured collection of data stored in a way that allows for easy retrieval and manipulation.
- To work with databases, we need specialized software called Database Management Systems (DBMS).
- There are two main types of databases: Relational Databases and Non-Relational Databases.
- Relational databases use SQL (pronounced as "sequel" or "S-Q-L") to interact with data.
- Non-relational databases use various data models like key-value pairs, documents, graphs, or columnar stores.

@ -1,20 +0,0 @@
---
title: Intro to SQL
description: Learn the basics of SQL, the language for querying databases.
order: 100
type: lesson
---
The SQL language is widely used today across web frameworks and database applications. Knowing SQL gives you the freedom to explore your data, and the power to make better decisions. By learning SQL, you will also learn concepts that apply to nearly every data storage system.
The statements covered in this course use SQLite Relational Database Management System (RDBMS). You can also access a glossary of all the SQL commands taught in this course.
We have some code sample here for users to test. For example this is an example of `inline-code` entry and below is an example of a code block:
```javascript
const someVariable = 'xyz';
console.log(someVariable);
```

@ -0,0 +1,72 @@
---
title: Types of Queries
description: Learn the different types of queries in SQL.
order: 160
type: lesson
---
SQL (Structured Query Language) is a powerful tool for managing and manipulating data in relational databases. It provides a variety of query types that serve different purposes, from retrieving data to modifying database structures and more. There are three core types of queries in SQL:
1. Data Query Language (DQL)
2. Data Manipulation Language (DML)
3. Data Definition Language (DDL)
However, there are two additional types of queries that are used to manage the database itself:
1. Data Control Language (DCL)
2. Transaction Control Language (TCL)
DCL and TCL are not covered in this course, but we will cover the other three types of queries in detail.
## Data Query Language (DQL)
DQL is used to retrieve data from a database, enabling users to extract meaningful information based on specific conditions. The most common DQL statement is the `SELECT` statement.
For example, the following statement retrieves all rows from the `users` table:
```sql
SELECT * FROM users;
```
## Data Manipulation Language (DML)
DML is used to manipulate data stored in the database, such as inserting, updating, or deleting records. The most common DML statements are `INSERT`, `UPDATE`, and `DELETE`.
For example, the following statement inserts a new row into the `users` table:
```sql
INSERT INTO users (name, email, age)
VALUES ('John Doe', 'john.doe@example.com', 25);
```
The following statement updates the email address of the user with the id 1:
```sql
UPDATE users
SET email = 'john.doe@example.com'
WHERE id = 1;
```
The following statement deletes the user with the id 1:
```sql
DELETE FROM users
WHERE id = 1;
```
## Data Definition Language (DDL)
DDL is used to define the structure of the database. The most common DDL statements are `CREATE`, `ALTER`, and `DROP`.
For example, the following statement creates a new table called `users`:
```sql
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(255),
email VARCHAR(255),
age INT
);
```
Don't worry if you don't understand the syntax of these statements yet, we will cover them in detail in later chapters.

@ -1,71 +0,0 @@
---
title: Welcome Quiz
description: Learn the basics of SQL, the language for querying databases.
order: 150
type: lesson-quiz
questions:
- id: 1
title: 'Which of the following SQL clauses is used to filter results after the GROUP BY clause?'
options:
- id: 1
text: 'WHERE'
- id: 2
text: 'HAVING'
isCorrectOption: true
- id: 3
text: 'GROUP BY'
- id: 4
text: 'ORDER BY'
- id: 2
title: 'Which SQL function is used to return the first non-null expression?'
options:
- id: 1
text: 'COALESCE'
isCorrectOption: true
- id: 2
text: 'IFNULL'
- id: 3
text: 'NULLIF'
- id: 4
text: 'NVL'
- id: 3
title: 'What is the purpose of an SQL CTE (Common Table Expression)?'
options:
- id: 1
text: 'To create temporary tables that last for the duration of a query'
isCorrectOption: true
- id: 2
text: 'To define reusable views'
- id: 3
text: 'To encapsulate subqueries'
- id: 4
text: 'To optimize the execution of queries'
- id: 4
title: 'In an SQL window function, which clause defines the subset of rows to apply the function on?'
options:
- id: 1
text: 'ORDER BY'
- id: 2
text: 'PARTITION BY'
isCorrectOption: true
- id: 3
text: 'GROUP BY'
- id: 4
text: 'DISTINCT'
- id: 5
title: 'Which SQL join returns all rows when there is a match in either of the tables?'
options:
- id: 1
text: 'INNER JOIN'
- id: 2
text: 'LEFT JOIN'
- id: 3
text: 'RIGHT JOIN'
- id: 4
text: 'FULL OUTER JOIN'
isCorrectOption: true
---
The SQL language is widely used today across web frameworks and database applications. Knowing SQL gives you the freedom to explore your data, and the power to make better decisions. By learning SQL, you will also learn concepts that apply to nearly every data storage system.
The statements covered in this course use SQLite Relational Database Management System (RDBMS). You can also access a glossary of all the SQL commands taught in this course.

@ -0,0 +1,72 @@
---
title: What is SQL?
description: Learn the basics of SQL, the language for querying databases.
order: 150
type: lesson
---
As discussed in the previous chapter, SQL (Structured Query Language) is the language used to interact with relational databases. It is a standardized language that allows us to perform various operations on the data stored in the database. We will cover the different operations in detail in later chapters but for now, these operations can include creating, reading, updating, and deleting data.
## SQL is a Declarative Language
SQL is a declarative language, which means that we describe **what we want to achieve rather than how to achieve it**. This is in contrast to imperative languages like Python or JavaScript, where we provide step-by-step instructions on how to achieve a task e.g. to get all students from a file using Python we would write:
```python
# Open the file
students = open("students.txt", "r")
# Read the file
students = students.readlines()
# Print the file
print(students)
```
As you can see, we have to provide the steps to achieve the task. While in SQL, to get all students from a database we would write:
```sql
SELECT * FROM students;
```
Here we are telling the database to select all columns from the `students` table. The database will then figure out how to achieve this task.
## SQL is Case-Insensitive
SQL is case-insensitive, which means that the database will not differentiate between uppercase and lowercase letters. For example, `SELECT` and `select` are considered the same.
## Components of SQL
A relational database is made up of several components. We will cover several of the components in later chapters but the most important ones you need to know for now are Tables, Rows, and Columns.
- **Tables:** A table is a collection of data organized into rows and columns.
- **Rows:** A row is a single record in a table e.g. a `student` table might have a row for each student.
- **Columns:** A table is made up of columns, each column represent a specific attribute of the data e.g. a `student` table might have columns for `name`, `age`, `email`, etc.
There are many other components in a database but these are the most important ones you need to know for now to understand basic SQL operations for querying data.
SQL statements or queries are the commands that we use to interact with the database. They are the building blocks of SQL and are used to perform various operations on the data stored in the database. For example, to get all students from a database where the age is greater than 18 we would write:
```sql
SELECT *
FROM students
WHERE age > 18;
```
SQL statements are made up of clauses. A clause is a group of SQL keywords and conditions that perform a specific task. For example, if you look at the SQL statement above, we have the `SELECT` clause, the `FROM` clause, and the `WHERE` clause.
We will cover the different clauses in detail in later chapters but for now, here are some of the most common clauses:
- `SELECT`: Used to select data from a database.
- `FROM`: Specifies the table to select data from.
- `WHERE`: Filters the data to select.
- `GROUP BY`: Groups the data by one or more columns.
- `ORDER BY`: Orders the data by one or more columns.
- `LIMIT`: Limits the number of rows returned.
## Flavors of SQL
SQL is a standard language based on the [ISO/IEC 9075 standard](https://en.wikipedia.org/wiki/ISO/IEC_9075). However, there are many different flavors of SQL that are based on this standard. These flavors are different implementations of the SQL standard, optimized for specific databases. For example, MySQL, PostgreSQL, Oracle, and Microsoft SQL Server are all different flavors of SQL.
While these flavors differ, they are all based on the same standard, and therefore, the SQL statements you write will be compatible with most of them. By completing this course, you will be able to write SQL statements that are compatible with most popular database systems.
## Conclusion
In this chapter, we covered the basics of SQL, the language for querying databases. We discussed the different operations that can be performed on a database, the components of a database, the syntax of SQL statements, and the different flavors of SQL. In the next chapter, we will cover the different types of queries in SQL.
Loading…
Cancel
Save