-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMpExam2019_School_DataBase.sql
183 lines (130 loc) · 3.75 KB
/
MpExam2019_School_DataBase.sql
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
CREATE DATABASE School;
USE School;
-- create table our School Madhyamik Batch(2019).
CREATE TABLE MpBatch_2019( -- U can create more col for more info.
rollNo INT PRIMARY KEY,
name VARCHAR(50),
marks INT NOT NULL,
grade VARCHAR(3),
vill VARCHAR(50)
);
INSERT INTO MpBatch_2019
(rollNo,name,marks,grade, vill)
VALUES
(1,"Debu",84,"A", "Santipur"),
(2,"Kuntal",95,"A+", "Pandugram"),
(3,"Arpan",99,"A++","Bhatshala"),
(4,"Subho",91,"A+","Bhatshala"),
(5,"Chitra",98,"A++","Bhatshala"),
(6,"Kiran",70,"B","Jharia"),
(7,"Santosh",90,"A+","Jharia");
DROP TABLE MpBatch_2019; -- i need to run this line coz i have to change the grade char size.
-- so, i only show the students name and their marks from the Table.
SELECT * FROM MpBatch_2019; -- when i write astrarick means select all col. Print the whole table data.
SELECT name, marks FROM MpBatch_2019;
SELECT DISTINCT marks FROM MpBatch_2019; -- DISTINCT Shows me the unique value.
SELECT * FROM MpBatch_2019 WHERE marks>90; -- Use here WHERE Clause
-- In "WHERE" Clause there are multiple Operators.
SELECT *
FROM MpBatch_2019
WHERE marks+10 >100;
-- Comparision Operator.
SELECT *
FROM MpBatch_2019
WHERE marks < 90;
-- logical Operator.
SELECT *
FROM MpBatch_2019
WHERE marks < 90 AND grade = "B";
-- If we select data in particular Range.
SELECT *
FROM MpBatch_2019
WHERE marks BETWEEN 84 AND 90;
-- IN and NOT IN Caluse.
SELECT *
FROM MpBatch_2019
WHERE grade IN ("A++");
-- NOT IN
SELECT *
FROM MpBatch_2019
WHERE grade NOT IN ("A++");
-- Limit Clause. In here we also use WHERE Clause.
SELECT *
FROM MpBatch_2019
WHERE marks > 90
LIMIT 3;
-- Order By Clause.
-- Some times we need data Acendeing order or may be Decending Order.
SELECT *
FROM MpBatch_2019
ORDER BY name ASC;
-- I want top 3 student in our school.
SELECT *
FROM MpBatch_2019
ORDER BY marks DESC
LIMIT 3;
-- FUNCTION ARE START FROM HERE "AGGREGATE FUNCTION" -----
-- Calculate the maximum marks in my table.
SELECT MAX(marks)
FROM MpBatch_2019; -- same way cal the MINI
-- Calculate the Avrage Marks in Our Batch.
SELECT AVG(marks)
FROM MpBatch_2019;
-- Cal how many student in our batch.
SELECT COUNT(name)
FROM MpBatch_2019;
-- GROUP BY Clause---
SELECT vill , COUNT(name) -- Now i want to know how many students are learning from a particular village.
FROM MpBatch_2019 -- So , i find out how many student are learning base on Name
GROUP BY vill;
SELECT vill ,name, COUNT(name) -- You can pass multiple varriable.
FROM MpBatch_2019
GROUP BY vill, name;
-- Average marks in each village.
SELECT vill , AVG(marks)
FROM MpBatch_2019
GROUP BY vill;
-- Having Caluse
-- Similar as WHERE Clause apply condition on row.
-- Q: Count the number of student in each village where maximum number is cross 90
SELECT vill , count(rollNo)
FROM MpBatch_2019
GROUP BY vill
HAVING MAX(marks>90);
-- Some General Order .
-- SELECT -> FROM -> WHERE -> GROUP BY -> HAVING -> ORDER BY (ASC)
SELECT vill
FROM MpBatch_2019
WHERE grade = "A+"
GROUP BY vill
HAVING MAX(marks>90)
ORDER BY vill DESC;
-- Table Related Quries.
-- UPDATE
-- SET
-- WHERE
SET SQL_SAFE_UPDATES = 0;
UPDATE MpBatch_2019
SET grade = "O"
WHERE grade = "A++";
-- Suppose rollNumber(6) Review and his marks got increase so, i will update his marks in my database.
UPDATE MpBatch_2019
SET marks = 88
WHERE rollNo = 6;
UPDATE MpBatch_2019
SET grade = "A+"
WHERE rollNo = 6;
UPDATE MpBatch_2019
SET grade = "B"
WHERE marks BETWEEN 79 AND 89;
-- Like in exam 1 question is wrong so, School decided that increase each student marks 1.
UPDATE MpBatch_2019
SET marks = marks+1;
-- DELETE QUEARY
DELETE FROM MpBatch_2019
WHERE marks <30;
-- no one contain marks 30 , so i update anyone marks.
UPDATE MpBatch_2019
SET marks = 25
WHERE rollNo = 6;
SELECT * FROM MpBatch_2019;