0% found this document useful (0 votes)
4 views

Unit 2 MySQL - DBMS

abcd

Uploaded by

Bhavya Bhatt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Unit 2 MySQL - DBMS

abcd

Uploaded by

Bhavya Bhatt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 70

CLASS: 12 IP Term II Unit 2: Database Query using SQL Marks: 25

 Math functions: POWER (), ROUND (), MOD ().

 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 (*).

 Querying and manipulating data using Group by, Having, Order by


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

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

select * from student;

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

length(string) upper(string) or ucase(string) lower(string) or lcase(string) instr(string, substring)


It display total number of It display lower case string into It display upper case string into It display index postion of
characters including spaces. upper case string. lower case string. substirng in given string.

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

select * from student;

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

concat(string1, string2) ltrim(string) rtrim(string) trim(string)


It joins two or more strings It removes, only left side white It removes, only right side white It removes both, left & right side
together and display. space and then display. space and then display. white space and then display.

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:

sign(value) mod(value, divisor) power(base, exponent) sqrt(value)


It display 1,-1 and 0 It display remainder value after division. It display power of any value. It display squae root.

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); select truncate(52.356, 0); select ceil(52.0); select floor(52.0);


52 52 52 52

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

Date & Time Funtions:


MySQL Date Format ‘YYYY-MM-DD’ or ‘YYYY/MM/DD’
book table
dateofpurchase column
2018-10-28
2019-11-29
2020-12-30
curdate() date(date) day(date) month(date) year(date)
It show only today date. To store any date and To show day part value. To show month part To show year part
display. value. value.
select curdate(); select date(‘2021-12-31’); select day(‘2021-12-31’); select month(‘2021-12-31’); select year(‘2021-12-31’);
2022-01-06 2021-12-31 31 12 2021

select date(‘2020-11-12’); select day(‘2020-11-12’); select month(‘2020-11-12’); select year(‘2020-11-12’);


select now();
2022-01-06 22:09:57 2020-11-12 12 11 2020

select date(‘2019-10-28’); select day(‘2019-10-28’); select month(‘2019-10-28’); select year(‘2019-10-28’);


select sysdate();
2022-01-06 22:09:57 2019-10-28 28 10 2019

monthname(date) dayname(date) dayofmonth(date) dayofyear(date) sleep(seconds)


It show only month name. It show only day name. To show day part value. To show year(1-366). To pause process.
select monthname(‘2021-12-31’); select dayname(‘2021-12-31’); select dayofmonth(‘2021-12-31’); select dayofyear(‘2021-12-31’); select sleep(2);
Dec Friday 31 365 sleep for 2 seconds.

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.

11:25:21 after sleep 11:25:21 11:25:42 after sleep 11:25:44


now() sysdate()
So time in seconds not updated. So time in seconds updated.
book table
dateofpurchase column
2018-10-28
2019-11-29
2020-12-30

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

Set Operation Concept


T1 T2 Union output Intersect output Minus output Minus output
Left Right T1 Union T2 T1 Intersect T2 T1 – T2 T2 – T1
1 1 1 1 4 3
2 1 2 2
4 2 3
2 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

Grouping with having Concept (for selected group, here 1)


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
for selected group 1 for selected group 1 for selected group 1
for selected group 1 for selected group 1
1 T2 Sum T2 max T2 min T2 avg T2 count
1 1 2 1 1 1 1 1 1 1 2
2
2
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;

For joining operation, need at least two tables.


Joining Types: Here based on city column.
Cartesian – multiplication of both tables. Each row of right table interacts with all the rows of left table.
Equi - only matching rows of both tables will show. Non-matching rows of both tables will not show.
Left - only matching rows of left table will show. Non-matching rows of left table will show null in right table.
Right - only matching rows of right table will show. Non-matching rows of right table will show null in left table.

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;

Grouping Based on city columns values of student2 table.

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;

For selected group, must use having clasues.


To show only selected group based on city columns values of student2 table.
Here seleted group is ‘dwarka’.
To show sum of fees of To show maximum fees of To show minimum fees of To show average fees of
selected city of dwarka selected city of dwarka selected city of dwarka selected city of dwarka
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
having city=’dwarka’; having city=’dwarka’; having city=’dwarka’; having city=’dwarka’;

Here seleted group is ‘mithapur’.


To show sum of fees of To show maximum fees of To show minimum fees of To show average fees of
selected city of mithapur selected city of mithapur selected city of mithapur selected city of mithapur
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
having city=’mithapur’; having city=’mithapur’; having city=’mithapur’; having city=’mithapur’;

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;

Union Intersect Minus Minus


To show all rows, without okha and jamnager rows only okha will include, here only rajkot will include,
duplicate of both tables. will not include. Because student is first table. here student2 is first table.
they are uncommon.
select city select city select city select city
from student from student from student from student2
union intersect minus minus
select city select city select city select city
from student2; from student2; from student2; from student;
city city city
mithapur okha rajkot
dwarka
jamnagar

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

here left table is book


here right table is issue.

Q2. Write the output of following mysql code:


select *
from book join issue
on book.bid = issue.bid
Ans. bid bname bprice bid sname issuedate
b1 Python 350 b1 rohit 2020/12/11
b2 Java 450 b2 vinod 2021/11/09

Q3. Write the output of following mysql code:


select book.bid, issue.sname, issue.issuedate
from book join issue
on book.bid = issue.bid
Ans. bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2021/11/09

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

Q7. In above, what is database name?


Ans. library

Q8. In above, what are tables name?


Ans. book table
issue table

Q9. In above, What is primary key of book table?


Ans. bid

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.

Q11. Write mysql code to create above library database?


Ans. create database library;

Q12. Write mysql code to open above library database?


Ans. use library;

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,

foreign key (bid) references book(bid)


