Posts

MySQL IF & CASE Statement

Image
 How to work IF Claus? IF Claus Syntax: Code: IF Clause SELECT id , name , percentage , IF ( percentage >= 33 , "Pass" , "Fail" ) AS Result FROM students ; How to Use Case clause in mysql? Syntax: CASE Clause SELECT id , name , percentage , CASE WHEN percentage >= 80 AND percentage <= 100 THEN "Merit" WHEN percentage >= 60 AND percentage <= 80 THEN "Ist Division" WHEN percentage >= 45 AND percentage <= 60 THEN "IInd Division" WHEN percentage >= 33 AND percentage <= 45 THEN "IIIrd Division" WHEN percentage < 33 THEN "Fail" ELSE "Not Correct %" END AS Grade FROM students ; UPDATE students SET percentage = ( CASE id WHEN 3 THEN 39 WHEN 7 THEN 62 END ) WHERE id IN ( 3 , 7 ) ;

MySQL UNION & UNION ALL

Image
 How to work UNION & UNION ALL: Example: Syntax: Code: CREATE TABLE personal( id INT NOT NULL, name VARCHAR(50) NOT NULL,     percentage INT NOT NULL, age INT NOT NULL, gender VARCHAR(1) NOT NULL, city INT NOT NULL,     courses INT NOT NULL,     PRIMARY KEY (id), FOREIGN KEY (city) REFERENCES City (cid),     FOREIGN KEY (courses) REFERENCES Courses (course_id)   ); INSERT INTO personal(id,name,percentage,age,gender,city,courses) VALUES (1,"Aqib","45","13","M",1,1), (2,"Khadija","56","21","F",2,2), (3,"Salman Khan","62","20","M",1,1), (4,"Aqeela","47","18","F",3,1), (5,"Saqib","74","22","M",1,3), (6,"Abraham","64","21","M",2,2), (7,"Shahid","52","20","M",1,3); create table "city" and in...

MySQL SubQuery With EXISTS & NOT EXISTS

Image
 How to use SubQuery in MySql table: SubQuery Syntax: Code: CREATE TABLE personal( id INT NOT NULL, name VARCHAR(50) NOT NULL,     percentage INT NOT NULL, age INT NOT NULL, gender VARCHAR(1) NOT NULL, city INT NOT NULL,     courses INT NOT NULL,     PRIMARY KEY (id), FOREIGN KEY (city) REFERENCES City (cid),     FOREIGN KEY (courses) REFERENCES Courses (course_id)   ); INSERT INTO personal(id,name,percentage,age,gender,city,courses) VALUES (1,"Aqib","45","13","M",1,1), (2,"Khadija","56","21","F",2,2), (3,"Salman Khan","62","20","M",1,1), (4,"Aqeela","47","18","F",3,1), (5,"Saqib","74","22","M",1,3), (6,"Abraham","64","21","M",2,2), (7,"Shahid","52","20","M",1,3); create table "city" and i...