Repository navigation
Expand file tree
/
Copy pathquery.sql
More file actions
129 lines (88 loc) · 3.13 KB
/
Copy pathquery.sql
File metadata and controls
129 lines (88 loc) · 3.13 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
### Access phpMyAdmin
-- 1. **Start MySQL**
-- 2. Open: `http://localhost/phpmyadmin`
CREATE DATABASE IF NOT EXISTS school_db;
USE school_db;
CREATE TABLE IF NOT EXISTS students (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
age INT,
grade VARCHAR(10),
email VARCHAR(100) unique
);
INSERT INTO students (name, age, grade, email)
VALUES ('John Doe', 16, '10th', 'john.doe@email.com');
INSERT INTO students (name, age, grade, email)
VALUES ('Jane Smith', 17, '11th', 'jane.smith@email.com');
INSERT INTO students (name, age, grade, email)
VALUES ('Mike Johnson', 15, '9th', 'mike.j@email.com'),
('Emily Davis', 16, '10th', 'emily.d@email.com'),
('Sarah Wilson', 18, '12th', 'sarah.w@email.com');
INSERT INTO students (name, age, grade, email) VALUES
('Alex Brown', 17, '11th', 'alex.b@email.com'),
('Lisa Martinez', 15, '9th', 'lisa.m@email.com');
SELECT * FROM students;
SELECT name, grade FROM students;
SELECT * FROM students WHERE grade = '10th';
SELECT * FROM students WHERE age > 16;
SELECT * FROM students ORDER BY name ASC;
SELECT * FROM students ORDER BY age DESC;
UPDATE students
SET email = 'john.newemail@email.com'
WHERE id = 1;
UPDATE students
SET grade = '11th'
WHERE name = 'John Doe';
UPDATE students
SET age = 17, grade = '11th'
WHERE id = 3;
DELETE FROM students WHERE id = 5;
DELETE FROM students WHERE age < 15;
-- ==========================================
-- PART 2: QUERYING DATA (DQL)
-- Retrieving specific information
-- ==========================================
-- 1. Basic Selects
SELECT * FROM students; -- Get all columns and all rows
SELECT name, email FROM students; -- Get only specific columns
-- 2. Filtering with WHERE
-- Exact match
SELECT * FROM students WHERE age = 17;
-- Comparisons (> , < , >= , <= , !=)
SELECT * FROM students WHERE age > 15;
-- Logic (AND, OR)
SELECT * FROM students
WHERE age >= 15 AND age <= 25;
-- Pattern Matching (LIKE)
-- % matches any number of characters
SELECT * FROM students WHERE name LIKE 'j%'; -- Names starting with 'J' (John, Jane)
SELECT * FROM students WHERE email LIKE '%@email.com'; -- Specific domain
-- 3. Sorting & Limits
-- Order By (ASC is default, DESC is reverse)
SELECT * FROM students ORDER BY age DESC;
-- Limit (Good for pagination or just peeking at data)
SELECT * FROM students LIMIT 2;
-- Combining them: "Get the 3 youngest students"
SELECT * FROM students ORDER BY age ASC LIMIT 3;
SELECT COUNT(*) AS total_students FROM students;
SELECT AVG(age) AS average_age FROM students;
SELECT MAX(age) AS oldest_age FROM students;
-- Add a new column (e.g., for phone numbers)
ALTER TABLE students ADD phone VARCHAR(15);
-- Modify an existing column
ALTER TABLE students MODIFY name VARCHAR(150);
-- 1. Find user by email
SELECT * FROM users WHERE email = 'john@example.com';
-- 2. Clean up students without email
DELETE FROM students WHERE email IS NULL;
-- 1. Average age
SELECT AVG(age) AS average_age FROM students;
-- 2. Count per grade
SELECT grade, COUNT(*) AS student_count
FROM students
GROUP BY grade
ORDER BY grade;
-- 3. Alphabetical list
SELECT name, age, grade
FROM students
ORDER BY name ASC;