on update cascade
on delete cascade
);

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’);

Q17. Not null means………………………


Ans. It is compulsory to input values.

Q18. What is null value in mysql?


Ans. When information is not available or missing information so we cannot left blank, so we must use null.

Q19. library- Database name


book table issue table
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 various mysql code, for cross join between book and issue table.
here left table is book
here right table is issue.
Ans. select *
from book cross join issue;

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

b1 python 350 b2 vinod 2021/11/09


b2 java 450 b2 vinod 2021/11/09
b3 html 250 b2 vinod 2021/11/09
Book table have 3 rows
Issue table have 2 rows;
So, in cross join 3 X 2 = 6 rows.
Total 6 rows will display (each rows of issue table interacts with all the rows of book table).

Cardinality, after cross join, total 6 rows.


Degree, after cross join, total 6 columns.
14
Q21. library- Database name
book table issue table
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
What is cardinality and degree of book and issue table.
Ans. Cardinality, means total number of rows.
Book table cardinality 3 rows
Issue table cardinality 2 rows

Degree, means total number of columns.


Book table degree is 3 columns.
Issue table degree is 3 columns.

Q23. In above book, write mysql code to add new more column following

author varchar(30) not null

What is degree of book table?


Ans. alter table book add author varchar(30) not null;

After adding one more column author so degree of book table is 4 columns.

Q24. 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 left join between book and issue table without aliases
here left table is book
here right table is issue.
Ans. select *
from book left join issue
on book.bid = issue.bid;

here left table is book


here right table is issue.

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;

Q26. Write the output of following mysql code:


select *
from book b left join issue i
on b.bid = i.bid;
Ans. bid bname bprice bid sname issuedate
b1 Python 350 b1 rohit 2020/12/11
b2 Java 450 b2 vinod 2021/11/09
b3 html 250 Null Null Null

Q27. Write the output of following mysql code:


15
select b.bid, i.sname, i.issuedate
from book b left join issue i
on b.bid = i.bid;
Ans. bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2021/11/09
b3 Null Null

Q28. Write the output of following mysql code:


select book.bid, issue.sname, issue.issuedate
from book left join issue
on book.bid = issue.bid;
Ans. bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2021/11/09
b3 Null Null

Q29. Write the output of following mysql code:


select book.bid, issue.bid
from book left join issue
on book.bid = issue.bid;
Ans. bid bid
b1 b1
b2 b2
b3 Null

Q30. 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 right join between book and issue table without aliases
Ans. select *
from book right join issue
on book.bid = issue.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;

Q32. Write the output of following mysql code:


select *
from book b right join issue i
on b.bid = i.bid;
Ans. bid bname bprice bid sname issuedate
b1 Python 350 b1 rohit 2020/12/11
b2 Java 450 b2 vinod 2021/11/09

Q33. Write the output of following mysql code:


select b.bid, i.sname, i.issuedate
from book b right join issue i
on b.bid = i.bid;
16
Ans. bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2021/11/09

Q34. Write the output of following mysql code:


select book.bid, issue.sname, issue.issuedate
from book right join issue
on book.bid = issue.bid;
Ans. bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2021/11/09

Q35. Write the output of following mysql code:


select book.bid, issue.bid
from book right join issue
on book.bid = issue.bid;
Ans. bid bid
b1 b1
b2 b2

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

Q43. After union set, operation write the output :


fruit1 fruit2
Apple Apple
Orange Banana
Grapes Grapes
Ans. Apple
Banana
Orange
Grapes

Q44. After intersect set, operation write the output :


fruit1 fruit2
Apple Apple
Orange Banana
Grapes Grapes
Ans. Apple
Grapes

Q45. After minus set (fruit1-fruit2), operation write the output :


fruit1 fruit2
Apple Apple
Orange Banana
Grapes Grapes
Ans. Orange

19
Q46. After minus set (fruit2-fruit1), operation write the output :
fruit1 fruit2
Apple Apple
Orange Banana
Grapes Grapes
Ans. Banana

Q47. After union all set, operation write the output :


fruit1 fruit2
Apple Apple
Orange Banana
Grapes Grapes
Ans. Apple
Apple
Banana
Orange
Grapes
Grapes

Q48. library- database name


book table (relation) issue table (relation)
bid bid
b1 b1
b2 b1
b3 b2
b4 b2
b5
Write SQL command to perform union all set operation, between book and issue table and write output.
Ans. select bid Output:
from book bid
union all b1
select bid b1
from issue; b1
b2
b2
b2
b3
b4
b5

Q49. library- database name


book table (relation) issue table (relation)
bid bid
b1 b1
b2 b1
b3 b2
b4 b2
b5
Write SQL command to perform only union set operation, between book and issue table and write output.
Ans. select bid Output:
from book bid
union b1
select bid b2
from issue; b3
b4
b5
20
Q50. library- database name
book table (relation) issue table (relation)
bid bid
b1 b1
b2 b1
b3 b2
b4 b2
b5
Write SQL command to perform only intersect set operation, between book and issue table and write output.
Ans. select bid Output:
from book bid
intersect b1
select bid b2
from issue;

Q51. library- database name


book table (relation) issue table (relation)
bid bid
b1 b1
b2 b1
b3 b2
b4 b2
b5
Write SQL command to perform only minus set operation between (book – issue) tables and write output.
Ans. select bid Output:
from book bid
minus b3
select bid b4
from issue;

Q52. library- database name


book table (relation) issue table (relation)
bid bid
b1 b1
b2 b1
b3 b2
b4 b2
b5
Write SQL command to perform only minus set operation between (issue – book) tables and write output.
Ans. select bid Output:
from issue bid
minus b5
select bid
from book;

Q53. Explain Minus or set difference set operation.


Ans. Minus
 It combines the result of two SELECT statements. Minus operator is used to display the rows which are
