Вы находитесь на странице: 1из 5

Basic MySQL Assignment-1

1. Wrte query to nd current date and tme.


Seect NOW();
2. Wrte a query to nd the database you are usng currenty.
Seect database();
3. Wrte a query to dspay a random number.
Seect rand();
4. Wrte a query to deete a the records n a tabe retanng the tabe structure.
Deete * from tabe;
5. Gve the syntax to create ndex on a partcuar ed n a tabe.
Create ndex ndexNAme on tabeName(CounmNAme);
6. Wrte query to add and subtract 2 dates.
Seect date(date_add(date1, date2))
Seect date(date_sub(date1, date2))
7. Wrte a query to nd the month dherence between 21/05/2010 and 22/09/2010.
8. Wrte a query to return the coumns and coumn nformaton pertanng to the
desgned tabe.
9. Gve the syntax to make a coumn unque.
Create tabe tabename (counmname type UNIOUE)
10.Gve the syntax to make a coumn bgger.
Ater tabe tabename modfy coumnname VARCHAR(sze)
11.Gve the syntax to return unque records from a partcuar coumn of a tabe.
Seect dstnct coumnname from tabe
12.How w you nd the coumns that are ndexed n a tabe?
Use the below table to answer the questions 13 to 37.
13.Wrte a query to nd the numbers of records n the tabe.
Seect count(*) from tabe
14.Wrte a query to dspay the rst 5 records.
Seect top 5 from tabe
15.Wrte a query to dspay the 3
rd
record.
Wth myTabe AS(seect *, ROW_NUMBER() as RN from tabe) SELECT * where RN=3
16.Wrte a query to dspay records 6 to 9.
Wth myTabe AS(seect *, ROW_NUMBER() as RN from tabe) SELECT * where RN
between 6 and 9
17.Add ndex to the coumn 'Name'.
Create ndex NAMEINDEX on tabe (Name)
18.Add a coumn 'SeraNo' whch s auto ncrementa.
Ater tabe add SeraNo nt dentty(1,1)
19.Change the coumn name of the coumn 'Name' as 'StudentName'.
Ater tabe rename Name to StudentName
20.Change the vaues n the coumn 'Mark' nto 'oat wth 2 decma ponts'.
Ater tabe ater coumn Mark oat(2)
21.Fnd out the no of students who got more marks than Ramu n Scence.
Seect count() from tabe where Sub|ect=Scence and Mark>(seect Mark from
tabe where Name=Ramu and Sub|ect=Scence )
22.Fnd out the no of students who got ess marks than Udhay n Engsh.
Seect count() from tabe where Sub|ect=Engsh and Mark<(seect Mark from
tabe where Name=Udhay and Sub|ect=Engsh )
Name ID Subject Mark
101Maths 85
102Science 54
103English 68
102Maths 74
103Science 42
101English 78
101Science 90
103Maths 84
104English 28
102English 32
104Maths 47
Raj 105English 74
104Science 34
Raj 105Maths 95
Raj 105Science 88
Ramu
Asif
Udha
Asif
Udha
Ramu
Ramu
Udha
!ijaa
Asif
!ijaa
!ijaa
23.Wrte a query to nd the tota number of students.
Seect count(dstnct ID) from tabe
24.Wrte a query to nd the tota marks earned by each student.
Seect Name, ID, sum(Mark) from tabe group by ID
25.Wrte a query to dspay ke "Udhay-103-Maths-84".
Seect concat(name,-,ID,-,Sub|ect,-,Marks) from tabe
26.Wrte a query to nd the hghest mark earned by each student.
Seect Name, max(Mark) from tabe group by Name
27.Wrte a query to nd the maxmum mark n each sub|ect.
Seect Sub|ect, max(Mark) from tabe group by Sub|ect
28.Wrte a query to nd the mnmum mark of V|aya.
Seect Name, mn(Mark) from tabe group by Name where Name=V|aya
29.Create a tabe whch stores the top scorers n each sub|ect
Create tabe top_scores (Name varchar(10), Sub|ect varchar(10))
Insert nto tabe top_scores(Sub|ect, Name) vaues (seect name from tabe where
mark=(seect max(Mark) from tabe group by sub|ect))
30.Create a new tabe that stores the studentName and marks of each student n
Maths and Engsh.
31.Wrte a query to nd the average mark of each student.
Seect avg(mark) from tabe group by Student
32.Wrte a query to dspay the student name and ID n descendng order of ID.
Seect name, dstnct ID from tabe order by ID Desc
33.Wrte a query to nd the sum of owest marks earned by each student.
Seect sum(seect mn(Mark) from tabe group by Name)
34.Wrte a query to dspay the names that start wth 'R'
Seect dstnct Name from tabe where Name ke R%
35.Wrte a query to dspay the names that has the etter '' n t.
Seect dstnct Name from tabe where Name ke %%
36.Wrte a query to dspay the name and d of the students who has scored n the
range 60 to 75 n Engsh.
Seect dstnct ID, Name from tabe where Sub|ect=Engsh and Mark between 60
and 75
37.Use case functon to dspay grades (A : >80 , B: 60-80, C: 40-60, F: < 40)
Ater tabe add coumn grade char
Insert nto tabe (grade) vaues(seect(case f Mark>80 then A ese f Mark
between 60 and 80 then B ese f Mark between 40 and 60 then C ese D))
Use the below tables to answer the questions 3 to !"
#i$en a %atabase with the &ollowing tables
'able( parent 'able( chd

38.Rename the tabe 'parent' as 'ParentDetas'
Rename tabe parent to ParentDetas
39.Wrte a query to dspay the chdren of 'C'.
Seect ChdNAme from parent eft |on chd on parent.ParentID=chd.ParentID
where Chdname=C
40.Wrte a query to dspay the parent wthout a chd.
Seect ParentNAme from parent eft |on chd on parent.ParentID=chd.ParentID
where Chdname=nu group by PArentID
41.Wrte a query to dspay the parent havng more than 1 chd.
Seect
42.Wrte a query to dspay the no of chdren each parent has.
Seect dstnct parentID, count(chd.parentID) from parent eft |on chd on
parent.ParentID=chd.ParentID
43.Wrte a query to dspay the parent of X,Y and Z.
Seect chdNAme, ParentNAme from chd eft |on parent where ChdName n
(X,Y,Z)
44.Wrte a query to nd the rst chd of each parent.
Seect ChdNAme from parent eft |on chd on parent.ParentID=chd.ParentID
where Age=(seect mn(Age) from parent eft |on chd group by PArentID )
45.Wrte a query to nd the chdren who are more than 4 years od.
Seect ChdnName from chd where age>4
46. Wrte a query to nd the parent havng chdren who are 3 years od.
Seect dstnct ParentNAme from parent eft |on chd on
parent.ParentID=chd.ParentID where age=3 group by PArentID
47.Create a tabe whch has the foowng eds: ParentID,ParentName,ChdName,Age.
Create tabe new_Tabe(PArentID nt, ParentNAme varchar(50), CHdNAme
varchar(50), Age nt)
Insert nto new_Tabe (seect parent.ParentID, parent.ParentName, chd.chdName,
chd.Age from parent eft |on chd on parent.ParentID=chd.ParentID)
48.Add a new coumn (ParentName) to the exstng tabe 'chd'.
Ater tabe chd add coumn ParentName varchar(50)
1 A
2 "
3 #
4 $
5 E
6 %
ParentID ParentName
Age
1 & 3
3 ' 6
3 R 3
5 ( 4
2 ) 5
3 * 1
2 + 2
ParentID ChildName
49.Update the coumn ParentName whch you have created.
Insert nto chd(ParentName) vaues (seect ParentName from parent where
parent.ParentID=chd.ParentID)
50. Deete the coumn Age from the tabe 'chd'.
Deete from chd coumn Age

Вам также может понравиться