-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14_Statements.sql
More file actions
136 lines (117 loc) · 3.36 KB
/
14_Statements.sql
File metadata and controls
136 lines (117 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**************************************************************
* MySQL 8.0 Statements Tutorial
* Description: This script demonstrates how indexes support
* common query predicates in MySQL and reviews the
* practical effect of filtering, grouping, joining,
* and sorting.
**************************************************************/
-------------------------------------------------
-- Region: 0. Initialization
-------------------------------------------------
USE mysql_course;
DROP TABLE IF EXISTS other_data;
DROP TABLE IF EXISTS sample_data;
CREATE TABLE sample_data
(
id INT PRIMARY KEY,
person_name VARCHAR(100) NOT NULL,
age INT NOT NULL,
created_date DATETIME NOT NULL
) ENGINE = InnoDB;
CREATE TABLE other_data
(
id INT PRIMARY KEY,
description_text VARCHAR(100) NOT NULL
) ENGINE = InnoDB;
CREATE INDEX ix_sample_data_person_name ON sample_data (person_name);
CREATE INDEX ix_sample_data_age ON sample_data (age);
CREATE INDEX ix_sample_data_created_date ON sample_data (created_date);
INSERT INTO sample_data (id, person_name, age, created_date)
VALUES
(1, 'Alice', 30, '2023-01-01 09:00:00'),
(2, 'Bob', 25, '2023-02-01 09:00:00'),
(3, 'Charlie', 35, '2023-03-01 09:00:00'),
(4, 'David', 40, '2023-04-01 09:00:00'),
(5, 'Eve', 28, '2023-05-01 09:00:00');
INSERT INTO other_data (id, description_text)
VALUES
(1, 'Description 1'),
(2, 'Description 2'),
(3, 'Description 3'),
(4, 'Description 4'),
(5, 'Description 5');
-------------------------------------------------
-- Region: 1. Equality, Range, and Set Predicates
-------------------------------------------------
EXPLAIN
SELECT *
FROM sample_data
WHERE person_name = 'Alice';
EXPLAIN
SELECT *
FROM sample_data
WHERE age >= 30;
EXPLAIN
SELECT *
FROM sample_data
WHERE created_date BETWEEN '2023-01-01' AND '2023-03-31';
EXPLAIN
SELECT *
FROM sample_data
WHERE person_name IN ('Alice', 'Bob');
-------------------------------------------------
-- Region: 2. Function-Based Predicates
-------------------------------------------------
/*
These patterns are often less index-friendly because the column
value is wrapped inside another expression.
*/
EXPLAIN
SELECT *
FROM sample_data
WHERE LEFT(person_name, 1) = 'A';
EXPLAIN
SELECT *
FROM sample_data
WHERE YEAR(created_date) = 2023;
-------------------------------------------------
-- Region: 3. LIKE Patterns
-------------------------------------------------
EXPLAIN
SELECT *
FROM sample_data
WHERE person_name LIKE 'A%';
EXPLAIN
SELECT *
FROM sample_data
WHERE person_name LIKE '%e';
-------------------------------------------------
-- Region: 4. Sorting and Joining
-------------------------------------------------
SELECT *
FROM sample_data
ORDER BY age;
SELECT
sd.person_name,
od.description_text
FROM sample_data AS sd
INNER JOIN other_data AS od
ON od.id = sd.id;
-------------------------------------------------
-- Region: 5. Statement Processing Example
-------------------------------------------------
/*
The logical order remains FROM, WHERE, GROUP BY, HAVING,
SELECT, and ORDER BY.
*/
SELECT
person_name,
COUNT(*) AS row_count
FROM sample_data
WHERE age >= 30
GROUP BY person_name
HAVING COUNT(*) > 0
ORDER BY person_name;
-------------------------------------------------
-- Region: End of Script
-------------------------------------------------