present in the first query but absent in the second query.
 It has no duplicates and data arranged in ascending order by default.

Q54. Explain Intersect set operation.


Ans. Intersect
 It is used to combine two SELECT statements. The Intersect operation returns the common rows from
both the SELECT statements.
 In the Intersect operation, the number of data type and columns must be the same.
21
 It has no duplicates and it arranges the data in ascending order by default.

Q55. Explain Union set operation.


Ans. Union
 The SQL Union operation is used to combine the result of two or more SQL SELECT queries.
 In the union operation, all the number of data type and columns must be same in both the tables on
which UNION operation is being applied.
 The union operation eliminates the duplicate rows from its result set.

Q56. Explain Union All, set operation.


Ans. Union All
 Union All operation is equal to the Union operation. It returns the set without removing duplication and
sorting the data.

Q57. What is difference between natural join and equi join?


Ans. The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in such a way that, columns with the same name
of associated tables will appear once only.

Q58. Explain different types of MySql constraints.


Ans.
Constraints:
These are business rules or regulations which can be applied to rows & columns of a table. It ensure
database integrity. Following constraints are used when you create a table:-
primary key It is used to identify a record uniquely, in a table. It never accepts duplicate data in a column.
It is a key field of a table. It never holds null value.
foreign key It is used in another table. It is depend on primary key. It may have duplicate data.
Whenever two tables are related by a common column, then the related column in the
parent table should be declared a PRIMARY KEY constraint and related column in the child
table should have FOREIGN KEY constraint.
unique This Constraint ensures that no two rows have the same value in the specified columns.
check This Constraint limits values that can be inserted into a column of a table.
default A default value can be specified for a column. when a user does not enter a value for the
column, automatically the defined default value is inserted in the field.
not null This Constraint allows user must enter a value. Value can't be blank.

Q59. Explain different types of keys of MySql.


Ans. Keys- It is used to decide how rows in a relation are different logically. Keys types:
 Primary Key:- A Primary key is a set of one or more attributes that can uniquely identify tuples
within the relation. for example:- Rollno of a student table.
 Candidate Key:- All attributes combination inside a relation that can serve as primary key are
candidate keys as they are candidates for the primary key position.
for example:- Rollno and GRNo are candidate key.
 Alternate Key:- A Candidate Key that is not the primary key is called an Alternate key. If we select
Rollno of a student table as a primary key than remaining key (GRNo) will be a alternate key of a
student table.
 Foreign Key:- A non-key attribute whose values are derived from the primary key of some other
table, is known as Foreign key in its current table.
for example:- Rollno of a marks table.

Q60. Explain DDL and DML commands of DBMS.


22
Ans.
 DDL (Data Definition Language):- This command allows, to perform tasks related to data definition. we
can create, edit and delete table structure. for example:- Create, Alter, Drop Schema Objects and Grant
and revoke privileges and roles.

 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.

Q61. Student table


fees
100
100
200
300
What is the output of following mysql code:
select sum(fees)
from student;
Ans. 700
(It sum all values including duplicate values)

Q62. Student table


fees
100
100
200
300
What is the output of following mysql code:
select sum(distinct fees)
from student;
Ans. 600
(It does not sum duplicate value)

Q63. Student table


city
kota
kota
jaipur
null
What is the output of following mysql code:
select count(city)
from student;
Ans. 3
(It does not count null value)

Q64. Student table


city
kota
kota
jaipur
null
What is the output of following mysql code:
select count(distinct city)
from student;
Ans. 2
23
(It does not count duplicate and null value)

Q65. Student table


city
kota
kota
jaipur
null
What is the output of following mysql code:
select count(*)
from student;
Ans. 4
(It count all rows including single, duplicate and null also )

Q66. Explain referential integrity for the RDBMS concept.


Ans. Referential Integrity:- A referential integrity is a system of rules that a DBMS uses to ensure that relationships
between records in related tables are valid, and that users don't accidentally delete or change related data.
when referential integrity is enforced, you must observe the following rules:-
 The matching field from the primary table is a primary key or has a unique index. The related fields have
the same data types. Both tables belong to the same database.
 you can't enter a value in the foreign key field of the related table that doesn't exist in the primary key
of the primary table.
 you can't delete a record from a primary table if matching records exist in a related table.
 you can't change a primary key valued in the primary table, if that record has related records.

Q67. 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
What is the output of following mysql code?
select left(sname, 1) from issue;
Ans. left(sname,1)
r
v
a
v
r
a

Q68. 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
What is the output of following mysql code?
24
select left(sname, 2) from issue;
Ans. left(sname,2)
ro
vi
am
vi
ro
am

Q69. 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
What is the output of following mysql code?
select sname, left(sname, 2) from issue;
Ans. sname left(sname,2)
rohit ro
vinod vi
amit am
vinay vi
rohit ro
amit am

Q70. 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
What is the output of following mysql code?
select sname, left(sname, 2), right(sname, 2) from issue;
Ans. sname left(sname,2) right(sname,2)
rohit ro it
vinod vi od
amit am it
vinay vi ay
rohit ro it
amit am it

Q71. 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
What is the output of following mysql code?
25
select sname, left(sname, 2), right(sname, 2), length(sname) from issue;
Ans. sname left(sname,2) right(sname,2) length(sname)
rohit ro it 5
vinod vi od 5
amit am it 4
vinay vi ay 5
rohit ro it 5
amit am it 4

Q72. 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
What is the output of following mysql code?
select sname, left(sname, 2), right(sname, 2), length(sname), upper(sname) from issue;
Ans. sname left(sname,2) right(sname,2) length(sname) upper(sname)
rohit ro it 5 ROHIT
vinod vi od 5 VINOD
amit am it 4 AMIT
vinay vi ay 5 VINAY
rohit ro it 5 ROHIT
amit am it 4 AMIT

