Unit 2 MySQL - DBMS
Unit 2 MySQL - DBMS
Text functions: UCASE ()/UPPER (), LCASE ()/LOWER (), MID ()/SUBSTRING ()/SUBSTR (), LENGTH (), LEFT (), RIGHT
(), INSTR (), LTRIM (), RTRIM (), TRIM ().
Date Functions: NOW (), DATE (), MONTH (), MONTHNAME (), YEAR (), DAY (), DAYNAME ().
Aggregate Functions: MAX (), MIN (), AVG (), SUM (), COUNT (); using COUNT (*).
i n f o r m a t i o n
index postion 1 2 3 4 5 6 7 8 9 10 11
left(string, number of chars) right(string, number) mid(string, positon, number) substr(string, positon, number)
It display left side characters. It display right side characters. It display middle characters. It display middle characters.
select left(“information”, 1); select right(“information”, 1); select mid(“information”, 1, 1); select substr(“information”, 1, 1);
i n i i
select left(“information”, 2); select right(“information”, 2); select mid(“information”, 1, 2); select substr (“information”, 1, 2);
in on in in
select left(“information”, 3); select right(“information”, 3); select mid(“information”, 1, 3); select substr (“information”, 1, 3);
inf ion inf inf
select left(“information”, 4); select right(“information”, 4); select mid(“information”, 2, 1); select substr (“information”, 2, 1);
info tion n n
select left(“information”, 5); select right(“information”, 5); select mid(“information”, 2, 2); select substr (“information”, 2, 2);
Infor ation nf nf
1
Text Funtions:
String means, a group of characters. It enclosed in single or double quotes. For example: “information”, ‘infomation’
I n f o r m a t i o n
index postion 1 2 3 4 5 6 7 8 9 10 11
select length(“information”); select upper(“information”); select lower (“INFORMATION”); select instr(“information”, “infor”);
11 INFORMATION information 1
select length(“mation”); select upper(“mation”); select lower (“MATION”); select instr(“information”, “nfor”);
6 MATION mation 2
select length(“tion”); select upper(“MATION”); select lower (“mation”); select instr(“information”, “for”);
4 MATION (no change) mation (no change) 3
select length(“infor mation”); select ucase(“Mation”); select lcase(“Mation”); select instr(“information”, “or”);
12 (space also count) MATION mation 4
select length(“infor ma tion”); select ucase(“infor mation”); select lcase(“INFOR MATION”); select instr (“information”, “tion”);
13 (space also count) INFOR MATION infor mation 8
2
Text Funtions:
String means, a group of characters. It enclosed in single or double quotes. For example: “information”, ‘infomation’
I n f o r m a t i o n
index postion 1 2 3 4 5 6 7 8 9 10 11
select concat(“infor”, “mation”); select ltrim(“ information”); select rtrim(“information ”); select trim(“ information ”);
information information information information
select concat(“informa”, “tion”); select ltrim(“ infor mation”); select rtrim(“infor mation ”); select trim(“ infor mation ”);
information infor mation infor mation infor mation
select concat(“informa ”, “tion”); select ltrim(“ infor ma tion”); select rtrim(“infor ma tion ”); select trim(“ infor ma tion ”);
informa tion (space also) infor ma tion infor ma tion infor ma tion
select concat(“infor ma ”, “ tion”); select ltrim(“ information ”); select rtrim(“ information ”); select trim(“ information ”);
Infor ma tion (space also) information__ __ information information
3
Math Funtions:
select sign(-15); select mod(5, 2); select power(2, 3); select sqrt(144);
-1 1 8 12
select sign(15); select mod(5, 3); select power(3, 2); select sqrt(169);
1 2 9 13
select sign(15-15); select mod(7, 2); select power(3, 3); select sqrt(225);
0 1 27 15
select sign(-15.2); select mod(7, 4); select power(3, 4); select sqrt(225.5);
-1 3 81 15.016657
select sign(15-17); select mod(7.25, 4); select power(4, 3); select sqrt(7*7);
-1 3.25 64 7
round(value, no. of decimals) truncate(value, no. of decimals) ceil(value) floor(value)
It display round off value. It reduce decimal places. But it never After decimal point it will After decimal point it will
round off value. increase to next value. decrease to preivious value.
select round(52.356, 0); select truncate(52.5635, 0); select ceil(52.1); select floor(52.1);
52 52 53 52
select round(52.5635, 0); select truncate(52.5635, 1); select ceil(52.2); select floor(52.2);
53 52.5 53 52
select round(52.5635, 1); select truncate(52.5635, 2); select ceil(-52.1); select floor(-52.1);
52.6 52.56 -52 -53
select round(52.5635, 2); select truncate(-52.5635, 2); select ceil(-52.2); select floor(-52.2);
52.57 -52.56 -52 -53
4
Aggregate Funtions:
book table
price column
10
20
30
sum(column) max(column) min(column) avg(column) count(column)
To show sum of given To show maximum value of To show minimum value of To show average value To count all values of given
column excluding null
column values given column values given column values of given column values
select sum(price) select max(price) select min(price) select avg(price) select count(price)
from book; from book; from book; from book; from book;
60 30 10 20 3
select sum(price) select max(price) select min(price) select avg(price) select count(price)
from book from book from book from book from book
where price>10; where price>10; where price>10; where price>10; where price>10;
50 30 20 25 2
select sum(price) select max(price) select min(price) select avg(price) select count(price)
from book from book from book from book from book
where price<30; where price<30; where price<30; where price<30; where price<30;
30 20 10 15 2
select monthname (‘2020-11-12’); select dayname (‘2020-11-12’); select dayofmonth (‘2020-11-12’); select dayofyear(‘2020-11-12’); select sleep(3);
Nov Thursday 12 317 sleep for 3 seconds.
select monthname (‘2019-10-28’); select dayname (‘2019-10-28’); select dayofmonth (‘2019-10-28’); select dayofyear(‘2019-10-28’); select sleep(4);
Oct Monday 28 301 sleep for 4 seconds.
5
Difference between now() and sysdate() function:
now() sysdate()
it return date with time which the function executes. it return updated date with time.
here before sleep() and after sleep() function, time here before sleep() and after sleep() function, time
in seconds, not changed. in seconds, changed.
6
Joining Concept
T1 T2 Equi join Left join output Right join output Full outer join output Cartesian product
Left Right output join output
1 1 T1 T2 T1 T2 T1 T2 T1 T2 T1 X T2
2 1 1 1 1 1 1 1 1 1 (3X5 = 15 rows)
4 2 1 1 1 1 1 1 1 1 T1 T2
2 2 2 2 2 2 2 2 2 1 1
3 2 2 2 2 2 2 2 2 2 1
4 NULL NULL 3 4 NULL 4 1
NULL 3
1 1
2 1
4 1
1 2
2 2
4 2
1 2
2 2
4 2
1 3
2 3
4 3
Grouping Concept
T2 After group T2 After group T2 After group T2 After group T2 After group T2
Sum output max output min output avg output count output
1 T2 Sum T2 max T2 min T2 avg T2 count
1 1 2 1 1 1 1 1 1 1 2
2 2 4 2 2 2 2 2 2 2 2
2 3 3 3 3 3 3 3 3 3 1
3
7
MySQL Joining Operation
create database school;
use school;
create table student create table student2
( (
city varchar(25), city varchar(25),
fees int fees int
); );
insert into student values(‘mithapur’, 100); insert into student2 values(‘mithapur’, 10);
insert into student values(‘dwarka’, 200); insert into student2 values(‘mithapur’, 20);
insert into student values(‘okha’, 300); insert into student2 values(‘dwarka’, 30);
insert into student values(‘jamnagar’, 400); insert into student2 values(‘jamnagar’, 50);
insert into student2 values(‘rajkot’, 500);
insert into student2 values(‘dwarka’, 5);
select * from student; select * from student2;
Cartesian Joining
Multiplication of both tables. Each row of right table interacts with all the rows of left table.
Here based on city column.
Here left table is student and right table is student2.
select *
from student cross join student2;
8
Equi Joining
Only matching rows of both tables will show. Non-matching rows of both tables will not show.
okha and rajkot city rows, will not shows. Because they are non-matching rows of both tables.
Here based on city column
select *
from student join student2
on student.city=student2.city;
Left Joining
Only matching rows of left table will show. Non-matching rows of left table will show null in right table.
okha city rows, will not available in student2 table. So it will display null values in student2 table.
Here based on city column
select *
from student left join student2
on student.city=student2.city;
Right Joining
Only matching rows of Right table will show. Non-matching rows of Right table will show null in left table.
rajkot city rows, will not available in student table. So it will display null values in student table.
Here based on city column
select *
from student right join student2
on student.city=student2.city;
9
Grouping operation
Here Based on city column of student2 table.
select * from student2;
To show sum of fees of To show maximum fees of To show minimum fees of To show average fees of each
each city each city each city city
select city, sum(fees) select city, max(fees) select city, min(fees) select city, avg(fees)
from student2 from student2 from student2 from student2
group by city; group by city; group by city; group by city;
10
Set operation
Here city column, is common in both tables.
Union- To show all rows without duplicate of both tables.
Insersect – Common rows in both tables.
Minus- first table non matched rows includes.
select * from student; select * from student2;
Unfortunetaly, Directly, MySql does not support intersect and minus set operation.
Only Union set opeation support directly.
11
Revision Question and Answers of MySQl and RDBMS
Q1. library- database name
book table (relation) issue table (relation)
bid bname bprice bid sname issuedate
b1 Python 350 b1 rohit 2020/12/11
b2 Java 450 b2 vinod 2020/11/09
b3 html 250
Write mysql code for equi join between book and issue table.
Ans. select *
from book join issue
on book.bid = issue.bid
Q4. Write the output of following mysql code, here aliases are used for table such book table is b and issue table is i
:
select b.bid, i.sname, i.issuedate
from book b join issue i
on b.bid = i.bid
Ans. bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2021/11/09
Q5. Write the output of following mysql code, here aliases are used for table such book table is b and issue table is i
:
select b.bid, i.bid
from book b join issue i
on b.bid = i.bid
Ans. bid bid
b1 b1
b2 b2
Q6. Write the output of following mysql code, here aliases are used for table such book table is b and issue table is i
:
select b.bid, b.bname, b.bprice, i.bid, i.sname, i.issuedate
from book b join issue i
on b.bid = i.bid
Ans. bid bname bprice bid sname issuedate
b1 Python 350 b1 rohit 2020/12/11
12
b2 Java 450 b2 vinod 2021/11/09
Q10. In above, what is foreign key or reference key or secondary key of issue table?
Ans. bid, because bid of issue table depend on book table, bid.
Q13. Write mysql code to create above book table with following column name, data type and constraints.
book table
bid bname bprice
int char(20) int
primary key not null not null
Ans. create table book
(
bid int primary key,
bname char(20) not null,
bprice int not null
);
Q14. Write mysql code, to create above issue table with following column name, data type and constraints.
issue table
bid sname issuedate
int char(20) date
foreign key not null not null
Ans. create table issue
(
bid int,
sname char(20) not null,
issuedate date not null,
Q15. Write mysql code, to insert following records in above book table.
book table
bid bname bprice
b1 python 350
b2 Java 450
b3 Html 250
Ans. insert into book values(‘b1’, ‘python’, 350);
13
insert into book values(‘b2’, ‘java’, 450);
insert into book values(‘b3’, ‘html’, 250);
Q16. Write mysql code, to insert following records in above issue table.
issue table
bid sname issudate
b1 rohit 2020/12/11
b2 vinod 2020/11/09
Ans. insert into issue values(‘b1’, ‘rohit’, ‘2020/12/11’);
insert into issue values(‘b2’, ‘vinod’, ‘2020/11/09’);
select *
from book join issue;
select *
from book, issue;
Q20. Write the output of following mysql code and how many rows will display after cross join, also write cardinality
and degree after cross join:
select *
from book cross join issue;
Ans. book table issue table
bid bname bprice bid sname issuedate
b1 python 350 b1 rohit 2020/12/11
b2 java 450 b1 rohit 2020/12/11
b3 html 250 b1 rohit 2020/12/11
Q23. In above book, write mysql code to add new more column following
After adding one more column author so degree of book table is 4 columns.
Q25. Write mysql code for left join between book and issue table with aliases, for book table use b and issue table
use i.
Ans. select *
from book b left join issue i
on b.bid = i.bid;
Q31. Write mysql code for right join between book and issue table with aliases, for book table use b and issue table
use i.
Ans. select *
from book b right join issue i
on b.bid = i.bid;
Q36. Write mysql code to group, bid column and count, how many book issued for each bid and display.
issue table (relation)
bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2020/11/09
b1 amit 2021/10/08
b3 vinay 2021/08/06
b2 rohit 2019/08/07
b2 amit 2018/05/03
Ans. select bid, count(bid)
from issue
group by bid;
bid count(bid)
b1 2
b2 3
b3 1
Q37. Write mysql code to group, bid column and count, how many book issued for ‘b1’ and display.
issue table (relation)
bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2020/11/09
b1 amit 2021/10/08
b3 vinay 2021/08/06
b2 rohit 2019/08/07
b2 amit 2018/05/03
Ans. select bid, count(bid)
from issue
group by bid
having bid=’b1’;
bid count(bid)
b1 2
Q38. Write mysql code to group, bid column and count, how many book issued for ‘b2’ and display.
17
issue table (relation)
bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2020/11/09
b1 amit 2021/10/08
b3 vinay 2021/08/06
b2 rohit 2019/08/07
b2 amit 2018/05/03
Ans. select bid, count(bid)
from issue
group by bid
having bid=’b2’;
bid count(bid)
b2 3
Q39. Write mysql code to group, bid column and count, how many book issued for ‘b3’ and display.
issue table (relation)
bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2020/11/09
b1 amit 2021/10/08
b3 vinay 2021/08/06
b2 rohit 2019/08/07
b2 amit 2018/05/03
Ans. select bid, count(bid)
from issue
group by bid
having bid=’b3’;
bid count(bid)
b3 1
Q40. Write mysql code to group, sname column and count, how many book issued for each student name and
display.
issue table (relation)
bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2020/11/09
b1 amit 2021/10/08
b3 vinay 2021/08/06
b2 rohit 2019/08/07
b2 amit 2018/05/03
Ans. select sname, count(sname)
from issue
group by sname;
sname count(sname)
amit 2
rohit 2
vinay 1
vinod 1
Q41. Write mysql code to group, sname column and count, how many book issued for ‘amit’ and display.
issue table (relation)
bid sname issuedate
18
b1 rohit 2020/12/11
b2 vinod 2020/11/09
b1 amit 2021/10/08
b3 vinay 2021/08/06
b2 rohit 2019/08/07
b2 amit 2018/05/03
Ans. select sname, count(sname)
from issue
group by sname
having sname=’amit’;
sname count(sname)
amit 2
Q42. Write mysql code to group, sname column and count, how many book issued for ‘rohit’ and display.
issue table (relation)
bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2020/11/09
b1 amit 2021/10/08
b3 vinay 2021/08/06
b2 rohit 2019/08/07
b2 amit 2018/05/03
Ans. select sname, count(sname)
from issue
group by sname
having sname=’rohit’;
sname count(sname)
rohit 2
19
Q46. After minus set (fruit2-fruit1), operation write the output :
fruit1 fruit2
Apple Apple
Orange Banana
Grapes Grapes
Ans. Banana
DML (Data Manipulation Language):- This command allow users to access or manipulate data as
organized by the appropriate data model. for example:- select, insert, update and delete records of a
table.
Q95. To remove front and rear spaces of any given string, so we can use:
Ans. trim()
Q99. What is the output of following mysql code?, suppose in string front have 2 spaces and rear have 1 space
select length(trim(“ data “));
Ans. 4
it removed both, front and rear space of above string. So length of above given string is 4.
Q100. What is the output of following mysql code?, suppose in string front have 2 spaces and rear have 1 space
select length(lrim(“ data “));
Ans. 5
it removed only front space of above string. So length of above given string is 5.
Q101. What is the output of following mysql code?, suppose in string front have 2 spaces and rear have 1 space
select length(rtrim(“ data “));
Ans. 7
it removed only rear space of above string. So length of above given string is 7.
1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1
3 1 2 2 2 2 2 2
5 1 3 Null Null 4
5 Null
1 1
2 1
3 1
5 1
1 2
2 2
3 2
5 2
1 4
2 4
3 4
5 4
Q107. Write mysql code of union, union all, intersection and minus set operation of above t1 and t2 tables.
Ans. union all union intersection minus minus
select c1 select c1 select c1 select c1 select c2
from t1 from t1 from t1 from t1 from t2
union all union intersect minus minus
select c2 select c2 select c2 select c2 select c1
from t2; from t2; from t2; from t2; from t1;
Output Output Output Output Output
1 1 1 3 4
1
2 2 5
1
2 3
2 4
3
35
4 5
5
Q108. Difference between char and varchar datatypes of mysql.
Ans. CHAR VARCHAR
CHAR data types specifies a fixed length character VARCHAR data types specifies a variable length
string. When a column is given datatype as string. When a column is given datatype as
char(n), then MySQL ensures that all values stored varchar(n), then the maximum size a value in this
in that column have this length. column can have is n bytes. Each value that is
stored in this column stores exactly as you specify it
i.e., no blanks are added if the length is shorter
than maximum length n. However, if you exceed
the maximum length n, then an error message is
displayed.
It use single quotes ' ' It use double quotes " "
r a m r a m
m o h a n m o h a n
a j a y a j a y
ram used total 10 bytes. ram used only 3 bytes.
mohan used total 10 bytes. mohan used only 5 bytes.
ajay used total 10 bytes. ajay used only 4 bytes.
so total 30 bytes are used. so total (3+5+4=12 bytes) are used.
here empty memory wasted means blocked. here empty memory no wasted.
empty memory will not use by another data. empty memory will use to store another data.
Q116.
sid sname dob scity pincode fees
(yyyy-mm-dd)
integer char (20) date varchar(15) varchar(6) decimal (7,2)
primary key not null not null unique default check
‘313001’ between 4000
and 7000
Write mysql command to create above student table with above data types and constraints.
Ans. create table student
(
sid integer primary key,
sname char(20) not null,
dob date not null,
38
scity varchar(15) unique,
pincode varchar(6) default ‘313001’,
fees decimal(7,2) check (fees between 4000 and 7000)
);
Q117.
sid subject marks
integer char (20) integer
foreign key check check
hindi and english between 40 and 70
Write mysql command to create above table marks and with above data types and constraints and with
referential integrity.
Ans. create table marks
(
sid integer not null,
subject char(20) check( subject in ('hindi', 'english') ),
marks integer check ( marks>40 and marks<70 ),
)engine=innodb;
Q119. Write mysql command to delete only all records of above student table.
Ans. delete from student ;
Q120. Write mysql command to drop (delete all records with all columns) above student table.
Ans. drop table if exists student;
Q121. Write mysql command to delete a record of rollno have 101, for above student table.
Ans. delete from student where rollno=101;
Q122. Write mysql command to delete a record of rollno have 102, for above student table.
Ans. delete from student where rollno=102;
Q123. Write mysql command to delete a record of rollno have 103, for above student table.
Ans. delete from student where rollno=103;
Q124. Write mysql command to delete all record where fees below 4000, for above student table.
Ans. delete from student where fees<4000;
Q125. Write mysql command to delete all record where fees above 4000, for above student table.
Ans. delete from student where fees>4000;
39
Q126. Write mysql commands to show all columns details with datatypes and with all constraints details, for above
student table?
Ans. desc student;
Q127. Write mysql command to create bank database and also open bank database.
Ans. create database bank;
use bank;
Q128. Write mysql command to create hotel database and also open hotel database.
Ans. create database hotel;
use hotel;
Q129. Write mysql command to create library database and also open library database.
Ans. create database library;
use library;
Q130. Write mysql command to show all databases names in mysql servers.
Ans. show databases;
Q131. Write mysql command to show all tables names in bank database.
Ans. use bank;
show tables;
Q132. Write mysql command to show all tables names in hotel database.
Ans. use hotel;
show tables;
40
Q139. Write mysql command to add a new column state with varchar(25) data type and not null constraints in above
student table.
Ans. alter table student
add state varchar(25) not null;
Q140. Write mysql command to add a new column height with integer(3) data type and not null constraints in above
student table.
Ans. alter table student
add height int(3) not null;
Q141 Write mysql command to change name of column height to heightnew to the student table.
Ans. alter table student
change height heightnew integer(3);
Q142 Write mysql command to change name of column state to statenew to the student table.
Ans. alter table student
change state statenew varchar(25);
Q143 Write mysql command to change name of column city to citynew to the student table.
Ans. alter table student
change city citynew char(30);
Q144. To change data type of column statenew char(20) to the student table.
Ans. alter table student
modify statenew char(20);
Q145. To change data type of column citynew varchar(15) to the student table.
Ans. alter table student
modify citynew varchar(15);
Q146. To change data type of column heightnew integer(2) to the student table.
Ans. alter table student
modify heightnew int(2);
Q150. To give primary key constraint to rollno column, to the student table.
Ans. alter table student
add primary key(rollno);
Q151. To delete only primary key constraint to rollno column, to the student table.
Ans. alter table student
drop primary key;
Q152. To give, not null constraint to fees column, to the student table.
Ans. alter table student
modify fees integer not null;
41
Q153. To remove or drop a database means:
Ans. It removes all its tables also get removed along with the database directly. So carefully this command will use.
Q157. Write mysql command to rename above student table into studentnew.
Ans. Alter table student rename to studentnew;
Q160. Write mysql command to show all the records of above student table.
Ans. select *
from student;
Q161. Write mysql command to show only name column of above student table.
Ans. select name
from student;
Q162. Write mysql command to show only dob column of above student table.
Ans. select dob
from student;
Q163. Write mysql command to show only fees column of above student table.
Ans. select fees
from student;
42
Q164. Write mysql command to show only rollno, fees column of above student table.
Ans. select rollno, fees
from student;
Q165. Write mysql command to show only rollno, name, fees column of above student table.
Ans. select rollno, name, fees
from student;
Q166. Write mysql command to show only fees, name, rollno column of above student table, to change the column
order.
Ans. select fees, name, rollno
from student;
Q168. Write mysql command to show all student details of rollno 101 for above student table.
Ans. select *
from student
where rollno=101;
Q169. Write mysql command to show all student details of rollno 102 for above student table.
Ans. select *
from student
where rollno=102;
Q170. Write mysql command to show all student details of rollno 103 for above student table.
Ans. select *
from student
where rollno=103;
Q171. Write mysql command to show all student details of rollno above 103 for above student table.
Ans. select *
from student
where rollno>103;
Q172. Write mysql command to show all student details of rollno below 103 for above student table.
Ans. select *
from student
where rollno<103;
Q173. Write mysql command to show all student details of fees between 2000 to 5000, for above student table.
Ans. select *
from student
where fees between 2000 and 5000;
or
select *
from student
where fees>=2000 and fees<=5000;
43
Q174. Write mysql command to show all student details of fees not between 2000 to 5000, for above student table.
Ans. select *
from student
where fees not between 2000 and 5000;
Q175. Write mysql command to show all student details of fees have null, for above student table.
Ans. select *
from student
where fees is null;
Q176. Write mysql command to show all student details of fees is not null, for above student table.
Ans. select *
from student
where fees is not null;
Q177. Write mysql command to show all student details of name start with ‘a’ letter, for above student table.
Ans. select *
from student
where name like ‘a%’ ;
Q178. Write mysql command to show all student details of name start with ‘r’ letter, for above student table.
Ans. select *
from student
where name like ‘r%’ ;
Q179. Write mysql command to show all student details of name end with ‘a’ letter, for above student table.
Ans. select *
from student
where name like ‘%a’ ;
Q180. Write mysql command to show all student details of name end with ‘r’ letter, for above student table.
Ans. select *
from student
where name like ‘%r’ ;
Q183. To view all the columns with all rows of the current table, we can use ………..
Ans. *
The LIKE clause uses the following symbols known as wildcard operators in SQL to perform this pattern-
matching task in SQL.
Q186.
ID Name City Salary Age
Q188. In above employee_details, change all city name ‘mithapur’ using update command.
Ans. update employee_details
set city = “mithapur”
ID Name City Salary Age
46
Q189. In above employee_details, increment all salary values 10,000 using update command.
Ans. update employee_details
set salary = salary + 10000
ID Name City Salary Age
Q190. In above employee_details, increment salary 5000 only who have salary more than 70000 using update
command.
Ans. update employee_details
set salary = salary + 5000
where salary>70000;
ID Name City Salary Age
Q191. In above employee_details, increment age 5 only who have age below 25 using update command.
Ans. update employee_details
set age = age + 5
where age<25;
ID Name City Salary Age
Q193. Which key word is used to eliminate duplicate or redundant data in query result?
Ans Distinct
Write mysql code, in above student table display scity column without duplicate scity.
Ans. select distinct scity
from student;
scity
kota
pali
jaipur
udaipur
ajmer
48
Q196. student table
scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer
49
Q199. student table
scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer
50
Ans. scity
kota
Kota
jaipur
jaipur
52
(v) Write SQL statement to change the name of Singer “Sonvi Kumar” to “Sonvi Mehra” in all the places
wherever it occurs in CD table.
Ans. Update CD
Set SINGER = “Sonvi Mehra”
Where SINGER = “Sonvi Kumar”;
(vi). Write MySQL statement to add a new column “Music_Director” which data type Varchar and size as 30 and
not null constraint in table “CD”
Ans. Alter table CD
add Music_Director varchar(30) not null;
(vii). Write command in SQL to display code, Title and corresponding description of all the CDs.
Ans. SELECT CD.CODE, CD.TITLE, TYPE.DESCRIPTION
FROM CD, TYPE
WHERE CD.CATEGORY=TYPE.CATEGORY.
CD.CODE CD.TITLE TYPE.DESCRIPTION
101 Sufi Songs Classical
102 Eureka Classical
104 Dosti Jazz
Q208. Consider the table FLIGHT given below. Write commands in SQL for (i) to (iv) and output for (v) to (vii)
Table: FLIGHT
FLCODE START DESTINATION NO_STOPS NO_FLIGHTS
IC101 DELHI AGARTALA 1 5
IC102 MUMBAI SIKKIM 1 3
IC103 DELHI JAIPUR 0 7
IC105 KANPUR CHENNAI 2 2
IC107 MUMBAI KANPUR 0 4
IC431 INDORE CHENNAI 3 2
IC121 DELHI AHMEDABAD 2 6
(i) Display details of all flights starting from Delhi.
Ans. select *
from FLIGHT
where START=’DELHI’;
(ii). Display details of flights that have more than 4 number of flights operating.
Ans. select *
from FLIGHT
where NO_FLIGTS>4;
(iii) Display flight codes, starting place, destination, number of flights in descending order of number of flights.
Ans.
select FLCODE, START, DESTINATION, NO_FLIGHTS
from FLIGHT
order by NO_FLIGHTS DESC;
(iv) Display destinations along with flight code of all the destinations starting with ‘A’.
Ans.
select DESTINATION, FLCODE
from FLIGHT
where DESTINATION like ‘A%’;
(v) SELECT MAX(NO_FLIGHTS) FROM FLIGHT;
Ans: 7
(vi) SELECT DISTINCT (NO_STOPS) FROM FLIGHT;
Ans:
DISTINCT (NO_STOPS)
1
0
2
3
53
(vii) SELECT START, COUNT(*) FROM FLIGHT GROUP BY START;
Ans:
START COUNT(*)
DELHI 3
MUMBAI 2
KANPUR 1
INDORE 1
Q209. Write the output of the following SQL queries:
(i) SELECT RIGHT(‘software’, 2);
Ans. re
(ii) SELECT INSTR(‘twelve’, ‘lv’);
Ans. 4
(iii) SELECT DAYOFMONTH(‘2014-03-01’);
Ans. 1
(iv) SELECT ROUND(76.987, 2);
Ans. 76.99
Q210. Given ‘Employee’ table as follows:
Employee_ID NAME Commission
101 Sabhyata Sharma NULL
102 Divya Arora 8900
103 Faizal Zaidi NULL
(i) What values will the following statements return?
SELECT COUNT(*) FROM Employee;
Ans. 3
It counts all rows including null also.
(ii) SELECT COUNT(Commission) FROM Employee;
Ans. 1
In above, null value will not count.
(iii) Write the UPDATE statement in MySQL, to increase commission by 100.00 in the “Commission” column in
the ‘Employee’ table
Ans.
Update Employee
Set Commission = Commission + 100.00
(iv) Rewrite the following SQL statement after correcting errors(s) Underline the corrections made
INSERT IN STUDENT(RNO, MARKS) VALUE (5, 78.5);
Ans.
INSERT INTO STUDENT (RNO, MARKS) VALUES(5, 78.5);
Q211. Write MySQL command to create the table ‘Toyz’ with structure and constraint.
Table : TOYZ
Column_name Data Type(size) Constraint
Toy_no int(10) Primary Key
Toy_name varchar(20)
Type char(10)
Price decimal (8, 2)
Colour varchar(15)
Ans. Create table Toyz
(
Toy_no int(10) primary key,
Toy_name varchar(20),
54
Type char(10),
Price decimal(8, 2),
Colour varchar(15)
);
Q212. In a Database – SAMS and VENDOR are two tables with the following information. Write MySQL queries for (i) to
(iii), based on tables SAMS and VENDOR:
Table : SAMS Table: VENDOR
ICode IName Price Colour Vcode VCode VName
S001 Refrigerator 20000 Blue P01 P01 Satish
S002 Mobile Phone 45000 Black P02 P02 Manoj
S003 LCD 60000 Silver P03 P03 Subodh
S004 Washing Machine 12500 Smoke P01 P04 Jacob
S005 Air Conditioner 16000 White P03
(i) To display ICode, IName and VName of all the vendors, who manufacture “Refrigerator”.
Ans.
select SAMS.ICode, SAMS.IName, VENDOR.VName
from SAMS, VENDOR
where SAMS.Vcode=VENDOR.VCode;
SAMS.ICode SAMS.IName VENDOR.VName
S001 Refrigerator Satish
S002 Mobile Phone Manoj
S003 LCD Subodh
S004 Washing Machine Satish
S005 Air Conditioner Subodh
(ii) To display IName, ICode, VName and Price of all the products whose price is more than 20000.
Ans.
select SAMS.ICode, SAMS.IName, VENDOR.VName, SAMS.Price
from SAMS, VENDOR
where SAMS.Vcode=VENDOR.VCode and SAMS.Price>20000;
SAMS.ICode SAMS.IName VENDOR.VName SAMS.Price
S002 Mobile Phone Manoj 45000
S003 LCD Subodh 60000
(iii) To display vendor names and names of all items manufactured by vendor whose code is “P03”.
Ans.
select VENDOR.VName, SAMS.IName
from SAMS, VENDOR
where SAMS.Vcode=VENDOR.VCode and VENDOR.Vcode='P03';
VENDOR.VName SAMS.IName
Subodh LCD
Subodh Air Conditioner
(iv) With reference to SAMS table, which column should be set as the Primary key? Which column is the foreign
key? Give reasons.
Ans.
For SAMS TABLE
ICode primary key
VCode foreign key
Q213. A table FUNFOOD has 13 rows and 17 columns. What s the cardinality and degree of this table?
Ans. Cardinality = 13 rows
Degree = 17 columns.
Q214. A numeric column MONEY contains 34567.7896. Write a command to truncate MONEY
(i) Up to 2 decimal places( i. e. expected result 34567.78)
55
Ans. Select truncate(MONEY, 2);
(ii) Up to -3 places (i.e. expected result 34000)
Ans. Select truncate(MONEY, -3);
Q215. A numerica data field CHANGER contains 25565.7765. Write a commands to round off CHANGER
(i) upto 2 decimal places (i.e. expected result 25565.78)
Ans. Select round( CHANGER, 2);
(ii) Whole number (i.e. expected result 25566)
Ans. Select round(CHANGER, 0);
Q216. Table ‘Club’ has 4 rows and 3 columns. Table ‘Member’ has 2 rows and 5 columns. What will be the cardinality
of the Cartesian product of them?
Ans. Club table has 4 rows and Member table has 2 rows
Cardinality 4 X 2 = 8 rows
Q217. Consider the following table names ‘EXAM’ with details of marks. Write command of MySQL for (i) to (iv) and
output for (v) to (vii).
Table : EXAM
Adno SName Percentage Classsection Stream
B001 Sushant 90.2 12A Science
B002 Vaidyanath 80.5 12B Humanities
B003 Miara 68.9 12B Science
B004 Niara 96.0 12A Commerce
B005 Shinjini 88.9 12D Commerce
(i). To display all information of the students of humanities in descending order of percentage.
Ans.
select *
from EXAM
where Stream='Humanities'
order by Percentage desc;
(ii). To display Ado, SName, Percentage and Stream of those students whose name is less than 6 characters long.
Ans.
select Adno, SName, Percentage, Stream
from EXAM
where len(Sname)<6;
(iii). To add aother column Bus_Fees wth data type and size as Decimal(8, 2).
Ans.
alter table EXAM
add Bus_Fees Decimal(8, 2));
(iv). To increase percentage by 2% of all the Humanities students.
Ans.
update EXAM
set Percentage=Percentage+2
where Stream='Humanities';
(v). SELECT COUNT(*) FROM Exam;
Ans. 5
(vi). SELECT SName, Percentage FROM EXAM WHERE NAME LIKE “N%”;
Ans.
Sname Percentage
Niara 96.0
(vii). SELECT ROUNT(Percentage, 0) FROM EXAM;
Ans.
Percentage
56
90
81
69
96
89
Q218. Table: EXAM
No Name Stipend Subject Average Division
1 Karan 400 English 68 FIRST
2 Aman 680 Mathematics 72 FIRST
3 Javed 500 Accounts 67 FIRST
4 Bishakh 200 Informatics 55 SECOND
5 Sugandha 400 History 35 THIRD
6 suparna 550 Geography 45 THIRD
(i). To list the names of those students who have obtained Division as ‘FIRST’ in the ascending order of NAME.
Ans.
select Name from EXAM
where Division='FIRST'
order by Name;
(ii). To display a report listing NAME, SUBJECT and Annual stipend received assuing that the stipend column has
montly stipend.
Ans.
select Name, Subject, Stipend*12 as ‘Annual_stipend’
from EXAM;
(iii). To count the number of students, who have eigher Account or Informatics as Subject.
Ans.
select Subject, count(*)
from EXAM
group by Subject
having Subject='Accounts' or Subject='Informatics';
(iv). To insert a new row in the table EXAM:
6 ‘Mohan’ 500 “English” 73 “SECOND”
Ans. Insert into EXAM values (6,"Mohan", 500,"English", 73, "SECOND");
(v). SELECT AVG (Stipend) FROM EXAM WHERE DIVISION = “THIRD”;
Ans. avg = 475
(vi). SELECT COUNT (DISTINCT Subject) FROM EXAM;
Ans. count =6
(vii). SELECT MIN(Average) FROM EXAM WHERE Subject = “English” ;
Ans. min=68
Q219. Answer the following:
(i) Which command is used to make changes to the rows of a table in MySql?
Ans. UPDATE command
(ii) Identify the column which can be used as a Primary key in the table SHOP given below:
Table : Shop
SNo PName SName Qty Price City DOP
S1 Bread Britannia 150 8.00 Delhi 06-06-2007
S2 Cake Britannia 250 20.00 Mumbai 04-03-2006
S3 Coffee Nescafe 170 45.00 Mumbai 07-03-2006
S4 Chocolate Amul 380 10.00 Delhi 12-08-2006
Ans. Sno column is a primary key of Shop table.
57
(iii). Rahul has created the Table Bank.
Select * from Bank;
Describe Bank;
Help him to choose the command to:
(a) View the structrure of the table
Ans. describe Bank;
(c) Mrs. Sharma is using table STUDENTS with the following columns RNO, ADMNO, NAME, AGGREGATE
She wants to display all information of students in descending order of name and ascending order of
aggreagate. She wrote the following SQL query but she did not get the desired output.
SELECT * FROM STUDENTS
ORDER BY NAME, AGGREAGATE DESC;
Rewrite the above query to get the desired ouput.
Ans:
SELECT * FROM STUDENTS
ORDER BY AGGREAGATE, NAME DESC;
(d) Mr. Bose has created Table Furniture with 5 rows and 6 columns. What is the degree & cardinality of the
Table Furniture?
Ans. Degree: 6 rows, Cardinality : 5 columns
(e) Differentiate between Char and Varchar datatypes.
Char Datatype Varchar Datatype
It specifies a fixexd length character String. Blanks are It specifies a variable length string. No blanks are
added if a value is shorter than its length. added if the length is shorter than maximum length.
Q220. Write a command in MySql to create a table PRINTERS with the following structure
Field Name Data Type No. of Characters
P_ID Integer Primary Key
P_Name Varchar 20
P_Company Varchar 20
Price Integer
Ans.
Create table Printers
(
P_ID integer Primary Key,
P_Name varchar(20),
P_Company varchar(20),
Price integer
);
Q221: (ii) Write the MySql Command to change the size of P_Company field to 25 characters of above Printer table..
Ans. Alter table Printers modify P_COMPANY Varchar(25);
(iii) Write the MySql Command to add one more column DateofManf which can hold date values of above
Printer table.
Ans. Alter table Printers add DateofManf Date;
Q222: Given the following Family relation: Write MySql queries:
No. Name FemaleMembers MaleMembers Income Occupation
1 Sharma 3 2 7000 Service
2 Sud 4 1 50000 Business
58
3 Gupta 6 3 8000 Mixed
4 Bhatia 2 2 25000 Business
5 Goel 7 2 20000 Mixed
6 Joshi 3 2 14000 Service
7 Rao 6 3 5000 Farming
8 Kumar 5 2 10000 Service
(i) To display all the information of the members whose occupation is service.
Ans. SELECT * FROM FAMILY WHERE OCCUPATION = “SERVICE”
(ii) To display the name of family where female members are more than 3.
Ans. SELECT NAME FROM FAMILY WHERE FEMALEMEMBERS>3;
(iii) To display names of family with income in ascending order.
Ans. SELECT NAME, INCOME FROM FAMILY ORDER BY NAME;
(iv) To insert a new record in the family table with the following data : ‘Sachdeva’,2,1,15000, ‘Service’.
Ans. INSERT INTO FAMILY VALUES(‘SACHEVA’, 2, 1, 15000, ‘SERVICE’);
Q223 Select Pow(-2, 3);
will display the output:
Ans. -8
Q224 Which statement is used to delete data from a table?
Ans. Delete
Q225 Identify the correct MySql statement
(i) Delete * from Salesman;
(ii) Delete from Salesman;
Ans. Delete from Salesman;
Q226. Write the ouptut of the following MySql statements:
(i) SELECT ROUND (444.862, 1);
Ans: 444.9
(ii) SELECT LENGTH (‘Meena#Kumari###’);
Ans: 15
(iii) SELECT INSTR(‘HIMALAYAN QUEEN’, ‘MALA’);
Ans: 3
(iv) SELECT DAYOFMONTH(‘2010-02-26’);
Ans: 26
Q227. Consider the table Friends and write the output of the following queries :
Table: Friends
ID FirstName LastName Gender Age Hobbies City
101 Nita Sharma Female 42 Swimming Jaipur
102 Seema Gupta Female 50 Reading Puri
103 Dheeraj Kumar Male 36 Travelling Ahmedabad
104 Sandeep Arora Male 22 Reading Chandigarh
105 Rewa Kakkar Female 36 Cooking Shimla
106 Ankur Sood Male 30 Music Mathura
(i) SELECT CONCAT(FirstName, ‘ ‘,LastName) FROM FRIENDS where city = “Mathura”;
Ans. Ankur Sood
59
(ii) SELECT DISTINCT(Age) FROM FRIENDS;
Ans:
42
50
36
22
30
(iii) SELECT LOWER(LastName) FROM FRIENDS WHERE HOBBIES= ‘Travelling’;
Ans. kumar
(iv) SELECT MID(City,2,3) FROM FRIENDS WHERE GENDER=‘Male’;
Ans:
hme
han
ath
Q228 What is the difference between the following two statements?
(i) Select LastName from Directory where LastName like “_a%”;
Ans: Will show all the lastnames that have the second letter as ‘a’ followed by any no. of characters:
e.g. Batra
(ii) Select LastName from Directory where LastName like “%a”;
Ans: will show all the lastnames that end with the letter ‘a’ e.g. Batra, Bhatia, Sharma, Mehta
Q229 In CHAR(10) and VARCHAR(10), what does the number 10 indicate?
Ans It indicates the maximum number of characters that can be stored.
OR
It indicates the Size of the column.
Q230 ‘Employee’ table has a column named ‘CITY’ that stores city in which each employee resides.
Write SQL query to display details of all rows except those rows that have CITY as ‘DELHI’ or ‘MUMBAI’ or
‘CHANDIGARH’.
Ans SELECT * FROM Employee
WHERE CITY NOT IN ('DELHI','MUMBAI','CHANDIGARH');
OR
SELECT * FROM Employee
WHERE CITY <> 'DELHI' AND CITY <> 'MUMBAI' AND CITY <> 'CHANDIGARH' ;
OR
SELECT * FROM Employee
WHERE NOT CITY IN ('DELHI','MUMBAI','CHANDIGARH');
Q231 Consider the following table :
Table : RESULTS
STUDENTID NAME EXAMID SCORE
10 Leena 1 20
10 Leena 2 25
11 Samarth 1 30
11 Samarth 2 35
12 Jai 1 14
12 Jai 2 15
14 Shoaib 1 30
14 Shoaib 2 12
Abhay wants to know the number of students who took the test. He writes the following SQL statement to
count STUDENTID without duplicates. However the statement is not correct. Rewrite the correct statement.
SELECT DISTINCT (COUNT STUDENTID) FROM RESULTS;
60
Ans SELECT COUNT (DISTINCT STUDENTID) FROM RESULTS;
Q232 Aman has used the following SQL command to create a table ‘stu’:
CREATE TABLE stu
(
id INTEGER,
name VARCHAR(100)
);
Ans Then, Aman enters the following SQL statements to enter 3 rows:
Write the output that will be produced by the following SQL statement :
61
Ans. SELECT * FROM TRANSPORTER WHERE DRIVERGRADE IS NOT NULL;
Q238 To display names of drivers, names of items and travel dates for those items that are being transported
on or before 1st April 2019.
Ans. SELECT DRIVERNAME, ITEM, TRAVELDATE FROM TRANSPORTER
WHERE TRAVELDATE <= "2019-04-01";
Q239 To display the number of drivers who have ‘MOHAN’ anywhere in their names.
Ans. SELECT COUNT(DRIVERNAME) FROM TRANSPORTER
WHERE DRIVERNAME LIKE "%MOHAN%";
Q240 To display the names of drivers, item names and travel dates in alphabetic (ascending) order of driver
names.
Ans. SELECT DRIVERNAME, ITEM, TRAVELDATE FROM TRANSPORTER
ORDER BY DRIVERNAME; [OR ORDER BY DRIVERNAME ASC;]
Q241 To display names of drivers whose names are three characters long
Ans. SELECT DRIVERNAME FROM TRANSPORTER
WHERE DRIVERNAME LIKE "___";
OR
SELECT DRIVERNAME FROM TRANSPORTER
WHERE LENGTH(DRIVERNAME) = 3 ;
Q242 SELECT ITEM,COUNT(*) FROM TRANSPORTER GROUP BY ITEM
HAVING COUNT(*) > 1;
Ans. ITEM COUNT(*)
TELEVISION 3
REFRIGERATOR 2
Q243 SELECT MAX(TRAVELDATE)
FROM TRANSPORTER WHERE DRIVERGRADE=’A’;
Ans. MAX(TRAVELDATE)
2019-04-19
Q244 Mr. Sen has to create a table named ‘Employee’ with Columns to store EmpID, Name, Designation, Age
and Salary.EmpID is the Primary Key and Name cannot be NULL.
Some of the rows that will be inserted are shown below.
101 Smita Kumar Secretary 28 39500.00
102 Mani Scott Programmer 32 45300.00
103 Firdaus Ali Programmer II 45 67500.00
Write SQL query to create the above table with appropriate data types and sizes of columns
Ans. CREATE TABLE Employee
(
EmpID INTEGER PRIMARY KEY,
Name VARCHAR(20) NOT NULL ,
Designation VARCHAR(20),
Age INTEGER ,
Salary DECIMAL(8,2)
);
Q245 Ms. Rajshri is the class teacher of class XII .She wants to create a table named ‘Student' to store marks in
different subjects of her class. Identify any 4 columns for the table along with their suitable data types.
Ans. Admno INT/CHAR / VARCHAR
Name CHAR / VARCHAR
Mark1 DECIMAL / FLOAT / INT / INTEGER
Mark2 DECIMAL / FLOAT / INT / INTEGER
Q246 Consider the following tables PARTICIPANT and ACTIVITY and answer the questions that follow:
Table: PARTICIPANT Table: ACTIVITY
ADMNO NAME HOUSE ACTVITYCODE ACTIVITYCODE ACTIVITYNAME POINTS
6473 Kapil Shah Gandhi A105 A101 Running 200
7134 Joy Mathew Bose A101 A102 Hopping bag 300
8786 Saba Arora Gandhi A102 A103 Skipping 200
62
6477 Kapil Shah Bose A101 A104 Bean bag 250
7658 Faizal Ahmed Bhagat A104 A105 Obstacle 350
When the table “PARTICIPANT” was first created, the column ‘NAME’ was planned as the Primary key by
the Programmer. Later a field ADMNO had to be set up as Primary key. Explain the reason.
Ans. NAME column has duplicate values, cannot be considered as Primary key, therefore Admno is to be
considered as Primary Key.
Q247 Identify data type and size to be used for column ACTIVITYCODE in table ACTIVITY.
Ans. Data type : CHAR / VARCHAR
Size : 4
Q248 To display Activity Code along with number of participants participating in each activity (Activity Code
wise) from the table Participant.
Ans. SELECT ACTIVITYCODE, COUNT(*)
FROM PARTICIPANT
GROUP BY ACTIVITYCODE;
OR
SELECT CONCAT(ACTIVITYCODE,COUNT(*))
FROM PARTICIPANT
GROUP BY ACTIVITYCODE;
Q249 How many rows will be there in the cartesian product of the two tables in consideration here?
Ans. Number of Rows: 25
Q250 To display Names of Participants, Activity Code, Activity Name in alphabetic ascending order of names of
participants.
Ans. SELECT NAME, PARTICIPANT. ACTIVITYCODE, ACTIVITYNAME
FROM PARTICIPANT , ACTIVITY
WHERE PARTICIPANT.ACTIVITYCODE = ACTIVITY.ACTIVITYCODE
ORDER BY NAME ;
OR
SELECT NAME , P.ACTIVITYCODE , ACTIVITYNAME
FROM PARTICIPANT P, ACTIVITY A
WHERE P.ACTIVITYCODE = A.ACTIVITYCODE
ORDER BY NAME ;
OR
SELECT PARTICIPANT.NAME , PARTICIPANT.ACTIVITYCODE , ACTIVITY.ACTIVITYNAME
FROM PARTICIPANT , ACTIVITY
WHERE PARTICIPANT.ACTIVITYCODE = ACTIVITY.ACTIVITYCODE
ORDER BY PARTICIPANT.NAME ;
OR
SELECT P.NAME , P.ACTIVITYCODE , A.ACTIVITYNAME
FROM PARTICIPANT P , ACTIVITY A
WHERE P.ACTIVITYCODE = A.ACTIVITYCODE
ORDER BY P.NAME ;
Q251 To display Names of Participants along with Activity Codes and Activity Names for only those participants
who are taking part in Activities that have ‘bag’ in their Activity Names and Points of activity are above
250.
Ans. SELECT NAME , PARTICIPANT.ACTIVITYCODE , ACTIVITYNAME
FROM PARTICIPANT , ACTIVITY
WHERE PARTICIPANT.ACTIVITYCODE = ACTIVITY.ACTIVITYCODE AND POINTS > 250 AND ACTIVITYNAME
LIKE ‘%bag%’;
OR
SELECT NAME , P.ACTIVITYCODE , ACTIVITYNAME
FROM PARTICIPANT P, ACTIVITY A
WHERE P.ACTIVITYCODE = A.ACTIVITYCODE
AND POINTS > 250 AND ACTIVITYNAME LIKE '%bag%';
OR
63
SELECT PARTICIPANT.NAME , PARTICIPANT.ACTIVITYCODE , ACTIVITY.ACTIVITYNAME
FROM PARTICIPANT , ACTIVITY
WHERE PARTICIPANT.ACTIVITYCODE = ACTIVITY.ACTIVITYCODE AND ACTIVITY.POINTS > 250 AND
ACTIVTY.ACTIVITYNAME LIKE '%bag%';
OR
SELECT P.NAME , P.ACTIVITYCODE , A.ACTIVITYNAME
FROM PARTICIPANT P, ACTIVITY A
WHERE P.ACTIVITYCODE = A.ACTIVITYCODE AND A.POINTS > 250 AND A.ACTIVITYNAME LIKE '%bag%';
Q252 The TRUNCATE() function, truncates a number to the specified number of decimal places.
Select truncate(number, number of decimal places);
Write the output of following mysql code: Output:
SELECT TRUNCATE(135.375, -2); 100 (explain, -2 digit, truncate before decimal data 135)
SELECT TRUNCATE(135.375, -1); 130 (explain, -1 digit, truncate before decimal side data 135)
SELECT TRUNCATE(135.375, 0); 135 (explain, 0 digit, truncate after decimal side data .375)
SELECT TRUNCATE(135.375, 1); 135.3 (explain, 1 digit, truncate after decimal side data .375)
SELECT TRUNCATE(135.375, 2); 135.37 (explain, 2 digit, truncate after decimal side data .375)
SELECT TRUNCATE(135.375, 3); 135.375(explain, 3 digit, truncate after decimal side data .375)
Q253 The ROUND() function, rounds a number to a specified number of decimal places.
Select round(number, number of decimal places);
Write the output of following mysql code: Output:
SELECT round(135.375, -2); 100 (explain, -2 digit, round before decimal data 135)
SELECT round (135.375, -1); 140 (explain, -1 digit, round before decimal side data 135)
SELECT round (135.375, 0); 135 (explain, 0 digit, round after decimal side data .375)
SELECT round (135.375, 1); 135.4 (explain, 1 digit, round after decimal side data .375)
SELECT round (135.375, 2); 135.38 (explain, 2 digit, round after decimal side data .375)
SELECT round (135.375, 3); 135.375(explain, 3 digit, round after decimal side data .375)
Q254 The ROUND() function, rounds a number to a specified number of decimal places.
Select round(number, number of decimal places);
Write the output of following mysql code: Output:
SELECT round(135.565, -2); 100 (explain, -2 digit, round before decimal data 135)
SELECT round (135.565, -1); 140 (explain, -1 digit, round before decimal side data 135)
SELECT round (135.565, 0); 136 (explain, 0 digit, round after decimal side data .565)
SELECT round (135.565, 1); 135.6 (explain, 1 digit, round after decimal side data .565)
SELECT round (135.565, 2); 135.57 (explain, 2 digit, round after decimal side data . 565)
SELECT round (135.565, 3); 135.565(explain, 3 digit, round after decimal side data . 565)
Q255 The CEIL() function, returns the smallest integer value that is bigger than or equal to a number.
CEIL() function, rounds up the nearest integer
The FLOOR() function, returns the largest integer value that is smaller than or equal to a number.
FLOOR() function, rounds down the nearest integer
Write the output of following mysql code:
SELECT ceil(135.565); 136
SELECT ceil (-135.565); -135
SELECT floor(135.565); 135
SELECT floor (-135.565); -136
Q256 The MOD() function returns the remainder of a number divided by another number.
MOD(x, y)
Select mod(18, 4); 2 (remainder after division)
Or
64
Select 18 mod 4; 2 (remainder after division)
Or
Select 18 % 4; 2 (remainder after division)
70