├── DBMS_shortcuts.txt └── DML_DBMS.txt /DBMS_shortcuts.txt: -------------------------------------------------------------------------------- 1 | SHORTCUTS: 2 | 3 | 1. For Execution of Query Press Contol+Shift+Enter; -------------------------------------------------------------------------------- /DML_DBMS.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | To Create DATABASE use "CREATE DATABASE BANK;" ---- it will create database with the name of bank and than to use it "use bank;" 4 | 5 | 6 | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 7 | 8 | 9 | 10 | create table employees(emp_id int(10) not null,first_name varchar(20),last_name varchar(20),salary int,primary key (emp_id)); 11 | 12 | 13 | ----------------------------------------------------------------------------------------- 14 | 15 | 16 | create table employee( 17 | emp_id int, 18 | first_name varchar(20), 19 | last_name varchar(20), 20 | salary int, 21 | dept_id int, 22 | primary key(emp_id) 23 | ); 24 | 25 | 26 | -------------------------------------------------------------------------------------- 27 | 28 | 29 | 30 | alter table world.valen3 drop column MyUnknownColumn; */ to delete column 31 | 32 | 33 | alter table employees add column dept varchar(20); */ to add column 34 | 35 | 36 | alter table world.valen rename column Player_id to player_id; */to change column name 37 | 38 | 39 | alter table bank_details add column geo_location varchar(20); 40 | 41 | 42 | insert into employees(emp_id,first_name,last_name,salary) values(5,'raju','patel',50000 ); 43 | 44 | update employees set last_name='patel' where emp_id=6; 45 | 46 | update department set dept_loc ='new jersey' where dept ='support'; 47 | 48 | update employee set salary = 64000 where emp_id =104; 49 | 50 | delete from employees where emp_id in (1,6); 51 | 52 | 53 | ---------------------------------------------------------------------------------------------- 54 | 55 | 56 | WHERE CLAUSE 57 | 58 | 59 | 60 | select * from employees where emp_id=2; 61 | 62 | select * from employees where first_name= 'amita' and salary=30000; 63 | 64 | select * from employees where first_name= 'kavi' or salary=30000; 65 | 66 | select * from employees where first_name != 'baba re baba'; 67 | 68 | select * from employees where salary >= 40000; 69 | 70 | select * from employees where first_name != 'kavi'; 71 | 72 | select * from employees where salary between 30000 and 50000; 73 | 74 | select * from employees where first_name like 'tainsa'; 75 | 76 | select * from employees where salary in (40000, 20000, 30000); 77 | 78 | select distinct (first_name) from employees; 79 | 80 | 81 | --------------------------------------------------------------------------------- 82 | 83 | 84 | OPERATORS AGGREGATION 85 | 86 | 87 | select avg (salary) from employees; 88 | 89 | select max(salary) from employees; 90 | 91 | select sum(salary) from employees; 92 | 93 | select count(*) from employees; 94 | 95 | 96 | --------------------------------------------------------------------------------- 97 | 98 | 99 | 100 | alter table employees add column dept varchar(20); 101 | 102 | 103 | alter table world.valen3 drop column MyUnknownColumn; */ to delete column 104 | 105 | 106 | insert into employees(emp_id, first_name,last_name,salary,dept) values(1,'valen','patel', 10000,'sales'); 107 | 108 | 109 | 110 | ------------------------------------------------------------------------------------ 111 | 112 | 113 | GROUPED CLAUSE 114 | 115 | 116 | select first_name, max(salary), dept from employees group by dept; *\ if we want to check the max salary with first names of the employees with a specific department 117 | 118 | 119 | 120 | ------------------------------------------------------------------------------------- 121 | 122 | 123 | HAVING CLAUSE 124 | 125 | 126 | select first_name, max(salary), dept from employees group by dept having count(dept)>=1; 127 | 128 | select first_name, avg(salary), dept from employees group by dept having count(dept)>=2; 129 | 130 | 131 | 132 | ------------------------------------------------------------------------------------------------------------- 133 | 134 | 135 | BY ORDER 136 | 137 | 138 | select * from employees order by salary desc; */ to view table in descending order/* 139 | 140 | select * from employees order by salary asc; */ to view table in ascending order/* 141 | 142 | 143 | 144 | -------------------------------------------------------------------------------------------------------------- 145 | 146 | 147 | 148 | UNION - THE UNION IS USED SO THAT THE REPETATION OF THE ELEMENTS HAVING COMMON NAMES CANNOT PRODUCE REPEAT VALUE IN THE RESULTS. IT IS USED TO NOT HAVE REPEATING VALUES 149 | 150 | 151 | select product_name from product1 152 | union 153 | select product_name from product2; 154 | 155 | 156 | 157 | -------------------------------------------------------------------------------------------------------------- 158 | 159 | 160 | 161 | UNION ALL - IT TAKES THE DUPLICATE VALUE AND SHOW IT IN RESULT 162 | 163 | 164 | select product_name from product1 165 | union all 166 | select product_name from product2; 167 | 168 | 169 | 170 | ------------------------------------------------------------------------------------------------------------ 171 | 172 | 173 | Error code 1175 update ---> go to "Edit" option there will be a pop out "Preferences" than go to "Sql Editor" than Uncheck the box "Safe Updates(reject UPDATES...)" 174 | Than Go to "Query" than "reconnect to server". 175 | 176 | 177 | --------------------------------------------------------------------------------- 178 | 179 | 180 | INNER JOIN 181 | 182 | 183 | select e.first_name, e.salary, d.dept,d.dept_loc 184 | from employees e 185 | inner join department d 186 | on e.dept=d.dept; 187 | 188 | 189 | LEFT JOIN 190 | 191 | 192 | select e.first_name, e.salary, e.dept, d.dept, d.dept_loc 193 | from employees e 194 | left join department d 195 | on e.dept=d.dept; 196 | 197 | 198 | RIGHT JOIN 199 | 200 | 201 | select e.first_name, e.salary, e.dept, d.dept, d.dept_loc 202 | from employees e 203 | right join department d 204 | on e.dept=d.dept; 205 | 206 | 207 | FULL OUTER JOIN 208 | 209 | 210 | select e.first_name, e.salary, e.dept, d.dept, d.dept_loc 211 | from employees e 212 | left join department d 213 | on e.dept=d.dept 214 | UNION 215 | select e.first_name, e.salary, e.dept, d.dept, d.dept_loc 216 | from employees e 217 | right join department d 218 | on e.dept=d.dept; 219 | 220 | 221 | CROSS JOIN 222 | 223 | 224 | select * from employees 225 | cross join department; 226 | 227 | 228 | ---------------------------------------------------------------------------------------------------------------------------------------- 229 | 230 | 231 | 232 | Create schema by Right clicking anywhere in the left side where all the other databases are 233 | 234 | 235 | you can add CSV file by Right clicking on the database you are working on and here are the steps:- 236 | 237 | 1. table data import wizard 238 | 2.select file path to import 239 | than keep clicking on next till Finish. don't do anything. 240 | 241 | 242 | 243 | ------------------------------------------------------------------------------------------------------------------------------ 244 | 245 | 246 | QUESTIONS+++++ 247 | 248 | 249 | #Datasets used : cricket_1.csv, cricket_2.csv instead i have valen.csv, valen2.csv 250 | --cricket_1 is the table for cricket test match 1. instead i have created world.valen and world.valen2, 251 | --Cricket_2 is the table for cricket test match 2. 252 | 253 | 254 | #Q.1 find all players who were present in test match 1 or test match 2; 255 | 256 | 257 | answer: 258 | 259 | 260 | SELECT * FROM world.valen 261 | union 262 | select * from world.valen2; 263 | 264 | 265 | -------------------------------------------------------------------------------- 266 | 267 | 268 | 269 | #Q.2 Write a MySQL query to find the players from the test match won having popularity higher than the average popularity. 270 | 271 | 272 | Answer : 273 | 274 | 275 | 276 | explanation - select will show the player name and popularity as we know 277 | from world.valen table 278 | 279 | select avg(popularity) from world.valen; will show the average popularity of the table 280 | 281 | 282 | 283 | 284 | select player_name, popularity from world.valen 285 | where popularity > (select avg(popularity) from world.valen); 286 | 287 | 288 | 289 | 290 | -------------------------------------------------------------------------------------------------------------------- 291 | 292 | 293 | 294 | #Q.3 Find player_id and player name that are common in the test match 1 and test match 2. 295 | 296 | 297 | answer : 298 | 299 | select player_id, player_name from world.valen where world.valen.player_id in (select player_id from world.valen2); 300 | 301 | 302 | 303 | ---------------------------------------------------------------------------------------- 304 | 305 | 306 | 307 | #Q.4 Retrive player_id, Runs and Player_name from cricket_1 Table and Display list of the players where the runs are more than the average runs. 308 | 309 | 310 | answer : 311 | 312 | 313 | select player_id, runs, player_name from world.valen */just breaking the code to read easily 314 | where runs > 315 | (select avg(runs) from world.valen); 316 | 317 | 318 | 319 | --------------------------------------------------------------------------------------- 320 | 321 | 322 | 323 | 324 | #Q.5 Write a Query to extract the player_id, Runs and Player_name from the table "cricket_1" where the runs are greater than 50. 325 | 326 | 327 | 328 | answer : 329 | 330 | 1. 331 | select player_id, runs, player_name from world.valen where runs>(50) ; 332 | 333 | 2. 334 | select player_id, runs, player_name from world.valen where runs>50 ; 335 | 336 | 337 | 338 | 339 | --------------------------------------------------------------------------------------- 340 | 341 | 342 | 343 | 344 | #Q.6 Write a query to extract all the columns from cricket_1 where player_namen starts with 'y' and ends with 'v'. 345 | 346 | 347 | 348 | answer : 349 | 350 | 351 | 352 | select * from world.valen2 where player_name like 'y%' 353 | 354 | 355 | 356 | 357 | ------------------------------------------------------------------------------------------ 358 | 359 | 360 | 361 | #Q.7 Write a query to extract all the columns from cricket_1 where players_name does not end with 'T'. 362 | 363 | 364 | 365 | answer : 366 | 367 | 368 | 369 | select * from world.valen where player_name not like '%t' 370 | 371 | 372 | 373 | 374 | 375 | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- 376 | 377 | Question - 378 | 379 | 380 | create table bank_details( 381 | product char(10), 382 | quantity int, 383 | price real, 384 | purchase_cost decimal(6,2), */ decimal(6,2) means 6 space numeric value where 2 is digits after decimal for ex - 1045.25 385 | estimated_sale_price float 386 | ); 387 | 388 | 389 | AS PER QUESTIONS - 390 | 391 | 1. insert into bank_details values('paycard',3,330,8008,9009); 392 | insert into bank_details values('paypoints',4,200,8000,6800); 393 | 394 | 2. alter table bank_details add column geo_location varchar(20); 395 | 396 | 3. select geo_location from bank_details where product ='paycard'; 397 | 398 | 4. select char_length(product) from bank_details where product ='paycard'; 399 | 400 | 5. alter table bank_details modify product varchar(10); 401 | 402 | 403 | --------------------------------------------------------------------------------------------- 404 | 405 | 406 | create table bank_holidays( 407 | holiday date, 408 | start_time time, 409 | end_time timestamp 410 | ); 411 | 412 | 413 | 414 | insert into bank_holidays values( 415 | current_date(), 416 | current_date(), 417 | current_date() 418 | ); 419 | 420 | 421 | 422 | 423 | update bank_holidays set holiday=date_add(holiday, interval 10 day); 424 | 425 | 426 | 427 | update bank_holidays set end_time = utc_timestamp(); 428 | 429 | 430 | 431 | select product as new_product from bank_details; */ for giving alias for once. so the product field will be seen as new product for a while. 432 | 433 | 434 | 435 | select * from bank_details limit 1; */ this will show only one line of record 436 | 437 | 438 | 439 | 440 | TO DISPLAY THE FIRST FEW CHARACTERS OF A "FIELD" OF TABLE -------->>> select substr(field 1,5) from table; 441 | --------------------------------------------------------------------------------