Q73. 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
What is the output of following mysql code?
select sname, left(sname, 2), right(sname, 2), length(sname), upper(sname)
from issue
where sname=”rohit”;
Ans. sname left(sname,2) right(sname,2) length(sname) upper(sname)
rohit ro it 5 ROHIT
rohit ro it 5 ROHIT

Q74. 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
What is the output of following mysql code?
select sname, left(sname, 3), right(sname, 3), length(sname), upper(sname)
from issue
26
where sname=”amit”;
Ans. sname left(sname,3) right(sname,3) length(sname) upper(sname)
amit ami mit 4 AMIT
amit ami mit 4 AMIT

Q75. 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
What is the output of following mysql code?
select sname, left(sname, 3), right(sname, 3), length(sname), upper(sname)
from issue
where sname=”vinay”;
Ans. sname left(sname,3) right(sname,3) length(sname) upper(sname)
vinay vin nay 5 VINAY

Q76. 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
What is the output of following mysql code?
select sname, left(sname, 2), right(sname, 2), length(sname), upper(sname)
from issue
where sname=”vinod” or sname=”vinay”;
Ans. sname left(sname,2) right(sname,2) length(sname) upper(sname)
vinod vi od 5 VINOD
vinay vi ay 5 VINAY

Q77. 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
What is the output of following mysql code?
select sname, left(sname, 1), right(sname, 2), length(sname), upper(sname)
from issue
where sname like ‘v%’ ;
Ans. sname left(sname, 1 ) right(sname,2) length(sname) upper(sname)
vinod v od 5 VINOD
vinay v ay 5 VINAY

Q78. issue table (relation)


27
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
What is the output of following mysql code?
select sname, left(sname, 1), right(sname, 2), length(sname), upper(sname)
from issue
where sname like ‘%it’ ;
Ans. sname left(sname, 1) right(sname,2) length(sname) upper(sname)
rohit r it 5 ROHIT
amit a it 4 AMIT
rohit r it 5 ROHIT
amit a it 4 AMIT

Q79. 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
What is the output of following mysql code?
select sname, left(sname, 1), right(sname, 2), length(sname), upper(sname)
from issue
where bid = ‘b3’ ;
Ans. sname left(sname, 1) right(sname,2) length(sname) upper(sname)
vinay vi ay 5 VINAY

Q80. 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
What is the output of following mysql code?
select bid, sname, left(sname, 1), right(sname, 2), length(sname), upper(sname)
from issue
where bid = ‘b2’ ;
Ans. bid sname left(sname, 1) right(sname,2) length(sname) upper(sname)
b2 vinod v od 5 VINOD
b2 vinay v ay 5 VINAY
b2 rohit r it 5 ROHIT
b2 amit a it 4 AMIT

Q81. issue table (relation)


bid sname issuedate
b1 rohit 2020/12/11
b2 vinod 2020/11/09
28
b1 amit 2021/10/08
b3 vinay 2021/08/06
b2 rohit 2019/08/07
b2 amit 2018/05/03
What is the output of following mysql code?
select concat(bid, sname)
from issue;
Ans. concat(bid, sname)
b1rohit
b2vinod
b1amit
b3vinay
b2rohit
b2amit

Q82. 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
What is the output of following mysql code?
select concat(sname, bid)
from issue;
Ans. concat(sname, bid)
rohitb1
vinodb2
amitb1
vinayb3
rohitb2
amitb2

Q83. 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
What is the output of following mysql code?
select concat(sname, length(sname))
from issue;
Ans. concat(sname, length(sname))
rohit5
vinod5
amit4
vinay5
rohit5
amit4

Q84. issue table (relation)


29
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
What is the output of following mysql code?
select concat(sname, length(bname)) as ‘name with length’
from issue;
Ans. name with length
rohit5
vinod5
amit4
vinay5
rohit5
amit4

Q85. 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
What is the output of following mysql code?
select mid(sname, 2, 3)
from issue;
Ans. mid(sname, 2, 3)
ohi
ino
mit
ina
ohi
mit

Q86. 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
What is the output of following mysql code?
select mid(sname, 3, 2)
from issue;
Ans. mid(sname, 3, 2)
hi
no
it
na
hi
30
it

Q87. 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
What is the output of following mysql code?
select substr(sname, 2, 3)
from issue;
Ans. substr(sname, 2, 3)
ohi
ino
mit
ina
ohi
mit

Q88. 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
What is the output of following mysql code?
select substr(sname, 3, 2)
from issue;
Ans. substr(sname, 3, 2)
hi
no
it
na
hi
it

Q89. 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
What is the output of following mysql code?
select instr(sname, ”it”)
from issue;
Ans. instr(sname, ”it”)
4
0
31
3
0
4
3

Q90. 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
What is the output of following mysql code?
select length(concat(bid, sname))
from issue;
Ans. length(concat(bid, sname))
7
7
6
7
7
6

Q91. 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
What is the output of following mysql code?
select length(issudate)
from issue;
Ans. length(issuedate)
10
10
10
10
10
10

Q92. 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
What is the output of following mysql code?
select right(issuedate,2)
from issue;
32
Ans. right(issuedate, 2)
11
09
08
06
07
03

Q93. 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
What is the output of following mysql code?
select left(issuedate,2)
from issue;
Ans. left(issuedate, 4)
2020
2020
2021
2021
2019
2018

Q94. 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
What is the output of following mysql code?
select mid(issuedate, 6, 2)
from issue;
Ans. mid(issuedate, 6, 2)
12
11
10
08
08
05

Q95. To remove front and rear spaces of any given string, so we can use:
Ans. trim()

Q96. To remove front spaces of any given string, so we can use:


Ans. ltrim()

Q97. To remove rear spaces of any given string, so we can use:


