Skip to content

Commit 3556dca

Browse files
committed
added the table creation part
1 parent 47c4c14 commit 3556dca

File tree

1 file changed

+95
-158
lines changed

1 file changed

+95
-158
lines changed

docs/sql/table-transformation/table-creation.md

Lines changed: 95 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -13,108 +13,42 @@ Welcome to the **Selecting Data** module! This foundational learning path is des
1313

1414
### 📘 Creating SQL Table
1515

16-
In this tutorial, you'll learn how to interpret and use rows in a database table. Tables are essential to storing structured data, and each **row** in a table represents a unique **item or record**.
17-
> Each row of a table represents a new item.
16+
In SQL, when creating a table, each column must have a data type that defines what kind of data it can hold. Common data types include:
17+
> `INTEGER` – whole numbers
18+
> `REAL` – decimal (floating-point) numbers
19+
> `TEXT` – strings of characters
1820
19-
> Each column of a table represents a specific attribute of the data, such as `id`, `name`, or `username`.
20-
> These columns define the **type of information** stored for each item in the table.
2121

22+
>Other types like `BLOB` and `NULL` exist but are not covered in this tutorial.
2223
23-
For example, consider a table named `Friends`. Below is how a simple table might look:
2424

25-
26-
## Task: Sort a table using the `ORDER BY` clause
25+
## 🛠️ Example: Creating a Table
2726

2827
### Lesson Overview
2928

30-
After using `ORDER BY`, we specify the column by which we want to order the entries.
31-
For example, to sort by the `name` column:
29+
Let’s create a simple table named Directory, which holds the floor number and company name.
30+
31+
>This CREATE TABLE statement defines a new table called Directory with two columns:
3232
33-
The first step in ordering table is the SELECT
34-
ORDER BY helps you to arrange data in readable form
35-
Here FROM specify the table we are selecting from ountries.
36-
Here the query is ordering the number by name/.
33+
>`floor` — stores the floor number as an `INTEGER`
3734
35+
>`company` — stores the company name as `TEXT`
3836
:::info
3937
<Tabs>
40-
<TabItem value="SQL Table" label="SQL Table">
41-
```sql title="Friends"
42-
| name | username|
43-
-----------|---------|
44-
Smith | 19 |
45-
Jones | 60 |
46-
Wilson | 25 |
47-
```
48-
</TabItem>
4938

5039
<TabItem value="SQL Code" label="SQL Code">
5140

5241
```sql title="Creating SQL Tables & db. "
5342

54-
-- creating orders
55-
SELECT * FROM patients
56-
ORDER BY name;
57-
43+
CREATE TABLE Directory (
44+
floor INTEGER,
45+
company TEXT
46+
);
5847
```
59-
60-
61-
62-
</TabItem>
63-
64-
<TabItem value="how-git-works" label="Output">
65-
| name | age |
66-
-------------------|--------------------------|
67-
Smith | 19 |
68-
Jones | 60 |
69-
Wilson | 25 |
70-
</TabItem>
71-
</Tabs>
72-
73-
74-
:::
75-
76-
77-
### Example Practice
78-
79-
> To query data from a table, use the FROM clause followed by the table's name.
80-
81-
82-
For example, consider a table named `Friends`. Below is how a simple table might look:
83-
84-
85-
86-
:::info
87-
<Tabs>
88-
<TabItem value="SQL Table" label="SQL Table">
89-
```sql title="Friends"
90-
| name | gdp |
91-
|---------|---------|
92-
| Greece | 187.46 |
93-
| Sweden | 474.15 |
94-
| Iceland | 21.6 |
95-
| Germany | 3449.05 |
96-
97-
```
98-
</TabItem>
99-
100-
<TabItem value="SQL Code" label="SQL Code">
101-
102-
```sql title="Creating SQL Tables. "
103-
SELECT *
104-
FROM countries
105-
ORDER BY name;
106-
```
107-
10848
</TabItem>
10949

11050
<TabItem value="how-git-works" label="Output">
111-
| name | gdp |
112-
|---------|---------|
113-
| Germany | 3449.05 |
114-
| Greece | 187.46 |
115-
| Iceland | 21.6 |
116-
| Sweden | 474.15 |
117-
51+
Query OK, table created successfully.
11852
</TabItem>
11953
</Tabs>
12054

@@ -124,117 +58,120 @@ ORDER BY name;
12458

12559

12660
:::tip
127-
When requesting data with SQL staments like SELECT, we say that we are making a query.
128-
From helps in select the name col from
129-
While not necessary but its a good practice to finish the sql queries with;
130-
61+
✅ Tip: SQL keywords like CREATE TABLE, INTEGER, and TEXT are not case-sensitive, but using uppercase for SQL keywords improves readability.
13162

132-
By following these best practices,
13363
:::
13464

135-
### 🔄 Arranging ORDERS BY with ASC , DESC
65+
### 🧾 Inserting Data into a Table
13666

13767
:::info
138-
Ordering text properties like name is different when comparing to the age
139-
We can order items in asseding starting with smalest value or deceding.
68+
Once a table structure is created using CREATE TABLE, the next step is to insert data into it. You can insert as many rows as you want, at any time.
69+
70+
We’ll continue with the Directory table from earlier. Let’s insert a couple of company records.
14071
<Tabs>
141-
<TabItem value="SQL Table" label="SQL Table">
142-
```sql title="friends"
143-
| name | age |
144-
|--------|-----|
145-
| Smith | 19 |
146-
| Jones | 60 |
147-
| Wilson | 25 |
14872

149-
```
150-
</TabItem>
15173

