0% found this document useful (0 votes)
5 views9 pages

Suranjan SQL Noats 5

The document outlines SQL commands for creating and managing a 'student' table, including attributes like id, name, dept, year, age, semester, passing_year, marks, and result. It details operations such as inserting values, retrieving data, updating records, and altering the table structure. Additionally, it includes examples of querying the table based on specific conditions and sorting results.

Uploaded by

suranjan.jana1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Suranjan SQL Noats 5

The document outlines SQL commands for creating and managing a 'student' table, including attributes like id, name, dept, year, age, semester, passing_year, marks, and result. It details operations such as inserting values, retrieving data, updating records, and altering the table structure. Additionally, it includes examples of querying the table based on specific conditions and sorting results.

Uploaded by

suranjan.jana1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
-— Notes 1. create a relationship with the following attributes - id, name, dept, year, age. ans: CREATE TABLE student ( id INT PRIMARY KEY, name VARCHAR(50), dept VARCHAR(10), year INT, age INT ); 2. show the description of the table. ans: DESC student; output: Resum ena | gi mimer mows: Field Type Null = =Key = Default > Jid int no pa Ms name varchar(50) YES cos dept varchar(10) YES cos year = int YES cm age int YES oo Result1 x -— Notes 3. Insert values into the table ans: INSERT INTO student (id, name, dept, year, age) VALUES (1, ‘Alice’, 'CSE', 2, 20), (2, 'Bob', 'ECE', 3, 21), (3, ‘Charlie’, 'MECH', 4, 22), (4, 'David', 'CSE', 1, 19), (5, 'Eve', 'IT', 2, 23); 4. Retrieve all the info stored in the table ans: SELECT * FROM student; output: Result Grid | J} @} Fitter Rows: id name dept year age > |1 Alice CSE 2 20 2 Bob ECE 3 21 3 Charlie MECH 4 22 4 David CSE 1 19 5 Eve IT 2 23 student 2 x -— Notes 5. Display student details where dept is CSE ans: SELECT * FROM student WHERE dept = 'CSE'; output: Result Grid | J] @} Filter Rows: id name dept year age > 1 Alice CSE 2 20 4 David CSE 1 19 cs i Ta HS mo 6. Display student ID and name where dept is ECE ans: SELECT id, name FROM student WHERE dept = 'ECE’; output: id name » |2 Bob rs 43 7. Update the age of the student whose ID is 4 ans: UPDATE student SET age = 20 WHERE id = 4; output after update: name id age > (David 4 20 mm SS — Notes 8. Include an attribute semester in the relation ans: ALTER TABLE student ADD COLUMN semester INT; 9. Retrieve all the info ans: SELECT * FROM student; output: Result Grid | {]]] €} Fiter Rows: | EB id name dept year age semester > [1 Alcee CSE 2 a os 2 Bob ECE 3 2a oo 3 Charlie MECH 4 2 oO 4 David CSE 1 18 5 Eve IT a 3 oS mm mm Ww oS oo 10. Set semester as 6 for all students ans: UPDATE student SET semester = 6; output: id mame dept year age semester > 1 Alice CSE 2 2 6 2 Bob ECE 3 21 6 3 Charlie MECH 4 22 6 4 David ce i 20 6 5 Eve T 2 23 6 — Notes Tl. Include an attribute passing year ans: ALTER TABLE student ADD COLUMN passing_year INT; 12. Rename the column 'year' to 'admission_year ans: ALTER TABLE student CHANGE year admission_year INT; output: id name dept —_admission_year age semester passing_year > |1 Ake CSE 2 2 6 mas 2 Bob &E 3 21 «6 an 3 Charlie MECH 4 2 6 a 4 David CSE 1 9 «66 cas 5 Eve 1 2 23 6 oo ms oo Prout] = om Pt] 13. Insert values keeping passing year blank ans: INSERT INTO student (id, name, dept, admission_year, age, semester, passing_year) VALUES (6, 'Frank', 'EEE', 3, 22, 6, NULL); — Notes 14. Retrieve all the info ans: SELECT * FROM student; output: id name dept admission_year age semester passing_year 2 Bob EE 3 2 66 _ 3 Cherie MECH 4 2 6 == 4 David «CSE 1 9 6 os 5 Eve oT 2 23 «6 os 6 Frank EEE 3 2 6 = mo = om = om oo 15. Set semester as 7 where dept is IT ans: UPDATE student SET semester = 7 WHERE dept = uT': output: Wd |ewme | dept | aduiscbn year | age | semester | paesna_yeor 2 Bb EE 3 2. «6 3 Charlie MECH 4 2 6 4 David CSE 1 9 66 5 &e 2 23 7 6 Frank EEE 3 2 6 Zi oo a fret} am wy cus 16. Show distinct dept from the student table ans: SELECT DISTINCT dept FROM student: output: dept » |CSE ECE MECH Tr — Neies 17. List student names where age is > 20 to 24 SELECT name FROM student WHERE age BETWEEN 20 AND _ 24; >» Alice Charlie Eve Frank 18. Include an attribute marks ans: ALTER TABLE student ADD COLUMN marks INT; select * from student; -- Update marks for students UPDATE student SET marks = 60 WHERE id = 1; UPDATE student SET marks = 70 WHERE id = 2; UPDATE student SET marks = 80 WHERE id = 3; UPDATE student SET marks = 95 WHERE id = 4; UPDATE student SET marks = 50 WHERE id = 5; output: id name dept admission_year age semester passing_year marks > |1 Ale CSE 2 a 6 = 60 2 Bob CE 3. 2 «6 = 7 3 Cherie MECH 4 2 6 = 80 4 David CSE 1 9 66 om 95 5 Be 2 3 7 = 50 6 Frank «EEE 3. 2 6 om om — Neies 19. Include a new attribute result ans: ALTER TABLE student ADD COLUMN result VARCHAR(20); 20. Insert values in the result column if marks > 60, else NULL ans: UPDATE student SET result = 'Passed' WHERE marks > 60; UPDATE student SET result = NULL WHERE marks <= 60; select * from student; id name dept _admission_yeer age semester _passing_year marks result pit Ae CSE 2 m 6 ray o wm 2 Bb EE 3 2 6 = 7 Passed 3 Cherie MECH 4 2 6 = 80 Passed 4 David CSE 1 9 6 = 95 Passed 5 fe © 2 ae ed > 6 3 2 6 oo = om Frank EEE student 22 21. List all info where the result is NULL and update as 'Not Promoted' SELECT * FROM student WHERE result IS NULL; UPDATE student SET result = 'Not Promoted! WHERE result IS NULL; — Netes select * from student; output: id name dept admission_year age semester passing_year marks result > |1 Ake CSE 2 mo 6 60 Not Promoted 2 Bb EES 21 6 = 7 Passed 3 Charlie MECH 4 2 6 os 80 Passed 4 David = CSE 1 Cy 6 as 6 Passed 5 eve Tr 2 3 2 0 ‘Not Promoted 6 Frank EEE 3 2 6 = a= Not Promoted =o = om oo = = 22. Show the name and dept of students according to marks in ascending and descending order ans: SELECT name, dept FROM student ORDER BY marks ASC; output: name dept > |Frank EEE Eve Tr Alice CSE Bob ECE Charlie = MECH David CSE SELECT name, dept FROM student ORDER BY marks DESC; output: name dept >» David CSE Charlie © MECH Bob ECE Alice CSE Eve TT Frank EEE

You might also like