33
Ans. rtrim()

Q98. What is the output of following mysql code?


select trim(“ data “);
Ans. data
it removed front and rear space of above string.

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.

Q102. What is the output of following mysql code?


select sqrt(144);
Ans. 12

Q103. What is the output of following mysql code?


select sqrt(140+2+2);
Ans. 12

Q104. What is the output of following mysql code?


select power(3, 2), power(2, 3);
Ans. power(3, 2) power(2, 3)
9 8

Q105. What is the output of following mysql code?


select power(4, 3), power(3, 4);
Ans. power(4, 3) power(3, 4)
64 81

Q106. Suppose you have x database,


in x database, t1 and t2 are two tables,
in t1 table, c1 column and have following values
in t2 table, c2 column and have following values
write mysql code for cross, equi, left, right join and also write output.
t1 t2
c1 c2
1 1
2 1
34
3 2
5 4
Ans. Cross join Equi join Left join Right join
t1 table have 4 rows Matching rows of both t1 is left table t1 is left table
t2 table have 4 rows tables will show and t2 is right table t2 is right table
t1 x t2 non matching rows Matching rows of left Matching rows of right
will not include table will show and table will show and
4X4
16 rows non matching rows of non matching rows of
right table will show left table will show
null null
select * select * select * select *
from t1 cross join t2; from t1 join t2 from t1 left join t2 from t1 right join t2
on t1.c1= t2.c2; on t1.c1= t2.c2; on t1.c1= t2.c2;
Output Output Output Output
t1 X t2 t1 t2 t1 t2 t1 t2
c1 c2 c1 c2 c1 c2 c1 c2

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.

Q109. Difference between now() and sysdate() function:


Ans. 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 in seconds, changed.
changed.

11:25:21 after sleep 11:25:21 11:25:42 after sleep 11:25:44


now() Sysdate()
So time in seconds not updated. So time in seconds updated.

Q110. 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
What is the output of following mysql code?
select day(issuedate)
from issue;
Ans. day(issuedate)
11
9
8
6
36
7
3

Q111. 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
What is the output of following mysql code?
select month(issuedate), monthname(issuedate), year(issuedate)
from issue;
Ans. month(issuedate) monthname(issuedate) year(issuedate)
12 December 2020
11 November 2020
10 October 2021
8 August 2021
8 August 2019
5 May 2018

Q112. 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
What is the output of following mysql code?
select dayname(issuedate), dayofmonth(issuedate), dayofyear(issuedate)
from issue;
Ans. dayname(issuedate) dayofmonth(issuedate) dayofyear(issuedate)
Friday 11 346
Monday 9 314
Friday 8 281
Friday 6 218
Wednesday 7 219
Thursday 3 123

Q113. 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
Write mysql command to show issuedate in ascending order (old to recent) and write output?
Ans. select issuedate
from issue
order by issuedate asc;
issuedate
2018/05/03
37
2019/08/07
2020/11/09
2020/12/11
2021/08/06
2021/10/08

Q114. 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
Write mysql command to show issuedate in descending order (recent to old) and write output?
Ans. select issuedate
from issue
order by issuedate desc;
issuedate
2021/10/08
2021/08/06
2020/12/11
2020/11/09
2019/08/07
2018/05/03

Q115. 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
Write mysql command to show only issuedate from ‘2019-01-01’ to ‘2020-12-31’ and write output?
Ans. select issuedate
from issue
where issuedate between ‘2019-01-01’ and ‘2020-12-31’;
issuedate
2020/12/11
2020/11/09
2019/08/07

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 ),

foreign key (sid) references student(sid)


on update cascade
on delete cascade

)engine=innodb;

Q118. student table


rollno Name dob fees
101 raj 2003/12/11 4000
102 himmat 2002/01/10 5000
103 akash 2001/08/14 3000
Write mysql commands to insert above records in student table?
Ans. insert into student values(101, ‘raj’, ‘2003/12/11’, 4000);
insert into student values(102, ‘himmat’, ‘2002/01/10’, 5000);
insert into student values(103, ‘akash’, ‘2001/08/14’, 3000);

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;

Q133. Write mysql command to drop bank database.


Ans. use bank;
drop database bank;

Q134. Write mysql command to drop hotel database.


Ans. use hotel;
drop database hotel;

Q135. Write mysql command to drop library database.


Ans. use library;
drop database library;

Q136. Create, alter, drop are …………………. Commands.


Ans. DDL (Data Definition commands)

Q137. Select, insert, update are …………………. Commands.


Ans. DML (Data Manipulation commands)

Q138. student table


rollno name dob fees
101 raj 2003/12/11 4000
102 himmat 2002/01/10 5000
103 akash 2001/08/14 3000
Write mysql command to add a new column city with char(30) data type and not null constraints in above
student table.
Ans. alter table student
add city char(30) not null;

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);

Q147. To drop/delete column heightnew to the student table.


Ans. alter table student
drop column heightnew;

Q148. To drop/delete column statenew to the student table.


Ans. alter table student
drop column statenew;

Q149. To drop/delete column citynew to the student table.


Ans. alter table student
drop column citynew;

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.

Q154. Difference between primary key and foreign key.


Ans.
 Primary Key:- A Primary key is a set of one or more attributes that can uniquely identify tuples within the
relation. for example:- Rollno of a student table.
 Foreign Key:- A non-key attribute whose values are derived from the primary key of some other table, is
known as Foreign key in its current table. for example:- Rollno of a marks table.

Q155. Explain TCL commands with Commit and rollback also


Ans.  TCL (Transaction Control Language):- It is used to manage transactions. (A transaction is one complete
unit of work). Some example of TCL commands are:- COMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION.
COMMIT:- It makes all the changes made by statements issued, permanent. After update and delete
command, we must use commit.
ROLLBACK:- It undoes all changes since the beginning of a transaction or since a savepoint.

