You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -13,108 +13,42 @@ Welcome to the **Selecting Data** module! This foundational learning path is des
13
13
14
14
### 📘 Creating SQL Table
15
15
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
18
20
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.
21
21
22
+
>Other types like `BLOB` and `NULL` exist but are not covered in this tutorial.
22
23
23
-
For example, consider a table named `Friends`. Below is how a simple table might look:
24
24
25
-
26
-
## Task: Sort a table using the `ORDER BY` clause
25
+
## 🛠️ Example: Creating a Table
27
26
28
27
### Lesson Overview
29
28
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:
32
32
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`
37
34
35
+
>`company` — stores the company name as `TEXT`
38
36
:::info
39
37
<Tabs>
40
-
<TabItemvalue="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>
49
38
50
39
<TabItemvalue="SQL Code"label="SQL Code">
51
40
52
41
```sql title="Creating SQL Tables & db. "
53
42
54
-
-- creating orders
55
-
SELECT*FROM patients
56
-
ORDER BY name;
57
-
43
+
CREATETABLEDirectory (
44
+
floor INTEGER,
45
+
company TEXT
46
+
);
58
47
```
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
-
108
48
</TabItem>
109
49
110
50
<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.
118
52
</TabItem>
119
53
</Tabs>
120
54
@@ -124,117 +58,120 @@ ORDER BY name;
124
58
125
59
126
60
:::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, andTEXT are not case-sensitive, but using uppercase for SQL keywords improves readability.
131
62
132
-
By following these best practices,
133
63
:::
134
64
135
-
### 🔄 Arranging ORDERS BY with ASC , DESC
65
+
### 🧾 Inserting Data into a Table
136
66
137
67
:::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.
140
71
<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 |
148
72
149
-
```
150
-
</TabItem>
151
73
152
74
<TabItem value="SQL Code" label="SQL Code">
153
75
154
76
```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');
158
87
159
88
```
160
89
161
90
</TabItem>
162
91
163
92
<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 |
169
97
170
98
</TabItem>
171
99
</Tabs>
172
100
173
101
174
102
:::
175
103
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
+
);
193
114
```
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
+
:::
217
116
218
117
219
-
:::
118
+
# ✅ What You Have Learned
220
119
120
+
In this module, you learned the fundamentals of creating tables in SQL, including:
221
121
122
+
-**Defining Table Structure**
123
+
How to use the `CREATE TABLE` statement to define a new table and specify columns with appropriate data types.
222
124
125
+
-**Common Data Types**
126
+
The purpose of data types like`INTEGER` for numbers and`TEXT` for strings.
223
127
224
-
# ✅ What You Have Learned
128
+
-**Inserting Data**
129
+
How to add rows to a table using the `INSERT INTO` statement.
225
130
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.
227
133
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.
230
136
231
-
- **Ascending Order** (ASC)
232
-
Understand that SQL sorts in ascending order by default, which is the same as explicitly using `ASC`.
233
137
234
-
- **Descending Order** (DESC)
235
-
Use the `DESC` keyword to sort results from highest to lowest or reverse alphabetical order.
138
+
---
236
139
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.
0 commit comments