0% found this document useful (0 votes)
12 views12 pages

Mysql Adv Notes-2

The document contains a series of SQL queries for managing student data, including selecting, updating, and inserting records in the STUDENTS, COURSE, and MARKS tables. It also demonstrates how to join these tables to retrieve combined information, calculate total and average marks, and determine pass/fail results and grades. Additionally, it discusses creating views to simplify complex queries and filter results based on specific criteria.

Uploaded by

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

Mysql Adv Notes-2

The document contains a series of SQL queries for managing student data, including selecting, updating, and inserting records in the STUDENTS, COURSE, and MARKS tables. It also demonstrates how to join these tables to retrieve combined information, calculate total and average marks, and determine pass/fail results and grades. Additionally, it discusses creating views to simplify complex queries and filter results based on specific criteria.

Uploaded by

joyalprincess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ADVANCE QUERIES

1. SELECT *FROM STUDENTS;


2. ALTER TABLE STUDENTS ADD CID INT NOT NULL,ADD ROLLNO VARCHAR(20) NOT NULL;
3. UPDATE STUDENTS SET ROLLNO='2328K0109',CID=1 WHERE ID=1;

UPDATE STUDENTS SET ROLLNO='2328K0110',CID=2 WHERE ID=2;

UPDATE STUDENTS SET ROLLNO='2328K0111',CID=3 WHERE ID=3;

UPDATE STUDENTS SET ROLLNO='2328K0112',CID=1 WHERE ID=4;

UPDATE STUDENTS SET ROLLNO='2328K0113',CID=2 WHERE ID=5;

UPDATE STUDENTS SET ROLLNO='2328K0114',CID=3 WHERE ID=6;

UPDATE STUDENTS SET ROLLNO='2328K0115',CID=1 WHERE ID=7;

UPDATE STUDENTS SET ROLLNO='2328K0116',CID=2 WHERE ID=8;

UPDATE STUDENTS SET ROLLNO='2328K0117',CID=3 WHERE ID=9;

UPDATE STUDENTS SET ROLLNO='2328K0118',CID=1 WHERE ID=10;

UPDATE STUDENTS SET ROLLNO='2328K0119',CID=2 WHERE ID=11;

UPDATE STUDENTS SET ROLLNO='2328K0120',CID=3 WHERE ID=12;

UPDATE STUDENTS SET ROLLNO='2328K0121',CID=1 WHERE ID=13;

UPDATE STUDENTS SET ROLLNO='2328K0122',CID=2 WHERE ID=14;

UPDATE STUDENTS SET ROLLNO='2328K0123',CID=3 WHERE ID=15;

UPDATE STUDENTS SET ROLLNO='2328K0124',CID=1 WHERE ID=16;

UPDATE STUDENTS SET ROLLNO='2328K0125',CID=2 WHERE ID=17;

UPDATE STUDENTS SET ROLLNO='2328K0126',CID=3 WHERE ID=18;

UPDATE STUDENTS SET ROLLNO='2328K0127',CID=1 WHERE ID=19;

UPDATE STUDENTS SET ROLLNO='2328K0128',CID=2 WHERE ID=20;

UPDATE STUDENTS SET ROLLNO='2328K0129',CID=3 WHERE ID=21;

UPDATE STUDENTS SET ROLLNO='2328K0130',CID=1 WHERE ID=22;

UPDATE STUDENTS SET ROLLNO='2328K0131',CID=2 WHERE ID=23;

UPDATE STUDENTS SET ROLLNO='2328K0132',CID=3 WHERE ID=24;


4. SELECT *FROM STUDENTS

5. create table course(cid int not null auto_increment,cname varchar(10),primary key(cid));


6. insert into course(cname) values('AIDS'),('CS'),('IT');
7. select *from course;
8. create table marks(mid int not null auto_increment,id int not null,m1 int not null,m2 int not
null,m3 int not null,primary key(mid));
9. insert into marks(id,m1,m2,m3) values

(1,60,85,90),

(2,21,29,94),

(3,100,85,18),

(4,95,52,64),

(5,45,56,72),

(6,25,23,57),

(7,40,55,92),

(8,56,74,80),

(9,47,55,93),

(10,30,35,70),

(11,88,77,66),

(12,85,77,44),

(13,65,45,21),

(14,50,55,60),
(15,80,55,40),

(16,78,95,80),

(17,98,23,40),

(18,90,85,70),

(19,70,33,70),

(20,50,65,90),

(21,90,75,90),

(22,80,35,50),

(23,90,95,98),

(24,80,85,90);

10. select *from marks;

Here we are going to join CNAME column from course table into students table with the help of join
query.
11. select students.name,students.rollno,students.cid from students;

12. select students.name,students.rollno,course.cname from students inner join course on


students.cid=course.cid;

(here we have replace “cname” instead of “cid” using joins)


Multiple Inner joins

13. SELECT students.name,students.rollno,course.cname,marks.M1,marks.M2,marks.M3 FROM