Q156. Domain of a column means…………..


Ans. A set of permitted values for a particular column or attributes.
For example : In rollno column will input, minimum 101 and maximum 10000. So all rollno will input between
this range so it is a domain of rollno column.

Q157. Write mysql command to rename above student table into studentnew.
Ans. Alter table student rename to studentnew;

Q158. Write mysql command to rename book table into booknew.


Ans. Alter table book rename to booknew;

Q159. student table


rollno name dob fees
101 raj 2003/12/11 4000
102 himmat 2002/01/10 5000
103 akash 2001/08/14 3000
Write mysql command to show only rollno column of above student table.
Ans. select rollno
from student;

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;

Q167. Write mysql command to show with title


rollno as ‘student rollno’ , fees as ‘student fees’, name as ‘student name’, rollno column of above student
table.
Ans. select rollno as ‘student rollno’, name as ‘student name’, fees as ‘student fees’
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’ ;

Q181. What is difference between a primary key and unique key?


Ans.  Both keys provide a guaranteed uniqueness for a column or a set of columns in a table or relation.
 The main difference among them is that the primary key identifies each record in the table, and the unique
key prevents duplicate entries in a column except for a NULL value.
 Primary key can store null value, whereas a unique key cannot store null value.

Q182. Write the full forms followings and some examples


Ans. DBMS: Database Management System
RDBMS: Relational Database Management System MS Access, Oracle, MySQL
DDL: Data Definition Language Create, Alter , Drop
DML: Data Manipulation Language Select, Insert, Update, Drop
TCL: Transaction Control Language Commit, Rollback
SQL: Structured Query Language
ASCII: American Standard Code for International Interchange

Q183. To view all the columns with all rows of the current table, we can use ………..
Ans. *

Q184. Pattern matching operator in mysql.


Ans. SQL pattern matching enables you to use
_ to match any single character and
% to match an arbitrary number of characters (including zero characters).
44
Q185. Which keyword is used in pattern matching?
Ans. In SQL, the LIKE keyword is used to search for patterns. Pattern matching employs wildcard characters to match
different combinations of characters.
The LIKE keyword indicates that the following character string is a matching pattern.

The LIKE clause uses the following symbols known as wildcard operators in SQL to perform this pattern-
matching task in SQL.

1. To represent zero, one or more than one character, % (percentage) is used.


2. To represent a single character _ (underscore) is used.
Examples of pattern matching:
Employee_details table
ID Name City Salary Age

1 Priyanka Bagul Nasik 26000 20


2 Riya Sharma Mumbai 72000 28
3 Neha Verma Varanasi 37000 19
4 Neeta Desai Nasik 39500 21
5 Priya Wagh Udaipur 60000 32

SELECT * FROM employee_details WHERE Name LIKE 'Pr%';

ID Name City Salary Age

1 Priyanka Bagul Nasik 26000 20


5 Priya Wagh Udaipur 60000 32

SELECT * FROM employee_details WHERE Name LIKE '%ya%';

ID Name City Salary Age

1 Priyanka Bagul Nasik 26000 20


2 Riya Sharma Mumbai 72000 28
5 Priya Wagh Udaipur 60000 32

SELECT * FROM employee_details WHERE City LIKE '%i';

ID Name City Salary Age

2 Riya Sharma Mumbai 72000 28

3 Neha Verma Varanasi 37000 19

SELECT * FROM employee_details WHERE Age LIKE '2%'


ID Name City Salary Age

1 Priyanka Bagul Nasik 26000 20


2 Riya Sharma Mumbai 72000 28
4 Neeta Desai Nasik 39500 21
45
SELECT * FROM employee_details WHERE City LIKE 'Na_ik';
ID Name City Salary Age

1 Priyanka Bagul Nasik 26000 20


4 Neeta Desai Nasik 39500 21

SELECT * FROM employee_details WHERE Salary LIKE '3__00';


ID Name City Salary Age

3 Neha Verma Varanasi 37000 19


4 Neeta Desai Nasik 39500 21

SELECT * FROM employee_details WHERE Name LIKE '_ _ _ _ a%';

In starting Four times underscore in name


ID Name City Salary Age

1 Priyanka Bagul Nasik 26000 20


4 Neeta Desai Nasik 39500 21
5 Priya Wagh Udaipur 60000 32

Q186.
ID Name City Salary Age

int varchar(30) varchar(20) integer(5) interger(2)


Primary key Not null Not null Not null Not null
Write mysql code, to create above employee_details table with above column names and its data types with
constraints.
Ans create table employee_details
(
ID int primary key,
Name varchar(30) not null,
City varchar(20) not null,
Salary int(5) not null,
Age int(2) not null
);

Q187. MySQL is an ………………. and ……………..


Ans. Open Source RDBMS software and not case sensitive language.

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

1 Priyanka Bagul mithapur 26000 20


2 Riya Sharma mithapur 72000 28
3 Neha Verma mithapur 37000 19
4 Neeta Desai mithapur 39500 21
5 Priya Wagh mithapur 60000 32

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

1 Priyanka Bagul mithapur 36000 20


2 Riya Sharma mithapur 82000 28
3 Neha Verma mithapur 47000 19
4 Neeta Desai mithapur 49500 21
5 Priya Wagh mithapur 70000 32

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

1 Priyanka Bagul mithapur 36000 20


2 Riya Sharma mithapur 87000 28
3 Neha Verma mithapur 47000 19
4 Neeta Desai mithapur 49500 21
5 Priya Wagh mithapur 70000 32

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

1 Priyanka Bagul mithapur 36000 25


2 Riya Sharma mithapur 87000 28
3 Neha Verma mithapur 47000 24
4 Neeta Desai mithapur 49500 26
5 Priya Wagh mithapur 70000 32

