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
Copy file name to clipboardExpand all lines: README.md
+63-7Lines changed: 63 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -50,19 +50,75 @@ Note 2: At the time of viewing this, I may still be in the process of developing
50
50
51
51
## Queries examples
52
52
53
-
Retrieves all data from table in a database:
53
+
Sample use case:
54
+
55
+
You are working for a logistics company that sends emails to customers to inform them about the status of their packages. The company has a database of all the emails sent, which includes the email ID, recipient email address, subject, body, and sending date.
56
+
57
+
1. Create the database and its tables:
58
+
59
+
```sql
60
+
CREATEDATABASElogistics_email;
61
+
62
+
USE logistics_email;
63
+
64
+
CREATETABLEemails (
65
+
email_id INT AUTO_INCREMENT PRIMARY KEY,
66
+
recipient VARCHAR(255) NOT NULL,
67
+
subject VARCHAR(255) NOT NULL,
68
+
body TEXTNOT NULL,
69
+
sent_date DATENOT NULL
70
+
);
71
+
```
72
+
73
+
2. Insert values to those tables:
54
74
55
75
```sql
56
-
SELECT*FROM table_name;
76
+
INSERT INTO emails (recipient, subject, body, sent_date)
77
+
VALUES
78
+
('recipient1@email.com', 'Package Update', 'Your package has been shipped', '2022-01-01'),
79
+
('recipient2@email.com', 'Package Delivery', 'Your package has been delivered', '2022-01-02'),
80
+
('recipient3@email.com', 'Package Delay', 'Your package has been delayed', '2022-01-03'),
81
+
('recipient4@email.com', 'Package Update', 'Your package is on its way', '2022-01-04'),
82
+
('recipient5@email.com', 'Package Arrival', 'Your package has arrived', '2022-01-05');
57
83
```
58
84
59
-
Query anatomy:
85
+
3. Update the email with the subject "Package Delivery" to "Package Delivery Update":
60
86
61
-
-`SELECT` keyword retrieve data from a database.
62
-
-`*` symbol is used to select all columns in the table.
63
-
-`FROM` keyword specifies the name of the table from which the data will be retrieved.
64
-
-`table_name` is the name of the table in the database.
87
+
```sql
88
+
UPDATE emails
89
+
SET subject ='Package Delivery Update'
90
+
WHERE email_id =2;
91
+
```
65
92
93
+
4. Add a new column called "status" with "Pending" as its default value:
94
+
95
+
```sql
96
+
ALTERTABLE emails
97
+
ADD COLUMN status VARCHAR(20) NOT NULL DEFAULT 'Pending';
98
+
```
99
+
100
+
5. Retrieve the number of emails sent to each recipient email address and the average length of the email bodies:
101
+
102
+
```sql
103
+
SELECT
104
+
recipient,
105
+
COUNT(email_id) as total_emails,
106
+
AVG(LENGTH(body)) as avg_body_length
107
+
FROM emails
108
+
GROUP BY recipient;
109
+
```
110
+
111
+
6. Retrieve the number of emails sent to each recipient email address and the average length of the email bodies only for the emails sent in the last week:
112
+
113
+
```sql
114
+
SELECT
115
+
recipient,
116
+
COUNT(email_id) as total_emails,
117
+
AVG(LENGTH(body)) as avg_body_length
118
+
FROM emails
119
+
WHERE sent_date >= DATE_SUB(NOW(), INTERVAL 7 DAY)
0 commit comments