students INNER JOIN course ON students.cid=course.cid
INNER JOIN marks ON students.id=marks.id;
Simple Dynamic Mark list using Queries

Find Total Marks

14. SELECT students.name,students.rollno,course.cname,marks.M1,marks.M2,marks.M3,


(marks.M1+marks.M2+marks.M3) As Total_Marks FROM students INNER JOIN course ON
students.cid=course.cid INNER JOIN marks ON students.id=marks.id;

Find Average Mark

15. SELECT students.name,students.rollno,course.cname,marks.M1,marks.M2,marks.M3,


(marks.M1+marks.M2+marks.M3) As Total_Marks,(marks.M1+marks.M2+marks.M3)/3 As
Average FROM students INNER JOIN course ON students.cid=course.cid INNER JOIN marks
ON students.id=marks.id;

ROUND
16. SELECT students.name,students.rollno,course.cname,marks.M1,marks.M2,marks.M3,
(marks.M1+marks.M2+marks.M3) As Total_Marks,

round((marks.M1+marks.M2+marks.M3)/3,2) As Average FROM students

INNER JOIN course ON students.cid=course.cid INNER JOIN marks ON students.id=marks.id;

FIND RESULT

17. SELECT students.name,students.rollno,course.cname,marks.M1,marks.M2,marks.M3,


(marks.M1+marks.M2+marks.M3) As Total_Marks,

round((marks.M1+marks.M2+marks.M3)/3,2) As Average ,

case

when marks.M1>=35 and marks.M2>=35 and marks.M3>=35 then 'PASS'

else 'FAIL'

end

as Result

FROM students

INNER JOIN course ON students.cid=course.cid INNER JOIN marks ON students.id=marks.id;


FIND GRADE

18. SELECT students.name,students.rollno,course.cname,marks.M1,marks.M2,marks.M3,


(marks.M1+marks.M2+marks.M3) As Total_Marks,

round((marks.M1+marks.M2+marks.M3)/3,2) As Average ,

case

when marks.M1>=35 and marks.M2>=35 and marks.M3>=35 then 'PASS'

else 'FAIL'

end

as Result ,

case

when marks.M1>=35 and marks.M2>=35 and marks.M3>=35 then

case

when round((marks.M1+marks.M2+marks.M3)/3,2)>=90 and


round((marks.M1+marks.M2+marks.M3)/3,2)<=100 then 'Grade-A'

when round((marks.M1+marks.M2+marks.M3)/3,2)>=80 and


round((marks.M1+marks.M2+marks.M3)/3,2)<=89 then 'Grade-B'

else 'Grade-C'

end

else 'No Grade'

end as Grade

FROM students

INNER JOIN course ON students.cid=course.cid INNER JOIN marks ON students.id=marks.id;


IF u want to view record base on Course?

where course.cname='AIDS'; ( just add this query at the end queries in above)

If u want to view ‘PASS’ result only?

where course.cname='AIDS' and Result='PASS'; ( just add this query in the above example)

( here leads error like “Error Code: 1054. Unknown column 'Result' in 'where clause')

Note : Because here ‘Result’ column is called Dynamic-column(or) Temporary columns .

We can solve this problem with the help of “having” – keyword

where course.cname='AIDS' having Result='PASS'; ( now its works )


where course.cname='AIDS' having Result='PASS' and (Average>=70 and Average<=100);

VIEW

 In above queries contains lot of lines


 Is there also some dynamic columns (temporary columns) like Total,Average,Result,Grade.
 These all are called “ select queries”
 We can convert “select query” to virtual table with the help of “view”
 Views in SQL are considered as a virtual table. A view also contains rows and columns.
 To create the view, we can select the fields from one or more tables present in the
database.

19. CREATE VIEW Reports AS ( just add this line at the beginning of your query)

SELECT students.name,students.rollno,course.cname,marks.M1,marks.M2,marks.M3,
(marks.M1+marks.M2+marks.M3) As Total_Marks,

round((marks.M1+marks.M2+marks.M3)/3,2) As Average ,

case

when marks.M1>=35 and marks.M2>=35 and marks.M3>=35 then 'PASS'

else 'FAIL'

end

as Result ,

case

when marks.M1>=35 and marks.M2>=35 and marks.M3>=35 then

case

when round((marks.M1+marks.M2+marks.M3)/3,2)>=90 and


round((marks.M1+marks.M2+marks.M3)/3,2)<=100 then 'Grade-A'

when round((marks.M1+marks.M2+marks.M3)/3,2)>=80 and


round((marks.M1+marks.M2+marks.M3)/3,2)<=89 then 'Grade-B'
else 'Grade-C'

end

else 'No Grade'

end as Grade

FROM students

INNER JOIN course ON students.cid=course.cid INNER JOIN marks ON students.id=marks.id where


course.cname='AIDS' having Result='PASS' and (Average>=70 and Average<=100);

(now view created successfully but it not display the table )

 In above example ‘Report’ is called view name


20. show tables;

21. show full tables;

To display the view?

22. select *from reports;


23. select rollno,name,Total_Marks,Result from reports;

You might also like