-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql code.txt
227 lines (186 loc) · 8.59 KB
/
mysql code.txt
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
CREATE TABLE worker (
emp_no INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(14) NOT NULL,
last_name VARCHAR(16) NOT NULL,
email VARCHAR(35) NOT NULL,
department VARCHAR(30) NOT NULL,
Salary INT NOT NULL,
join_date DATE NOT NULL
);
select * from worker
INSERT INTO worker (first_name, last_name, email, department, Salary, join_date)
VALUES
('James','Smith', '[email protected]','Geoinformatics',45000, '2019-10-03'),
('Christopher','Anderson', '[email protected]','Geoinformatics', 25000, '2019-11-07'),
('Ronald','Clark', '[email protected]','ECE', 27000, '2019-12-12'),
('Kamal', 'raja', '[email protected]', 'Automobile', 35000, '2019-12-25'),
('Ramesh', 'Kumar', '[email protected]', 'Thermal', 60000, '2020-01-20'),
('Michelle','Johnson', '[email protected]','Geoinformatics', 70000, '2020-03-03'),
('John' ,'Thomas', '[email protected]', 'Structural', 55000, '2020-05-16'),
('Daniel', 'Rodriguez', '[email protected]', 'Petroleum', 57000, '2020-07-01'),
('Anthony', 'Lopez', '[email protected]', 'Structural', 78000, '2020-08-20'),
('Robert', 'Lewis', '[email protected]', 'Geoinformatics',85000, '2020-03-10'),
('Santhosh', 'Narayan', '[email protected]', 'Structural', 81700, '2021-03-25'),
('David', 'Miller', '[email protected]', 'MBA', 137000, '2021-01-12'),
('Chris', 'Evans', '[email protected]', 'Petroleum', 120000, '2021-02-28'),
('Owen', 'Wilson', '[email protected]', 'MCA', 64000, '2021-06-08'),
('Margaret', 'Edwards', '[email protected]', 'MCA', 73000, '2022-08-26'),
('John', 'Doe', '[email protected]', 'ECE', 45000, '2018-10-01'),
('Mary', 'Smith', '[email protected]', 'Thermal', 55000, '2018-10-08'),
('William', 'Jones', '[email protected]', 'Automobile', 65000, '2018-10-15'),
('Emily', 'Davis', '[email protected]', 'Geoinformatics', 75000, '2018-10-22'),
('Robert', 'Brown', '[email protected]', 'ECE', 85000, '2018-10-29'),
('Sarah', 'Wilson', '[email protected]', 'Thermal', 95000, '2018-11-05'),
('Thomas', 'Lee', '[email protected]', 'Geoinformatics', 105000, '2018-11-12'),
('Olivia', 'Green', '[email protected]', 'Automobile', 115000, '2018-11-19'),
('Michael', 'Taylor', '[email protected]', 'ECE', 125000, '2018-11-26'),
('Sophia', 'Adams', '[email protected]', 'Thermal', 135000, '2018-12-03'),
('David', 'Hall', '[email protected]', 'Geoinformatics', 145000, '2018-12-10'),
('Isabella', 'Young', '[email protected]', 'Automobile', 155000, '2018-12-17'),
('James', 'Robinson', '[email protected]', 'ECE', 165000, '2018-12-24'),
('Emma', 'Allen', '[email protected]', 'Thermal', 175000, '2018-12-31'),
('Alexander', 'King', '[email protected]', 'Geoinformatics', 185000, '2019-01-07'),
('Jacob', 'Turner', '[email protected]', 'Automobile', 72000, '2023-01-01'),
('Ava', 'Carter', '[email protected]', 'Thermal', 83000, '2023-01-08'),
('Logan', 'Parker', '[email protected]', 'Geoinformatics', 94000, '2023-01-15'),
('Charlotte', 'Bell', '[email protected]', 'ECE', 105000, '2023-01-22'),
('Benjamin', 'Phillips', '[email protected]', 'Automobile', 116000, '2023-01-29'),
('Liam', 'Mitchell', '[email protected]', 'Thermal', 127000, '2023-02-05'),
('Mia', 'Kelly', '[email protected]', 'Geoinformatics', 138000, '2023-02-12'),
('William', 'Bailey', '[email protected]', 'ECE', 149000, '2023-02-19'),
('Evelyn', 'Cooper', '[email protected]', 'Automobile', 160000, '2023-02-26'),
('James', 'Richardson', '[email protected]', 'Thermal', 171000, '2023-03-05'),
('Sophia', 'Bryant', '[email protected]', 'Geoinformatics', 182000, '2023-03-12'),
('Logan', 'Henderson', '[email protected]', 'ECE', 193000, '2023-03-19'),
('Avery', 'Barnes', '[email protected]', 'Automobile', 204000, '2023-03-26'),
('Jackson', 'Gonzalez', '[email protected]', 'Thermal', 215000, '2023-04-02'),
('Aria', 'Fisher', '[email protected]', 'Geoinformatics', 226000, '2023-04-09'),
('Lucas', 'Coleman', '[email protected]', 'ECE', 237000, '2023-04-16'),
('Scarlett', 'Marshall', '[email protected]', 'Automobile', 248000, '2023-04-23'),
('William', 'Simpson', '[email protected]', 'Thermal', 259000, '2023-04-30'),
('Victoria', 'Fox', '[email protected]', 'Geoinformatics', 270000, '2023-05-07'),
('Ethan', 'Burke', '[email protected]', 'ECE', 281000, '2023-05-14')
('Robert', 'Peterson', '[email protected]','Thermal',58000, '2019-10-05');
CREATE TABLE city (
id INT NOT NULL,
address VARCHAR(20) NOT NULL
);
INSERT INTO city (id, address)
VALUES
('3', 'Chennai'),
('5', 'Delhi'),
('8', 'Ranchi');
CREATE TABLE worker2 (
emp_no INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(14) NOT NULL,
last_name VARCHAR(16) NOT NULL,
email VARCHAR(35) NOT NULL,
department VARCHAR(30) NOT NULL,
salary INT NOT NULL,
join_date DATE NOT NULL
);
INSERT INTO worker2 (first_name, last_name, email, department,salary,join_date)
VALUES
('Steve', 'Smith', '[email protected]','Geoinformatics',46000, '2020-10-03'),
('Corey','Anderson', '[email protected]','MCA', 75000, '2019-11-07'),
('Ronald','Clark', '[email protected]','ECE', 57000, '2019-12-12')
Task-1:
1.Select first_name as worker_name from worker;
2.Select DISTINCT department
from worker;
3.(Select * from worker
order by emp_no
DESC limit 5)
Order by emp_no ASC;
Task-2:
1.Select substring(first_name,1,3)
from worker;
2.Select first_name, position('a' IN first_name)
from worker;
3.SELECT emp_no, department, MAX(salary)
FROM worker
GROUP BY department;
Task-3:
1.Select RTRIM(first_name)
from worker;
2.Select DISTINCT department, LENGTH(department)
from worker;
3.Select DISTINCT salary
from worker a
where 5 >= (Select count(distinct Salary)
from worker b
where a.salary <= b.salary) order by a.Salary desc;
3.WITH RESULT AS
(
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS DENSERANK
FROM worker
)
SELECT salary
FROM RESULT
WHERE DENSERANK = 3;
Task-4:
1.Select REPLACE(first_name,'a','A') from worker;
2.Select * from worker order by first_name asc, department desc;
3.Select first_name, salary from worker
where
salary = (select max(salary) from worker);
Task-5:
1.Select * from worker
where first_name NOT IN ('Ramesh', 'Santhosh');
2.SELECT * FROM worker WHERE first_name LIKE '_____h';
3.Select first_name, email from worker
where email like '%guvi.com';
Task-6:
1.Select * from worker
where year(join_date) = 2021 and month(join_date) = 3;
3.WITH CTE AS (SELECT *,ROW_NUMBER()
OVER (PARTITION BY first_name,last_name ORDER BY first_name,last_name) AS RN FROM employees.worker)
DELETE FROM CTE WHERE RN > 1;
3.delete from worker
where emp_no in ( select * from
(select max(emp_no) from worker group by first_name having count(*)>1) as t);
2.Select department, first_name, COUNT(*)
FROM worker
GROUP BY department, first_name
HAVING count(*) > 1;
Task-7:
2.Create table worker_2
like worker;
insert into worker_2
select * from worker;
1.Select * from worker
where mod(emp_no,2)<>0;
Task-8:
1.Select emp_no, first_name,last_name
from worker
left join city
on worker.emp_no = city.id
INTERSECT
Select emp_no, first_name,last_name
from worker
right join city
on worker.emp_no = city.id;
2. SELECT * FROM worker2
WHERE first_name NOT IN
(SELECT first_name
FROM worker);
Task-9:
2.SELECT first_name,last_name, Salary
FROM Worker
ORDER BY Salary DESC
LIMIT 5,1;
1.SELECT first_name,last_name, Salary FROM Worker ORDER BY Salary DESC LIMIT 14;
Task-10:
1.SELECT first_name, last_name, salary
FROM worker w1
WHERE 8 = (
SELECT COUNT( DISTINCT ( w2.salary ) )
FROM worker w2
WHERE w2.salary >= w1.salary);
2.Select distinct W.emp_no, W.FIRST_NAME, W.Salary from Worker W, Worker W1
where W.Salary = W1.Salary
and W.emp_no != W1.emp_no;
1.SELECT Salary FROM (SELECT Salary, RANK() OVER (ORDER BY Salary DESC) as salary_rank
FROM worker
) ranked_salaries
WHERE salary_rank = 1;