Q192. In above employee_details, to change details of following, who have ID 5.


Name change Peter
City change Rajkot
Salary change 90000
Age change 45
Ans. update employee_details
set Name=’Peter’, City=’Rajkot’, Salary = 90000, Age=45
where ID=5;
ID Name City Salary Age

1 Priyanka Bagul mithapur 36000 25


2 Riya Sharma mithapur 87000 28
47
3 Neha Verma mithapur 47000 24
4 Neeta Desai mithapur 49500 26
5 Peter Rajkot 90000 45

Q193. Which key word is used to eliminate duplicate or redundant data in query result?
Ans Distinct

 By default data is selected from all the rows of the table.


 But DISTINCT keyword eliminates(removes) duplicates rows from the results of a SELECT statement.
 using DISTINCT only one null value is returned if so many null value presents.

Q194. student table


scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

Write mysql code, in above student table display scity column.


Ans. select scity
from student;

Q195. student table


scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

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

What is the output of following mysql code?


select count(scity)
from student;
Ans. 7 rows

Q197. student table


scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

What is the output of following mysql code?


select count(distinct scity)
from student;
Ans. 5 rows
Duplicate scities removed by distinct, so total 5 rows.

Q198. student table


scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

What is the output of following mysql code?


select scity, count(scity)
from student
group by scity;
Ans. scity count(scity)
kota 2
pali 1
jaipur 2
udaipur 1
ajmer 1

49
Q199. student table
scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

What is the output of following mysql code?


select scity
from student
where scity = ‘kota’;
Ans. scity
kota
Kota

Q200. student table


scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

What is the output of following mysql code?


select scity
from student
where scity = ‘jaipur’ or scity=’kota’;
Ans. scity
kota
Kota
jaipur
jaipur

Q201. student table


scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

What is the output of following mysql code?


select scity
from student
where scity in (‘kota’, ‘jaipur’) ;

50
Ans. scity
kota
Kota
jaipur
jaipur

Q202. student table


scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

What is the output of following mysql code?


select scity
from student
where scity not in (‘kota’, ‘jaipur’) ;
Ans. scity
pali
udaipur
ajmer

Q203. student table


scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

What is the output of following mysql code?


select count(scity)
from student
where scity not in (‘kota’, ‘jaipur’) ;
Ans. count(scity)
3
Q204. student table
scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

What is the output of following mysql code?


51
select count(scity)
from student
where scity in (‘kota’, ‘jaipur’) ;
Ans. count(scity)
4
Q205. student table
scity
kota
kota
pali
jaipur
jaipur
udaipur
ajmer

What is the output of following mysql code?


select count(distinct scity)
from student
where scity in (‘kota’, ‘jaipur’) ;
Ans. count(scity)
2
Distinct removes duplicate cities( kota and jaipur) ant then count only one time (kota and jaipur).
Q206. Write an SQL query to create the table ‘SALESPERSON’ with the following structure:
Table: SALESPERSON
Field Type Size Constraint
SCODE Decimal 6 Primary Key
FIRSTNAME Varchar 30 Not Null
LASTNAME Varchar 30 Not Null
CITY Varchar 30
SALES Decimal 8
Ans. Create table SALESPERSON
(
SCODE integer(6) Primary Key,
FIRSTNAME varchar(30) Not Null,
LASTNAME varchar(30) Not Null,
CITY varchar(30),
SALES decimal(8,0)
);
Q207. In a database there are two tables ‘CD’ and ‘TYPE’ as shown below:
Table :CD Table : TYPE
CODE TITLE DURATION SINGER CATEGORY CATEGORY DESCRIPTION
101 Sufi Songs 50 min Zakir Faiz 12 1 Jazz
102 Eureka 45 min Shyama Mukherjeet 12 12 Classical
103 Nagmey 23 min Sonvi Kumar 77 40 Country Side
104 Dosti 35 min Bobby 1 78 Pop
(i) Name the Primary key in ‘CD’ table.
Ans. CODE
(ii) Name the foreign key in ‘CD’ table.
Ans. CATEGORY
(iii) Write the Cardinality and Degree of “TYPE” table.
Ans. Cardinality = 4 rows, Degree = 2 columns
(iv) Check every value in CATEGORY column of both the tables. Do you find any discrepancy? State the
discrepancy.
Ans. 77 value does not exist in ‘TYPE’ table. It has violated referential integrity rules. First 77 value, must enter in
‘TYPE’ table because CATEGORY is the primary key of ‘TYPE’ table, then we can input in ‘CD’ table because
CATEGORY is the foreign key of ‘CD’ table.

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;

(b) View the contents of the table


Ans. Select * from 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:

INSERT INTO stu VALUES(1,"abc");


INSERT INTO stu VALUES(2,"abc");
INSERT INTO stu VALUES(3,"bcd");

Write the output that will be produced by the following SQL statement :

SELECT name, Count(*)


