Skip to content

Commit 02f027d

Browse files
authored
Update README.md with more queries examples
1 parent 220acea commit 02f027d

File tree

1 file changed

+63
-7
lines changed

1 file changed

+63
-7
lines changed

README.md

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,75 @@ Note 2: At the time of viewing this, I may still be in the process of developing
5050

5151
## Queries examples
5252

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+
CREATE DATABASE logistics_email;
61+
62+
USE logistics_email;
63+
64+
CREATE TABLE emails (
65+
email_id INT AUTO_INCREMENT PRIMARY KEY,
66+
recipient VARCHAR(255) NOT NULL,
67+
subject VARCHAR(255) NOT NULL,
68+
body TEXT NOT NULL,
69+
sent_date DATE NOT NULL
70+
);
71+
```
72+
73+
2. Insert values to those tables:
5474

5575
```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');
5783
```
5884

59-
Query anatomy:
85+
3. Update the email with the subject "Package Delivery" to "Package Delivery Update":
6086

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+
```
6592

93+
4. Add a new column called "status" with "Pending" as its default value:
94+
95+
```sql
96+
ALTER TABLE 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)
120+
GROUP BY recipient;
121+
```
66122

67123
## Table of Contents
68124

0 commit comments

Comments
 (0)