-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexerciseClausesAlter.sql
More file actions
executable file
·246 lines (161 loc) · 5.01 KB
/
exerciseClausesAlter.sql
File metadata and controls
executable file
·246 lines (161 loc) · 5.01 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
USE exercisedb;
CREATE TABLE dept_details(
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);
INSERT INTO dept_details VALUES
(1, 'IT'),
(2, 'HR'),
(3, 'Finance'),
(4, 'Marketing'),
(5, 'Operations');
CREATE TABLE emp_details(
emp_id INT PRIMARY KEY,
name VARCHAR(50),
dept_id INT,
salary INT,
age INT,
joining_date DATE,
FOREIGN KEY (dept_id) REFERENCES dept_details(dept_id)
);
INSERT INTO emp_details VALUES
(101, 'Amit', 1, 60000, 30, '2021-06-10'),
(102, 'Sneha', 2, 45000, 28, '2022-01-15'),
(103, 'Rahul', 1, 70000, 35, '2020-03-20'),
(104, 'Anita', 3, 50000, 32, '2019-11-01'),
(105, 'Vikas', 4, 30000, 25, '2023-02-12'),
(106, 'Rohit', 1, 55000, 29, '2021-08-05'),
(107, 'Neha', 2, 40000, 26, '2022-09-30'),
(108, 'Suresh', NULL, 48000, 31, '2020-07-18');
##################(WHERE / ORDER BY / LIMIT)#############################
## Display all employees earning more than 40,000.
SELECT *
FROM emp_details
WHERE salary > 40000;
## Show employees working in the IT department.
SELECT *
FROM emp_details
WHERE dept_id = (SELECT dept_id FROM dept_details WHERE dept_name = "IT");
## List employee names and salaries sorted by salary descending.
SELECT name, salary
FROM emp_details
ORDER BY salary DESC;
## Display the top 3 highest-paid employees.
SELECT *
FROM emp_details
ORDER BY salary DESC
LIMIT 3;
#############(DISTINCT / BETWEEN / IN / LIKE)#####################
## Display distinct department IDs from Employees.
SELECT DISTINCT dept_id
FROM emp_details;
## Find employees whose salary is between 35,000 and 60,000.
SELECT *
FROM emp_details
WHERE salary BETWEEN 35000 AND 60000;
## Find employees working in dept_id 1 or 3.
SELECT *
FROM emp_details
WHERE dept_id IN (1,3);
## Display employees whose name starts with ‘A’.
SELECT *
FROM emp_details
WHERE name LIKE 'A%';
## Display employees whose name contains ‘h’.
SELECT *
FROM emp_details
WHERE name LIKE '%h%';
######################(AGGREGATE FUNCTIONS)############################
## Find the highest salary in the company.
SELECT MAX(salary) AS max_salary
FROM emp_details;
## Find the average salary of all employees.
SELECT AVG(salary) AS avg_salary
FROM emp_details;
## Find the total salary paid to employees older than 30.
SELECT SUM(salary) AS total_salary
FROM emp_details
WHERE age > 30;
####################(GROUP BY & HAVING)############################
## Find the average salary of each department.
SELECT dept_id, AVG(salary) AS avg_salary
FROM emp_details
GROUP BY dept_id;
## Count the number of employees in each department.
SELECT dept_id, COUNT(*) AS total_emp
FROM emp_details
GROUP BY dept_id;
## Display departments where the average salary is greater than 50,000.
SELECT dept_id, AVG(salary) AS avg_salary
FROM emp_details
GROUP BY dept_id
HAVING avg_salary > 50000;
## Show departments having more than 2 employees.
SELECT dept_id, COUNT(*) AS emp
FROM emp_details
GROUP BY dept_id
HAVING emp > 2;
#######################(SUBQUERIES)#############################
## Find employees earning more than the company average salary.
SELECT *
FROM emp_details
WHERE salary > (SELECT AVG(salary) FROM emp_details);
## Find employees earning more than the average salary of their own department.
SELECT *
FROM emp_details e
WHERE salary > (SELECT AVG(salary) FROM emp_details WHERE dept_id=e.dept_id);
###############################################################
CREATE TABLE students (
student_id INT,
name VARCHAR(50),
age INT,
email VARCHAR(100)
);
INSERT INTO students VALUES
(1, 'Ravi', 20, 'ravi@gmail.com'),
(2, 'Priya', 22, 'priya@gmail.com'),
(3, 'Kunal', 17, 'kunal@gmail.com'),
(4, 'Neha', 21, 'neha@gmail.com');
################(ALTER – ADD COLUMN)##################
## Add a column phone_number of type VARCHAR(10).
ALTER TABLE students
ADD phone_number VARCHAR(10);
SELECT * FROM students;
## Add a column dob of type DATE.
ALTER TABLE students
ADD dob DATE;
SELECT * FROM students;
###################(ALTER – MODIFY / ALTER COLUMN)################
## Change the datatype of phone_number to VARCHAR(15).
ALTER TABLE students
MODIFY phone_number VARCHAR(15);
SELECT * FROM students;
## Modify the age column to NOT NULL.
ALTER TABLE students
MODIFY age INT NOT NULL;
SELECT * FROM students;
## Increase the size of the name column to VARCHAR(100).
ALTER TABLE students
MODIFY name VARCHAR(100);
SELECT * FROM students;
#################(ALTER – ADD CONSTRAINT)#####################
SHOW CREATE TABLE students;
## Add a PRIMARY KEY on student_id.
ALTER TABLE students
ADD PRIMARY KEY(student_id);
SELECT * FROM students;
DESC students;
## Add a UNIQUE constraint on email.
ALTER TABLE students
ADD CONSTRAINT UQ_email UNIQUE (email);
## Add a CHECK constraint to ensure age ≥ 18.
ALTER TABLE students
ADD CONSTRAINT CHECK(age >= 18);
#########################(ALTER – DROP)####################################
## Drop the dob column.
ALTER TABLE students
DROP dob;
SELECT * FROM students;
## Remove the CHECK constraint on age.
ALTER TABLE student
DROP CONSTRAINT student_chk_1;