FROM stu
GROUP BY name;
NAME COUNT(*)
abc 2
bcd 1
Q233 Write SQL command to add a column “COUNTRY” with data type and size as VARCHAR(70) to the existing
table named “PLAYER”. Is it a DDL or DML or TCL command?
Ans ALTER TABLE PLAYER ADD COUNTRY VARCHAR(70);
It is a DDL command
Q234 Consider the following table ‘Transporter’ that stores the order details about items to be transported.
Write SQL commands for the statements (i) to (viii) and write output for SQL queries (ix) and (x)
Table : TRANSPORTER
ORDERNO DRIVERNAME DRIVERGRADE ITEM TRAVELDATE DESTINATION
10012 RAM YADAV A TELEVISION 2019-04-19 MUMBAI
10014 SOMNATH SINGH FURNITURE 2019-01-12 PUNE
10016 MOHAN VERMA B WASHING MACHINE 2019-06-06 LUCKNOW
10018 RISHI SINGH A REFRIGERATOR 2019-04-07 MUMBAI
10019 RADHE MOHAN TELEVISION 2019-05-30 UDAIPUR
10020 BISHEN PRATAP B REFRIGERATOR 2019-05-02 MUMBAI
10021 RAM TELEVISION 2019-05-03 PUNE
To display names of drivers and destination city where TELEVISION is being transported.
Ans SELECT DRIVERNAME, DESTINATION FROM TRANSPORTER
WHERE ITEM="TELEVISION";
Q235 To display driver names and destinations where destination is not MUMBAI.
Ans. SELECT DRIVERNAME, DESTINATION FROM TRANSPORTER
WHERE DESTINATION <> "MUMBAI";
OR
SELECT DRIVERNAME, DESTINATION
FROM TRANSPORTER WHERE DESTINATION != "MUMBAI";
OR
SELECT DRIVERNAME, DESTINATION FROM TRANSPORTER
WHERE DESTINATION NOT IN ("MUMBAI");
OR
SELECT DRIVERNAME, DESTINATION FROM TRANSPORTER
WHERE NOT DESTINATION = "MUMBAI";
Q236 To display the names of destination cities where items are being transported. There should be no
duplicate values.
Ans. SELECT DISTINCT(DESTINATION) FROM TRANSPORTER;
Q237 To display details of rows that have some value in DRIVERGRADE column.

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)

Q257 Char() function return, ASCII character of a given number.


ASCII Numbers ASCII Characters
65 to 90 A to Z
97 to 122 a to z
48 to 57 0 to 9
Write the output of following mysql code: output
select char(65); A
select char(66); B
select char(97); a
select char(98); b

Q258 ASCII() function return, ascii number of a given ascii character.


ASCII Characters ASCII Numbers
A to Z 65 to 90
a to z 97 to 122
0 to 9 48 to 57
Write the output of following mysql code: output
select ascii(‘A’); 65
select ascii(‘B’); 66
select ascii(‘a’); 97
select ascii(‘b’); 98

Q259 student table


name fees
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see total fees paid by each student:
student table
name total fees paid
akash 5
ram 7
Ans. select name, sum(fees) as ‘total fees paid’
from student
group by name;

Q260 student table


name fees
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see minium fees paid by each student:
student table
65
name minimum fees paid
akash 1
ram 2
Ans. select name, min(fees) as ‘minium fees paid’
from student
group by name;

Q261 student table


name fees
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see maximum fees paid by each student:
student table
name maximum fees paid
akash 4
ram 5
Ans. select name, max(fees) as ‘maximum fees paid’
from student
group by name;

Q262 student table


name fees
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see average fees paid by each student:
student table
name average fees paid
akash 2.5
ram 3.5
Ans. select name, avg(fees) as ‘average fees paid’
from student
group by name;

Q263 student table


name fees
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see total number of fees paid by each student:
student table
name Total number of fees paid
akash 2
ram 2
Ans. select name, count(fees) as ‘total number of fees paid’
from student
66
group by name;

Q264 student table


name fees
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see total fees paid by only ‘ram’:
student table
name total fees paid by ram
ram 7
Ans. select name, sum(fees) as ‘total fees paid by ram’
from student
group by name
having name=”ram”;

Q265 student table


name fees
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see total fees paid by only ‘akash’:
student table
name total fees paid by akash
akash 5
Ans. select name, sum(fees) as ‘total fees paid by akash’
from student
group by name
having name=”akash”;

Q266 student table


name fees
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see maximum fees paid by only ‘ram’:
student table
name maximum fees paid by ram
ram 5
Ans. select name, max(fees) as ‘maximum fees paid by ram’
from student
group by name
having name=”ram”;

Q267 student table


name fees
ram 5
akash 4
67
ram 2
akash 1
Write mysql code to generate following output, to see maximum fees paid by only ‘akash’:
student table
name maximum fees paid by akash
akash 4
Ans. select name, max(fees) as ‘maximum fees paid by akash’
from student
group by name
having name=”akash”;

Q268 student table


depositdate fees
2018/01/10 5
2018/01/11 4
2018/01/12 2
2018/01/13 1
2019/02/02 1
2019/02/03 3
2019/02/04 4
Write mysql code to generate following output, to see year wise, total fees collected for each month of all years:
student table
monthname(depositdate) sum(fees)
January 12
February 8
Ans. select monthname(depositdate), sum(fees)
from student
group by month(depositdate);

Q269 student table


depositdate fees
2018/01/10 5
2018/01/11 4
2018/01/12 2
2018/01/13 1
2019/02/02 1
2019/02/03 3
2019/02/04 4
Write mysql code to generate following output, to see year wise, total fees collected for each year:
student table
year(depositdate) sum(fees)
2018 12
2019 8
Ans. select year(depositdate), sum(fees)
from student
group by year(depositdate);

Q270 student table


name fees
ram 5
68
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see all names in ascending order (A to Z) :
student table
name fees
akash 1
akash 4
ram 2
ram 5
Ans. select *
from student
order by name asc;

Q271 student table


name fees
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see all names in descending order (Z to A) :
student table
name fees
ram 5
ram 2
akash 4
akash 1
Ans. select *
from student
order by name desc;

Q272 student table


name fees
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see all fees in descending order (9 to 0) :
student table
name fees
ram 5
akash 4
ram 2
akash 1
Ans. select *
from student
order by fees desc;

Q273 student table


name fees
69
ram 5
akash 4
ram 2
akash 1
Write mysql code to generate following output, to see all fees in ascending order (0 to 9) :
student table
name fees
akash 1
ram 2
akash 4
ram 5
Ans. select *
from student
order by fees asc;

70

You might also like