Skip to content

Commit bae106c

Browse files
committed
first
1 parent a8a1118 commit bae106c

File tree

337 files changed

+5853
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

337 files changed

+5853
-1
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
# fcc-learn-sql-assets
1+
# fcc-learn-sql-assets
2+
3+
This is a snapshot of the code samples for the ["Learn SQL" course](https://boot.dev/courses/learn-sql) on [Boot.dev](https://boot.dev) at the time the video for FreeCodeCamp was released on YouTube. If you want the most up-to-date version of the code, please visit the official [Boot.dev course](https://boot.dev/courses/learn-sql). Otherwise, if you're looking for the files used in the video, you're in the right place!
4+
5+
* [Course code samples](/course)
6+
7+
## License
8+
9+
You are free to use this content and code for personal education purposes. However, you are *not* authorized to publish this content or code elsewhere, whether for commercial purposes or not.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT * from people;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT * from users;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# What is SQL?
2+
3+
Structured Query Language, or [SQL](https://en.wikipedia.org/wiki/SQL), is the primary programming language used to manage and interact with [relational databases](https://cloud.google.com/learn/what-is-a-relational-database). SQL can perform various operations such as creating, updating, reading, and deleting records within a database.
4+
5+
@[youtube](https://www.youtube.com/watch?v=pYKirBUnr-8)
6+
7+
## CashPal
8+
9+
In this course, we will be building the database for a pretend PayPal clone called *CashPal*! Storing information related to people's money, transactions, and identity is very important! So we will need to make sure we use proper conventions to build a safe, and reliable database architecture that our users can rely on.
10+
11+
## Assignment
12+
13+
I have provided a simple SQL statement for you that retrieves some records from a table. However there isn't a `people` table, the table in our database is called `users`. Fix the bug by changing `people` to `users` within the `SELECT` statement.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TABLE users (id INTEGER, name TEXT, age INTEGER, is_admin BOOLEAN);
2+
INSERT into users (id, name, age, is_admin) values (1, 'John Doe', 27, false);
3+
INSERT into users (id, name, age, is_admin) values (2, 'Sally Rae', 18, true);

course/1-what_is_sql/exercises/2-select-statement/code.sql

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT name, balance from users;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# SQL Select Statement
2+
3+
Let's write our own SQL statement from scratch! A `SELECT` statement is the most common operation in SQL - often called a "query". `SELECT` retrieves data from one or more tables. Standard `SELECT` statements do *not* alter the state of the database.
4+
5+
```SQL
6+
SELECT id from users;
7+
```
8+
9+
## Select a single field
10+
11+
A `SELECT` statement begins with the keyword `SELECT` followed by the fields you want to retrieve.
12+
13+
```SQL
14+
SELECT id from users;
15+
```
16+
17+
## Select multiple fields
18+
19+
If you want to select more than one field you can specify multiple fields separated by commas.
20+
21+
```SQL
22+
SELECT id, name from users;
23+
```
24+
25+
## Select all fields
26+
27+
If you want to select *every* field in a record you can use the shorthand `*` syntax.
28+
29+
```SQL
30+
SELECT * from users;
31+
```
32+
33+
After specifying fields, you need to indicate which table you want to pull the records *from* using the `from` statement followed by the name of the table. We'll talk more about tables later, but for now, you can think about them like structs or objects. For example, the `users` table might have 3 fields:
34+
35+
* `id`
36+
* `name`
37+
* `balance`
38+
39+
And finally, *all* statements end with a semi-colon `;`.
40+
41+
## Assignment
42+
43+
The state of our *CashPal* `users` table is as follows:
44+
45+
| id | name | age | balance | is_admin |
46+
| --- | ------------- | --- | ------- | -------- |
47+
| 1 | John Smith | 28 | 450 | 1 |
48+
| 2 | Darren Walker | 27 | 200 | 1 |
49+
| 2 | Jane Morris | 33 | 496.24 | 0 |
50+
51+
It's very common to write queries that only return specific portions of data from a table. Our HR team has requested a report asking for all the `name`s and `balance`s of all of our users.
52+
53+
Write a query that retrieves all of the `name`s and `balance`s from the `users` table.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE users (id INTEGER, name TEXT, age INTEGER, balance INTEGER, is_admin BOOLEAN);
2+
INSERT into users (id, name, age, balance, is_admin) values (1, 'John Smith', 28, 450.00, true);
3+
INSERT into users (id, name, age, balance, is_admin) values (2, 'Darren Walker', 27, 200.00, true);
4+
INSERT into users (id, name, age, balance, is_admin) values (2, 'Jane Morris', 33, 496.24, false);

course/1-what_is_sql/exercises/3-sql-technologies/code.sql

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SELECT id, name, is_admin from users;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Which Databases Use SQL?
2+
3+
SQL is just a query language. You typically use it to interact with a specific database technology. For example:
4+
5+
* [SQLite](https://www.sqlite.org/index.html)
6+
* [PostgreSQL](https://www.postgresql.org/)
7+
* [MySQL](https://www.mysql.com/)
8+
* [CockroachDB](https://www.cockroachlabs.com/)
9+
* [Oracle](https://www.oracle.com/database/)
10+
* etc...
11+
12+
Although many different databases use the SQL *language*, most of them will have their own *dialect*. It's *critical* to understand that *not* all databases are created equal. Just because one SQL-compatible database does things a certain way, doesn't mean every SQL-compatible database will follow those exact same patterns.
13+
14+
## We're using SQLite
15+
16+
In this course, we'll be using [SQLite](https://www.sqlite.org/index.html) specifically. SQLite is great for embedded projects, web browsers, and toy projects. It's lightweight, but has limited functionality compared to the likes of PostgreSQL or MySQL - two of the more common production SQL technologies.
17+
18+
We'll point out to you whenever some functionality we're working with is unique to SQLite!
19+
20+
## Assignment
21+
22+
One way in which SQLite is a bit different is that it stores Boolean values as integers - the integers `0` and `1`.
23+
24+
* `0` = `false`
25+
* `1` = `true`
26+
27+
Select all of the `id`s, `name`s, and `is_admin` flags from the `users` table.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE users (id INTEGER, name TEXT, age INTEGER, is_admin BOOLEAN);
2+
INSERT into users (id, name, age, is_admin) values (1, 'Lane Holland', 27, false);
3+
INSERT into users (id, name, age, is_admin) values (2, 'Allan Rae', 18, true);
4+
INSERT into users (id, name, age, is_admin) values (2, 'Sally Wagoner', 18, true);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"question": "Each NoSQL Database tends to use ____ query language(s)",
3+
"answers": [
4+
"different",
5+
"the same"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# NoSQL vs SQL
2+
3+
When talking about SQL databases, we also have to mention the elephant in the room: [NoSQL](https://en.wikipedia.org/wiki/NoSQL).
4+
5+
To put it simply, a NoSQL database is a database that does *not* use SQL (Structured Query Language). Each NoSQL typically has its own way of writing and executing queries. For example, [MongoDB](https://www.mongodb.com/) uses MQL (MongoDB Query Language) and [ElasticSearch](https://www.elastic.co/) simply has a JSON API.
6+
7+
While most relational databases are fairly similar, NoSQL databases tend to be fairly unique and are used for more niche purposes. Some of the main differences between a SQL and NoSQL databases are:
8+
9+
1. NoSQL databases are usually non-relational, SQL databases are usually [relational](https://cloud.google.com/learn/what-is-a-relational-database) (we'll talk more about what this means later).
10+
2. SQL databases usually have a defined schema, NoSQL databases usually have dynamic schema.
11+
3. SQL databases are table-based, NoSQL databases have a variety of different storage methods, such as document, key-value, graph, wide-column, and more.
12+
13+
@[youtube](https://www.youtube.com/watch?v=NovjCrDFlXk)
14+
15+
## Types of NoSQL databases
16+
17+
* [Document Database](https://en.wikipedia.org/wiki/Document-oriented_database)
18+
* [Key-Value Store](https://en.wikipedia.org/wiki/Key%E2%80%93value_database)
19+
* [Wide-Column](https://en.wikipedia.org/wiki/Wide-column_store)
20+
* [Graph](https://en.wikipedia.org/wiki/Graph_database)
21+
22+
A few of the most popular NoSQL databases are:
23+
24+
* [MongoDB](https://en.wikipedia.org/wiki/MongoDB)
25+
* [Cassandra](https://en.wikipedia.org/wiki/Apache_Cassandra)
26+
* [CouchDB](https://en.wikipedia.org/wiki/Apache_CouchDB)
27+
* [DynamoDB](https://en.wikipedia.org/wiki/Amazon_DynamoDB)
28+
* [ElasticSearch](https://www.elastic.co/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"question": "____ compatible databases tend to be more similar in their functionality than ____ databases",
3+
"answers": [
4+
"SQL, NoSQL",
5+
"NoSQL, SQL"
6+
]
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# NoSQL vs SQL
2+
3+
When talking about SQL databases, we also have to mention the elephant in the room: [NoSQL](https://en.wikipedia.org/wiki/NoSQL).
4+
5+
To put it simply, a NoSQL database is a database that does *not* use SQL (Structured Query Language). Each NoSQL typically has its own way of writing and executing queries. For example, [MongoDB](https://www.mongodb.com/) uses MQL (MongoDB Query Language) and [ElasticSearch](https://www.elastic.co/) simply has a JSON API.
6+
7+
While most relational databases are fairly similar, NoSQL databases tend to be fairly unique and are used for more niche purposes. Some of the main differences between a SQL and NoSQL databases are:
8+
9+
1. NoSQL databases are usually non-relational, SQL databases are usually [relational](https://cloud.google.com/learn/what-is-a-relational-database) (we'll talk more about what this means later).
10+
2. SQL databases usually have a defined schema, NoSQL databases usually have dynamic schema.
11+
3. SQL databases are table-based, NoSQL databases have a variety of different storage methods, such as document, key-value, graph, wide-column, and more.
12+
13+
@[youtube](https://www.youtube.com/watch?v=NovjCrDFlXk)
14+
15+
## Types of NoSQL databases
16+
17+
* [Document Database](https://en.wikipedia.org/wiki/Document-oriented_database)
18+
* [Key-Value Store](https://en.wikipedia.org/wiki/Key%E2%80%93value_database)
19+
* [Wide-Column](https://en.wikipedia.org/wiki/Wide-column_store)
20+
* [Graph](https://en.wikipedia.org/wiki/Graph_database)
21+
22+
A few of the most popular NoSQL databases are:
23+
24+
* [MongoDB](https://en.wikipedia.org/wiki/MongoDB)
25+
* [Cassandra](https://en.wikipedia.org/wiki/Apache_Cassandra)
26+
* [CouchDB](https://en.wikipedia.org/wiki/Apache_CouchDB)
27+
* [DynamoDB](https://en.wikipedia.org/wiki/Amazon_DynamoDB)
28+
* [ElasticSearch](https://www.elastic.co/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"question": "Which type of database always uses table structures?",
3+
"answers": [
4+
"SQL",
5+
"NoSQL",
6+
"Both"
7+
]
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# NoSQL vs SQL
2+
3+
When talking about SQL databases, we also have to mention the elephant in the room: [NoSQL](https://en.wikipedia.org/wiki/NoSQL).
4+
5+
To put it simply, a NoSQL database is a database that does *not* use SQL (Structured Query Language). Each NoSQL typically has its own way of writing and executing queries. For example, [MongoDB](https://www.mongodb.com/) uses MQL (MongoDB Query Language) and [ElasticSearch](https://www.elastic.co/) simply has a JSON API.
6+
7+
While most relational databases are fairly similar, NoSQL databases tend to be fairly unique and are used for more niche purposes. Some of the main differences between a SQL and NoSQL databases are:
8+
9+
1. NoSQL databases are usually non-relational, SQL databases are usually [relational](https://cloud.google.com/learn/what-is-a-relational-database) (we'll talk more about what this means later).
10+
2. SQL databases usually have a defined schema, NoSQL databases usually have dynamic schema.
11+
3. SQL databases are table-based, NoSQL databases have a variety of different storage methods, such as document, key-value, graph, wide-column, and more.
12+
13+
@[youtube](https://www.youtube.com/watch?v=NovjCrDFlXk)
14+
15+
## Types of NoSQL databases
16+
17+
* [Document Database](https://en.wikipedia.org/wiki/Document-oriented_database)
18+
* [Key-Value Store](https://en.wikipedia.org/wiki/Key%E2%80%93value_database)
19+
* [Wide-Column](https://en.wikipedia.org/wiki/Wide-column_store)
20+
* [Graph](https://en.wikipedia.org/wiki/Graph_database)
21+
22+
A few of the most popular NoSQL databases are:
23+
24+
* [MongoDB](https://en.wikipedia.org/wiki/MongoDB)
25+
* [Cassandra](https://en.wikipedia.org/wiki/Apache_Cassandra)
26+
* [CouchDB](https://en.wikipedia.org/wiki/Apache_CouchDB)
27+
* [DynamoDB](https://en.wikipedia.org/wiki/Amazon_DynamoDB)
28+
* [ElasticSearch](https://www.elastic.co/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE users (id INTEGER, name TEXT, age INTEGER);
2+
INSERT into users (id, name, age) values (1, 'John Doe', 21);
3+
INSERT into users (id, name, age) values (2, 'Montgomery Burns', 33);
4+
SELECT * from users;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE users (id INTEGER, name TEXT, age INTEGER);
2+
INSERT into users (id, name, age) values (1, 'John Doe', 21);
3+
INSERT into users (id, name, age) values (2, 1, 33);
4+
SELECT * from users;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Comparing SQL Databases
2+
3+
Let's dive deeper and talk about some of the popular SQL Databases and what makes them different from one another. Some of the most popular SQL Databases right now are:
4+
5+
* [PostgreSQL](https://en.wikipedia.org/wiki/PostgreSQL)
6+
* [MySQL](https://en.wikipedia.org/wiki/MySQL)
7+
* [Microsoft SQL Server](https://db-engines.com/en/system/Microsoft+SQL+Server)
8+
* [SQLite](https://en.wikipedia.org/wiki/SQLite)
9+
* [And many others](https://en.wikipedia.org/wiki/List_of_relational_database_management_systems)
10+
11+
*Source: [db-engines.com](https://db-engines.com/en/ranking)*
12+
13+
While all of these Databases use SQL, each database defines specific rules, practices, and strategies that separate them from their competitors.
14+
15+
## SQLite vs PostgreSQL
16+
17+
Personally, SQLite and PostgreSQL are my favorites from the list above. Postgres is a very powerful, open-source, production-ready SQL database. SQLite is a lightweight, embeddable, open-source database. I usually choose one of these technologies if I'm doing SQL work.
18+
19+
SQLite is a serverless database management system (DBMS) that has the ability to run within applications, whereas PostgreSQL uses a Client-Server model and requires a server to be installed and listening on a network, similar to an HTTP server.
20+
21+
See a full [comparison here](https://db-engines.com/en/system/PostgreSQL%3BSQLite).
22+
23+
## We use SQLite in this course
24+
25+
In this course, we will be working with SQLite, a lightweight and simple database. For most backend web servers, PostgreSQL is a more production-ready option, but SQLite is great for learning and for small systems.
26+
27+
## Assignment
28+
29+
Let's take a look at how SQLite does *not* enforce type-checking. Notice that within the `CREATE TABLE` statement, `name` is defined as a `TEXT` field.
30+
31+
1. Run the code and take a look at the results (don't submit yet!)
32+
2. On line `3`, change the text string `'Montgomery Burns'` to the integer `1` and run the code
33+
34+
Notice how even though we defined `name` as a `TEXT` field, SQLite allowed us to use an integer! Like Python and JavaScript, SQLite has a loose type system... You can store any type of data in any field, regardless of how you defined it. *Remember: just because you can do something, doesn't mean you should!*
35+
36+
To pass the assignment, submit the code in the altered state, where the record with `id` `2` has a `name` of `1`.

course/1-what_is_sql/exercises/5-sql-tech-compare/up.sql

Whitespace-only changes.

course/10-joining-tables/exercises/1-inner_join/code.sql

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT *
2+
FROM users
3+
INNER JOIN countries
4+
ON countries.country_code = users.country_code;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Joins
2+
3+
Joins are one of the most important features that SQL offers. Joins allow us to make use of the relationships we have set up between our tables. In short, joins allow us to query multiple tables at the *same time.*
4+
5+
## Inner Join
6+
7+
The simplest and most common type of join in SQL is the `INNER JOIN`. By default, a `JOIN` command is an `INNER JOIN`. An `INNER JOIN` returns all of the records in `table_a` that have matching records in `table_b` as demonstrated by the following Venn diagram.
8+
9+
![inner join](https://i.imgur.com/wgxAmhA.png)
10+
11+
## On
12+
13+
In order to perform a join, we need to tell the database which fields should be "matched up". The `ON` clause is used to specify these columns to join.
14+
15+
```SQL
16+
SELECT *
17+
FROM employees
18+
INNER JOIN departments
19+
ON employees.department_id = departments.id;
20+
```
21+
22+
The query above returns *all* the fields from *both* tables. The `INNER` keyword doesn't have anything to do with the number of *columns* returned - it only affects the number of *rows* returned.
23+
24+
## Assignment
25+
26+
Our frontend team is working on a profile page and would like to display a user's country *name* instead of just the country's two-letter *code*. Let's start by writing a simple join between the `users` table and `countries` table. We will expand on this query more in the next exercise.
27+
28+
* Write an `INNER JOIN` between `users` and `countries`
29+
* Return *all* fields from both tables
30+
* Join on the `country_code` field
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
CREATE TABLE users (
2+
id INTEGER PRIMARY KEY,
3+
name TEXT NOT NULL,
4+
age INTEGER NOT NULL,
5+
country_code TEXT NOT NULL,
6+
username TEXT UNIQUE,
7+
password TEXT NOT NULL,
8+
is_admin BOOLEAN
9+
);
10+
11+
INSERT INTO users(id, name, age, country_code, username, password, is_admin)
12+
VALUES (1, 'David', 34, 'US', 'DavidDev', 'insertPractice', false);
13+
14+
INSERT INTO users(id, name, age, country_code, username, password, is_admin)
15+
VALUES (2, 'Samantha', 29, 'BR', 'Sammy93', 'addingRecords!', false);
16+
17+
INSERT INTO users(id, name, age, country_code, username, password, is_admin)
18+
VALUES (3, 'John', 39, 'CA', 'Jjdev21', 'welovebootdev', false);
19+
20+
INSERT INTO users(id, name, age, country_code, username, password, is_admin)
21+
VALUES (4, 'Ram', 42, 'IN', 'Ram11c', 'thisSQLcourserocks', false);
22+
23+
INSERT INTO users(id, name, age, country_code, username, password, is_admin)
24+
VALUES (5, 'Hunter', 30, 'US', 'Hdev92', 'backendDev', false);
25+
26+
INSERT INTO users(id, name, age, country_code, username, password, is_admin)
27+
VALUES (6, 'Allan', 27, 'US', 'Alires', 'iLoveB00tdev', true);
28+
29+
INSERT INTO users(name, age, country_code, username, password, is_admin)
30+
VALUES ('Lance', 20, 'US', 'LanChr', 'b00tdevisbest', false);
31+
32+
INSERT INTO users(name, age, country_code, username, password, is_admin)
33+
VALUES ('Tiffany', 28, 'US', 'Tifferoon', 'autoincrement', true);
34+
35+
INSERT INTO users(name, age, country_code, username, password, is_admin)
36+
VALUES ('Lane', 27, 'US', 'wagslane', 'update_me', false);
37+
38+
INSERT INTO users(name, age, country_code, username, password, is_admin)
39+
VALUES ('Darren', 15, 'CA', 'Dshan', 'found_me', false);
40+
41+
INSERT INTO users(name, age, country_code, username, password, is_admin)
42+
VALUES ('Albert', 55, 'BR', 'BertDev', 'one_al_name', false);
43+
44+
INSERT INTO users(name, age, country_code, username, password, is_admin)
45+
VALUES ('Alvin', 27, 'US', 'AlvinA27', 'easter_egg', false);
46+
47+
INSERT INTO users(name, age, country_code, username, password, is_admin)
48+
VALUES ('Al', 39, 'JP', 'quickCoder', 'snake_case', false);
49+
50+
CREATE TABLE countries (
51+
id INTEGER PRIMARY KEY,
52+
country_code TEXT,
53+
name TEXT,
54+
FOREIGN KEY (country_code)
55+
REFERENCES users (id)
56+
);
57+
58+
INSERT INTO countries(country_code, name)
59+
VALUES ('US', 'United States');
60+
61+
INSERT INTO countries(country_code, name)
62+
VALUES ('CA', 'Canada');
63+
64+
INSERT INTO countries(country_code, name)
65+
VALUES ('IN', 'India');
66+
67+
INSERT INTO countries(country_code, name)
68+
VALUES ('JP', 'Japan');
69+
70+
INSERT INTO countries(country_code, name)
71+
VALUES ('BR', 'Brazil');

0 commit comments

Comments
 (0)