15274
<TabItem value="SQL Code" label="SQL Code">
15375

15476
```sql title="Creating SQL Tables. "
155-
SELECT *
156-
FROM patients
157-
ORDER BY age ASC;
77+
CREATE TABLE Directory (
78+
floor INTEGER,
79+
company TEXT
80+
);
81+
82+
INSERT INTO Directory (floor, company)
83+
VALUES (1, 'Acme Inc.');
84+
85+
INSERT INTO Directory (floor, company)
86+
VALUES (2, 'Homeflix');
15887
15988
```
16089

16190
</TabItem>
16291

16392
<TabItem value="how-git-works" label="Output">
164-
| name | age |
165-
|--------|-----|
166-
| Smith | 19 |
167-
| Wilson | 25 |
168-
| Jones | 60 |
93+
| floor | company |
94+
|-------|-----------|
95+
| 1 | Acme Inc. |
96+
| 2 | Homeflix |
16997

17098
</TabItem>
17199
</Tabs>
172100

173101

174102
:::
175103

176-
## 🧹 Selecting with `DESC`
177-
178-
For Text value it order by Alphabetically
179-
When arranging numerical value the item with smallest value in that coloumn comes first
180-
181-
---
182-
183-
:::info
184-
<Tabs>
185-
<TabItem value="SQL Table" label="SQL Table">
186-
```sql title="Subscribers"
187-
| name | age |
188-
|--------|-----|
189-
| Smith | 19 |
190-
| Jones | 60 |
191-
| Wilson | 25 |
192-
104+
:::tip
105+
✅ Tip: Always match the order of columns in your INSERT INTO statement with the order of values inside VALUES().
106+
107+
🚫 Avoiding Duplicate Table Creation
108+
When running a CREATE TABLE command, trying to create a table that already exists will usually result in an error. To avoid this, we can use the IF NOT EXISTS clause.
109+
```sql title="Creating SQL Tables.
110+
CREATE TABLE IF NOT EXISTS Directory (
111+
floor INTEGER,
112+
company TEXT
113+
);
193114
```
194-
</TabItem>
195-
196-
<TabItem value="SQL Code" label="SQL Code">
197-
198-
```sql title="Creating SQL Tables. "
199-
SELECT *
200-
FROM patients
201-
ORDER BY age DESC;
202-
203-
```
204-
205-
</TabItem>
206-
207-
<TabItem value="how-git-works" label="Output">
208-
| name | age |
209-
|--------|-----|
210-
| Jones | 60 |
211-
| Wilson | 25 |
212-
| Smith | 19 |
213-
214-
215-
</TabItem>
216-
</Tabs>
115+
:::
217116

218117

219-
:::
118+
# ✅ What You Have Learned
220119

120+
In this module, you learned the fundamentals of creating tables in SQL, including:
221121

122+
- **Defining Table Structure**
123+
How to use the `CREATE TABLE` statement to define a new table and specify columns with appropriate data types.
222124

125+
- **Common Data Types**
126+
The purpose of data types like `INTEGER` for numbers and `TEXT` for strings.
223127

224-
# ✅ What You Have Learned
128+
- **Inserting Data**
129+
How to add rows to a table using the `INSERT INTO` statement.
225130

226-
This module covers essential concepts related to ordering data in SQL:
131+
- **Preventing Duplicate Tables**
132+
Using `IF NOT EXISTS` with `CREATE TABLE` to avoid errors if the table already exists.
227133

228-
- **ORDER BY Clause**
229-
Learn how to sort query results based on one or more columns using the `ORDER BY` keyword.
134+
- **Best Practices**
135+
Writing SQL keywords in uppercase for readability and matching column order in `INSERT INTO` statements.
230136

231-
- **Ascending Order** (ASC)
232-
Understand that SQL sorts in ascending order by default, which is the same as explicitly using `ASC`.
233137

234-
- **Descending Order** (DESC)
235-
Use the `DESC` keyword to sort results from highest to lowest or reverse alphabetical order.
138+
---
236139

237-
- **Sort by Column Values**
238-
Practice sorting by numeric or text column values to organize data meaningfully.
140+
## 📝 Quiz: Test Your Knowledge
141+
142+
#### 1. How can you ensure a table is only created if it doesn't already exist, to avoid errors?
143+
<details>
144+
<summary>Answer</summary>
145+
<ul>
146+
<li>By adding <code>IF NOT EXISTS</code> to the <code>CREATE TABLE</code> statement:</li>
147+
</ul>
148+
149+
```sql
150+
CREATE TABLE IF NOT EXISTS Directory (
151+
floor INTEGER,
152+
company TEXT
153+
);
154+
```
155+
</details>
156+
#### 2. Complete the column definition for the `Tickets` table creation syntax with the appropriate column data types.
157+
```sql
158+
CREATE TABLE Tickets (
159+
qty _____,
160+
email _____
161+
);
162+
```
163+
<details>
164+
<summary>Answer</summary>
165+
<ul>
166+
<li>Specify <code>qty</code> as <code>INTEGER</code> and <code>email</code> as <code>TEXT</code>:</li>
167+
</ul>
168+
169+
```sql
170+
CREATE TABLE Tickets (
171+
qty INTEGER,
172+
email TEXT
173+
);
174+
```
175+
</details>
239176

240177
---

0 commit comments

Comments
 (0)