Mysql Full Notes
Mysql Full Notes
Definitions(Quick review)
MySQL: MySQL is an open-source relational database management system.
DBMS: Database management system is a software which is used to create, modify and manage
the database: Eg: MYSQL ,oracle
Char datatype: fixed length memory location used to store characters(1-255 characters)
varChar datatype: variable length memory location used to store characters(65535 characters)
8)alter:it is a DDL command, used to add, delete, or modify columns in an existing table.
9)where: The WHERE clause is used to filter records.WHERE clause is used to extract only
those records that fulfill a specified condition.
10)update:it is a DML command it is used to modify existing data in a table.
11)order by:it is used to display the values in a table in ascending(asc) or descending(desc) order.
12)between-it is used to display within a range of values (inclusive). begin and end values are
included.
14)like -it is used to display data in database which match certain patterns.
Two wild card characters with like operator:
% Used to match zero or more characters.
_ Used to match one or single character
Keys in database:
1)Primary Key – A primary is a column in a table that uniquely identifies tuples
(rows) in that table
1. Which of the following is the correct syntax to add a field using alter command?
a) ALTER TABLE table_name ADD field_name data type;
b) ALTER TABLE table_name, field_name data type;
c) ALTER TABLE field_name data type;
2. Suppoe you are asked to display all the names which have 'a' as their second
character then which query pattern you will use?
a) _ _ a%;
b) _a_;
c) _a%;
d) _%a;
3. Storing same data in many places is called
a) Iteration
b) Concurrency
c) Redundancy
d) enumeration
4. In order to add a new column to an existing table in SQL, we can use the
command
a) Modify table
b) Edit table
c) Alter table
d) Alter columns
5. In an RDBMS relationship between tables are created by using
a) Alternate keys
b) Foreign keys
c) Candidate keys
d) Composite keys
6. Sarthak, a student of class XII, created a table”class”. Grade is one of the
columns of this table. To find the details of student whose Grades have not
He needs to display names of students who have not been assigned any stream
or have been assigned stream_name that ends with “computers”.
He wrote the following command, which did not give the desired result.
Help Mr. Mittal to run the query by removing the error and write correct query.
Single Row Function /scalar function Multiple Row Function /group function
3)DATE FUNCTIONS
NOW() YEAR()
DATE() DAY()
MONTH() DAYNAME()
MONTHNAME() curdate()
Differentiate between single row function and multiple row function
Multiple Row Function:
Single Row function:
Multiple row functions are work on multiple
Single row functions are work on single value values and return one output
and return one output.
Sum(),max(),min() etc…
Eg: mod(), left(), right()etc…
1)Math/Numeric Functions:
1)power() or pow() :it returns the value raised to the specified power.
3)MOD():MOD( ) is used to return the remainder of the division operation between two numbers.
Q:write mysql command to find the reminder of 49 by 8.
Answer:
i) Select round(x,-2);
ii) Select pow(4,5);
Write mysql command for the following
1)To display the remainder of 100 by 4 Ans:Select mod(100,4);
2)round it off 235.25 after one decimal place. Ans: Select round(235.25,1);
3)round off 565.before two decimal place Ans:Select round(565.23,-2);
4)Find the power of the value 15 raised to 3 Ans:Select power(15,3);
5)Consider the variable x with values 5649.78
i)round off two decimal place Ans:select round(x,2);
ii)round off 3 decimal place Ans: Select round(x,3);
Find the output:
1)select powe(2,2*2); output 16
2)select round(2872,79,-1); output 2870
3)select mod(5,2)+round(28.5); output 32
4)Select power(5,2)+mod(25,3)+round(27.8); output 54
5)Select round(926.25,-3),round(85.26,1) +10; output 1000, 95.3
6)Select mod(88,9)+20 output 27
7)Select mod(3,4); output 3
8)Select mod(3,0); output NULL
9)Select mod(0,3); output 0
Output: LENGTH(‘INFORMATICS’)
11
Q:Write mysql command to display number of characters in the string “INFO RMATICS” as display the
output with heading “ANSWER IS”
Ans: SELECT LENGTH(‘INFO RMATICS’) “ANSWER IS”;
Output:
ANWER IS
12
Q2: write mysql command to display the given strings STR with values “HARDWARE” in
lowercase
Ans:SELECT LOWER(STR); Output: hardware
4) UPPER( )/ UCASE( ): Converts a string in to uppercase.
Q:write mysql command to display the given string “computer” in lowercase with heading ‘uppercase.’
Ans:SELECT UCASE(‘computer’) “uppercase”;
Output:
uppercase
COMPUTER
5) RIGHT( ) : Returns the given number of characters by extracting them from the right side of the given string.
Q: write mysql command to display last three characters from “INFORMATICS PRACTICES”
6)LEFT( ) : Returns the given number of characters by extracting them from the left side of the given string.
Q: write mysql command to display FIRST three characters from “INFORMATICS PRACTICES”
Ans : SELECT LEFT(‘INFORMATICS PRACTICES’, 3); Output: INF
7) MID( )/SUBSTR( )/substring( ) : Returns a substring starting from the specified position in a given string.
Q:Write mysql command to display four characters from third position.
Ans:SELECT MID(‘INFORMATICS PRACTICES’,3,4);
Output: FORM
2)display four characters starting from third character of the string “information” Ans:select mid(“information”,3,4);
3)Display last five characters from the string “Green City” Ans:select right(“Green City”,5);
4)How many characters are there in the string “CANDIDE” ans:select length(“CANDIDE”);
6)To remove leading space from the string “my country” Ans: select ltrim(“ my country”);
a) Write the command to display “Know” Ans:select mid (“As You Know More”,8,4);
b)convert the string into uppercase. Ans:select ucase(“As You Know More”);
9)
Ans: i)YOU GROW MORE II)Grow
String function with table:
Name Class Address
Arun xi Doha
Rohan xi Wakra
2)Display position of occurrence of “an” in students name Ans:select instr(Name,”an”) from student;
3)Display four characters from students name starting from second character ans:select mid(Name,2,4) from student;
4)Remove leading and trailing space from address Ans: select trim(Address) from student;
Mid(Name,3)
un
han
Ucase(Address)
DOHA
WAKRA
Date functions:
1)Now():it returns the current date and time with the format “yyyy-mm-dd hh-mm-ss”
Now( ) function will not take any parameters inside the parenthesis.
3)date():it extracts the date from date time expression or date expression
Q:write mysql command to extract date from 2020-04-8 01-15-40
Ans: select date(“2020-04-08 01:15:40”);
output: 2020-04-08
Select date(now( ));
Output: 2023-09-10
4)month():it returns the month from the date expression in the range 1 to 12
Q:write mysql command to display the month from 2018-04-08
Ans:select month(“2018-04-08”) ;
output:04
5)year():it returns the year from the date expression
Q:write mysql command to display the year from 2020-04-08
Ans:Select year(“2020-04-08”) ;
output:2020
6)Day( ) or DAYOFMONTH(): DAY() returns the day of the month for a specified date. The day
returned will be within the range of 1 to 31.. The DAYOFMONTH() is the synonym of DAY().
Q:write mysql command to display the day
Ans:SELECT DAY('2008-05-15');
output:15
7)Monthname( ): it returns the full name of the month for a given date.
Q: write mysql command to display the month name from '2009-05-18'
Ans: SELECT MONTHNAME('2009-05-18'); output:may
8)dayname():it returns the name of the weekday.it help us to know which day you born.
Ans:select DAYNAME(“2009-05-13”);
DAYNAME('2009-05-13')
WEDNESDAY
Write mysql command for the following.(From CBSE sample Question paper)
1) Display the name of the day of the current date.
Ans:select dayname(now( ));
Consider the table Employee and write mysql command for the following.
1)Display name of the day of all the employees Ans:select dayname(DOB) from Employee;
2)Display year of date of birth of all the employees Ans:select year(DOB) from Employee;
Write mysql command for the following questions based on Employee table
Employee
EmployeeId EmployeeName Salary Age
E1 Arun 5000 34
E2 Rahul NULL 33
E3 Akash 2000 37
E4 Mukesh 5000 39
Q1: Display highest salary from the table.
A: Select max(Salary) from Employee; output:5000
Output:90 XI Mohammed 40
Q:Display total marks of all the students who have scored more than 40 marks.
A: Select sum(mark) from students where mark>40;
Output:120
Competency based Questions(cbse sample paper)
Answer:
1) 10+mod(5,3)
=10+2
=12
2)round(10.50,2)*5 =52.5
Round(25.10,2)*3 =75.3
3)ucase (right(pname,2)
ER
EN
EN
ER
Find the output
a)SELECT MID(“Sunshine Public School” , 3 , LENGTH(“Java”));
SELECT MID(“Sunshine Public School” , 3 , 4);
Ans:nshi
b) SELECT MOD(14 * 9 , 90 / 3);
select mod(126,30.0)
Ans:6.0
c)SELECT YEAR(NOW( )) + DAY(NOW( )) + MONTH(NOW( )) “new value”;
2023+15+9
Ans:
New value
2047
d)SELECT SUBSTR(“GOOD MORNING INDIA” , MOD(11 , 6) , 2);
SELECT SUBSTR(“GOOD MORNING INDIA” , 5 , 2);
Ans: M
Aggregate functions(continue)
Q:in a table EMP has a column “Salary” contains the data set (6000,5000,6000,10000,NULL),
what will be the output after the execution of the given queries?
1)SELECT min(salary) From Emp; output: 5000
2)SELECT max(salary) FROM Emp; output:10000
3)SELECT sum(salary) FROM Emp; output: 27000
4)SELECT count(salary) FROM Emp; output:4
5)SELECT distinct salary FROM Emp; output:6000
5000
10000
2)display total marks of all class xii students those who are staying in delhi.
A: select sum(mark) from student where class=”xii” and city=”delhi”; Sum(mark)
NULL
3)display lowest and highest marks of all students.
A: select min(mark),max(mark) from student; min(mark) Max(mark
30 80
4)Display minimum marks of students whose number of characters are in their name more than 5
A: select min(mark) from student where length(name)>5; min(mark)
NULL
5)display number of students those who are staying in Jaipur. 80
A: select count(*) from student where city=”Jaipur”;
Count(*)
2
8)display lowest marks of the students those who born in the year
min(mark)
2008
50
A: select min(mark) from student where year(dob)=2008;
9)Display name,class,fees of the students those who born in the month of may.
A: select name, class, fees from student where monthname(dob)=”may”;
name class fees
Arun xi 365.50
anjali xi 285.78
10)Display marks and name of the students in lowercase those who are staying in Jaipur.
A: select mark, lcase(name) from student where city=”Jaipur”;
11)Display last three characters from all students name whose marks more than 50.
A: select right(name,3) from student where mark>50;
13)Display average marks and fees rounded as one decimal place of all class xi students.
Ans: select avg(mark),round(fees,1) from student where class=”xi”;
Q: Find the output for the following questions based on the given table
Name class Gender mark Fees DOB City
Q2) display minimum marks of the students with respect to the class
Ans:select class,min(mark) from student group by class;
2)Display total number of students in city wise whose fees less than 500
Ans: select city,count(*) from student where fees<500 group by city;
2)Display total number of students in city wise whose total fees less than 500
Ans: select city,count(*) from student group by city having sum(fee)<500;
3)display minimum marks of the students class wise number of students more than 2.
Ans:select class,min(mark) from student group by class having count(*)>2;
Example:
Display average marks of the students in class wise whose minimum mark less than 40
Select class,avg(mark) from student group by class having min(mark)<40;
Predict the output for the following: Answers already discussed
in the class.(given in the notebook)
a)select max(QTYsold),min(qtysold) from salesman;
b)Select count(area) from salesman;
c)Select length(sname) from salesman where month(dateofjoin)=10;
d)Select sname from salesman where right(scode,1)=5;
Write mysql command for the following:
1)count the number of salesman
2)display the maximum qtysold from each area.
3)Display the average qtysold from each area where number of salesman more than 1
4)Display all records in descending order of area.
2)Table:Garment
Gcode Gname category colour price
111 Tshirt XL Red 1500.00
112 jeans L Blue 1500.00
113 skirt M Black 1000.00
114 Ladies jacket XL Blue 5000.00
115 Kurtha L Red 2000.00
116 Ladies top L Pink 1000.00
write mysql command for the following.
a)Display category wise total price of all garments .
b)display highest and least price from the table.
c)display color wise minimum price of all the graments.
d)display gcode and total price of all garments in category wise whose total price more than 2000.
e)Display number of garments and garments name in color wise total number of garment less than 2.
f)display minimum price from the table with respect to the category.
Gcode Gname category colour price
111 Tshirt XL Red 1500.00
112 jeans L Blue 1500.00
113 skirt M Black 1000.00
114 Ladies jacket XL Blue 5000.00
115 Kurtha L Red 2000.00
116 Ladies top L Pink 1000.00
3)Mahesh, a database administrator needs to display house wise total number of records of
‘Green’ and ‘Orange’ house. She is getting an error .
1)SELECT HOUSE, COUNT (*) FROM STUDENT GROUP BY HOUSE WHERE HOUSE=’Green’ OR house=‘Orange’ ;
2)select house,sum(marks) from student group by house where count(*)>2;
5)
a) select sum(MARKS) from student where OPTIONAL= ‘IP’ and STREAM= ‘Commerce’;
b) select max(MARKS)+min(MARKS) from student where OPTIONAL= ‘CS’;
c) select avg(MARKS) from student where OPTIONAL= ‘IP’;
d) select length(SNAME) from student where MARKS is NULL;
Answer the Following
WHERE Clause is used to filter the records HAVING Clause is used to filter record
from the table based on the specified from the groups based on the specified
condition. condition.
WHERE Clause is used before GROUP HAVING Clause is used after GROUP
BY Clause BY Clause
Eg:select City from Student where
Eg:select city from Student group by city
salary>5000 group by City;
having min(salary)>5000;
EQUI join: SQL EQUI JOIN performs a JOIN equality or matching column(s)
values of the associated tables. An equal sign (=) is used as comparison operator
The syntax of equi join is given below:
SELECT column_name (s) FROM tablename1, tablename2,
WHERE tablename1. Foreign key= tablename2.foreign key;
Table:Sam Table:Vendor
Q1:What is the primary key and foreign key of the above table
Primary key of SAMS table-Icode Primary key of VENDORS table-Vcode
Foreign key for the above table-Vcode
Q4) Display Icode,color ,Vname and vcode from the above tables.
Ans: SELECT icode,colour,vname FROM SAM S, VENDOR V
WHERE S.Vcode = V.Vcode
Q5:Display Icode,Iname and Vname of all vendors who manufacture “Refrigerator”
Q6.Display iname,icode,vname and price of all the product whose price more than
20000
Q7)Display vendor name and names of all items manufactured by vendors whose
vcode is P03
9)Display icode, price and vcode of vendors from the above table whose iname
contains “h”
Answer:
i.SELECT YEAR(MIN(TRANSACTION_DATE)) FROM BLOCKCHAIN;
ii. SELECT MONTH(MAX(TRANSACTION_DATE)) FROM BLOCKCHAIN;
iii. SELECT * FROM BLOCKCHAIN WHERE MONTHNAME (TRANSACTION_DATE)='MAY';
iv. SELECT COUNT(*) FROM BLOCKCHAIN WHERE YEAR(TRANSACTION_DATE)=2022;
Competency based questions:(CBSE sample question paper)
Q2:
Answer:
i.SELCT FUEL, AVG(QT1) FROM CAR_SALES GROUP BY FUEL;
ii. SELECT SEGMENT, MAX(QT2) FROM CAR_SALES GROUP BY SEGMENT;
iii. SELECT * FROM CAR_SALES ORDER BY QT2 DESC;
Find the output:
Answers:
11. In
a Database Company, there are two tables given below
Table : SALES Table : LOCATION
SALESIMANID NAME SALES LOCATIONID LOCATIONID LOCATIONNAME
ANITA SINGH ARORA 250000 102 101 Delhi
S1
Y.P. SINGH 1300000 101 102 Mumbai
S2
$3
TINA JAISWAL 1400000 103 103 Kolkata
S4
GURDEEP SINGH 1250000 102 104 Chennai
Solution.
() SELECT SALESMANID, NAME, LOCATIONID, LOCATIONNAME
FROM SALES S, LOCATION L
WHERE S.LOCATIONID = L.LOCATIONID ;
(1) SELECT NAME, SALES, LOCATIONNAME
FROM SALES S, LOCATION L
WHERE S. LOCATIONID = L.LOCATIONID AND SALES > 130000
(iii) SELECT NAME
FROM SALES
WHERE NAME LIKE '%SINGH%
(10) Primary Key : SALESMANID
Keason. It uuniquely identifies all ROWS in the table and does not contain empty/zero or
null values.
() UPDATE SALES
SET LOCATIONID = 104
WHERESALESMANID ='S3';
Sep 23 2023,09: l0
12. In a Database Multiplexes, there are two tables with the following data. Write MysQL queries for (i)
and (i),
ohich are based on TicketDetails and AgentDetails : [CBSE
Table : AgentDetails
D20141
Table : TicketDetails
A code ACode
Tcode NAME Tickets
A01
AName
SO01 Meena 7 A01 Mr. Robin
A02
S002 Vani 5 A02 Mr. Ayush
A03
SO03 Meena 9 A01 Mr. Trilok
A04
S004 Karish 2 A03 Mr. John
1 A02
SO05 Suraj
records where the
() To display Tcode, Name and Aname of all the number of tickets sold is
more thar 5.
(ii) To display total number of tickets booked by agent "Mr. Ayush".
(i) To isplay Acode, Aname and coresponding Tcode where Aname ends with "k"
Solution.
(i) Select Tcode, Name, AName
From TicketDetails TD, AgentDetails AD
Where TD.A Code = AD. Acode
AND Tickets > 5;
(ii) Select Count (*)
From TicketDetails TD, AgentDetails AD
Where TD.A Code = AD.Acode
AND AName = "Mr. Ayush";
(iii) Select Acode, AName, Tcode
From TicketDetails TD, AgentDetails AD
Where TD.A Code = AD.Acode
AND AName Like '%K' ;
13. In a Database - SAMS and VENDOR are two tables with the
(i) to (ii), based on tables SAMS and following information. Write MySQL querets jur
VENDOR: [CBSE OD 2014)
1) In a table named ‘Student’, if a column “Name” contains the data set (“Rashi”,
“Shreyas”, “Nitya”, “Rashi”,“Nitya”, “Nityam”, “Kavya”, “Rashi”), what will be
the output after the execution of the given query?
SELECT COUNT(DISTINCT Name) FROM student;
(a) Error – cannot work on char data type (b) 5
(c) “Five” (d) No output
2) If column “Quantity” contains the data set (5,4,7,5,8,5,4), what will be the output
after the execution of the given query?SELECT SUM(DISTINCT Quantity) FROM
sales;
a) 38
b) 24
c) 4
d) None
3) Aggregate functions can be used in the select list or the clause of a select
statement. They cannot be used in a clause.
a) Where, having
b) Having, where
c) Group by, having
d) Group by, where
4) If column “Salary” contains the data set {10000, 15000, 25000, 10000, 15000},
what will be the output after the execution of the given query?
SELECT SUM(DISTINCT SALARY) FROM EMPLOYEE;
a)75000
b) 25000
c) 10000
d) 50000
5) The correct output of MySQL> Select trim(leading ‘&’from ‘&&&India&&&’);
(a) India&& (b) India&&&
(c) &&India (d) &&&India
6) Consider the decimal number n with value 278.6975. Write commands in SQL :
i. That gives output 279
ii. That gives output 280
8) Write outputs for SQL queries (i) to (iii) which are based on the given table
BANK:
ACCNO ANAME CITY BALANCE LASTUPDATEDON
101011 Ramesh Jammu 1000.67 2022-11-06
101316 Suresh Srinagar 9025.76 2022-11-01
101512 Mehak Delhi 8053.43 2022-11-06
101011 Kashish Jammu 7061.55 2022-10-10
(i) SELECT SUBSTR(ACCNO,3,2), CITY FROM BANK WHERE BALANCE>5000;
(ii) SELECT ROUND(BALANCE,1),ANAME FROM BANK WHERE
LENGTH(ANAME)>5;
(iii) SELECT YEAR(LASTUPDATEDON), ACCNO FROM BANK WHERE
BALANCE>1000;
Note: DOJ refers to date of joining and DOB refers to date of birth of workers