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

Mysql Full Notes

Uploaded by

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

Mysql Full Notes

Uploaded by

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

Database concept and SQL

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

Data types in sql:: int, char ,varchar, float, date, time

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)

Tuple/record-Each row in a table


Attribute-Each column in a table

Degree-number of columns in a table

Cardinality-Number of rows in a table.


SQL commands(DDL,DML,DQL)
DDL commands:it stands for Data Definition language ,it used to define the structure of a data
Create ,alter,drop
DML commands: it stands for Data manipulation language.it is used manipulate the data.
Insert into,update,delete
DQL commands: it stands for Data Query language.it is used to fetch the data
Select

Sql commands for Query


1)desc-it used to display the structure.
2)Show databases-used display all existing databases.
3)create :it is used to create database and table.
4)use-open the databse
5)insert into-it is used to add new record into the table
6)select-it is uded to display is used to select data from a database.

7)distinct:it is used to return only distinct (different or unique) values.

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.

13)in- it used to display in the list of values.

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

15)Null: a NULL value is a field with no value. it is not zero


16)DELETE: Delete all records from a database table
17)drop:it is used to drop an existing table in a database including the structure.

Keys in database:
1)Primary Key – A primary is a column in a table that uniquely identifies tuples
(rows) in that table

2)Candidate Key – It is an attribute or a set of attributes or keys participating for


Primary Key, to uniquely identify each record in that table
Table: Books Answer:
1)Create database library;
2)use library;
3)show databases;
4)create table Books(Bookid varchar(20),Book_Name
varchar(20),Authorname varchar(20),Publishers varchar(20),price int
,Type varchar(20),Qty int);
5)desc Books
1.Create a database Name with “Library”.
Or
2.Open the database Library Describe Books;
3.To list all databases. 6)show tables;
4.Create the above table “Books” with appropriate datatypes. 7)insert into Books values(“F004”,”ip””,”sumitha”,”bpb”500,”Text”, 20);
5.To display the structure of the table. 8)insert into Books values(“F006”,”cs””,NULL,”bpb”,NULL,”Text”, 20);
6.To check whether the table is created or not. 9)select authorname FROM BOOKS;
10)SELECT Book_name,price from Books;
7.insert one new record into the table.(Assume your own data)
11)select * from Books;
8.add one new record without Authorname and Price.
12)select distinct qty from books;
9.Display Author Name of all the books. 13)alter table books add location varchar(20);
10.Display Name and Price of all the books. 14)alter table books modify price float;
11.Display all the details of the books. 15)alter table books drop location;
12.display unique quantity of the books. 16)select book_name,authorname from books where price is null;
13.Add a new column Location for the above table. 17)select authorname,Type from books order by Bookid desc;
14.change the datatype of Price into decimal. 18)Select Bookid, price from Books where publishers =”ËPB”;
15.delete the column Location from the above table. 19)select authorname,Type from Books where price>400 and price<600;
16.Display Book name and author name of books that contains no price. 20)delete from books Where authorname=”Änna”;
17.Display author name and Type of books in descending order of Bookid 21)update books set bookname=”Ïp”,Authorname=”sumitha” where
18.Display Bookid and Price of EPB publishers. Bookid=”T002”;
19.Display Author name and Type of books those price above 400 and 22)select Authorname from books where price between 400 and 700;
below 600. 23)select Book_name from books where price in(650,300,700);
20.Delete all the details of books of Anna . Or
21.Modify the Book name as “IP” Author name as “Sumitha” of select Book_name from books where price=650 or price=300 or price
Bookid T002. =700;
24)select authorname, Type from Books where Book_name like “_ y%”;
22.Display Author Name of the Books that contains Price in the
25) select Book_name ,Price from books where where Authorname like
range 400 to 700.
“ %a”;
23.Display name of the Books that price contains 650 or 300 or 26)alter table books add primary key(Bookid);
700. 24.Display Author name and Type of Books that Book Name 27)alter table books drop primary key;
contains Second character “ y”. 28)delete from books;
25. Display Book name and Price of Books that Author Name 29)drop table books;
ending with “a”. 30)drop database books;
Find the output:
26.Add Bookid as Primary key in to the table.
1)
27.remove Primary key from the table.
28.Remove all the values from the table.
29.Delete the above table from the database. 2)
30.Delete database permanently.
Find the output:
a)select distinct Type from Books;
b)Select Bookid,Price from Books where Book_Name like “T%”; 3)
C)select * from Books where Bookid=”T002”;
CBSE SAMPLE QUESTION PAPER 2023

ANS: 1)create table FOOD;


2)create table Nutrients(Food_item varchar(20) primary key,Calorie integer);
Q2)

i. Insert a new record in the table having following values: [6,'Khushi','CS',85]


ii. To change the value “IP” to “Informatics Practices” in subject column.
iii. To remove the records of those students whose marks are less than 30 .
IV. To add a new column Grade of suitable datatype.
V. To display records of “Informatics Practices” subject.
VI. which column can be suitable for primary key? justify your answer?
vii. display all the details of the students whose marks not in the range 90 to 98.
viii. Display name and subject of the students whose name not starts with V
ix. if two more column are added what is the degree and cardinality of the above table?

Ans:i)insert into exam values(6,'Khushi','CS',85);


ii)update exam set subject=informatics practices” where subject=”ïp”;
iii)delete from student where marks<30;
iv)alter table exam add Grade varchar(20);
v)select * from exam where subject=”informatics Practices”);
vi)Regno column we can set as a primary key, because primary key is a column in a table
that uniquely identifies each row in a table.
vii)select * from exam where marks not between 90 and 98;
viii)select name, subject from exam where name not like “v%”;
ix)Degree-number of columns =6 Cardinality=number of rows=5
M.E.S INDIAN SCHOOL, DOHA - QATAR
Worksheet 8 Notes 2023- 2024

Section: Boys’ &Girls‘ Date : 9-9-23


Class & Div. : XII (All Divisions) Subject:IP
Lesson / Topic: Database Concept and MySQL
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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

F 061, Rev 01, dtd 10th March 2020


been entered, he wrote the following Mysql query, which did not give the
desired result:
Select * from class Where Grade=”NULL”;
Help sarthak to run the query by removing the errors from the query and write
the correct query.
7. A relation STUDENT is given below:

Write SQL Commands to:


a) Create the above table student with Rollno as a primary key.
b) Display the name and marks of all students.
c) Display all the details of the students who have secured marks > 90.
d) Display the names of the student in descending order.
e) Update all student marks by 10.
f) add one more column age to the above table student.
g) List the details of all students whose marks is between 70 and 95.
h) Increase the marks of all student by 20%
i) List the name of all students who are from class 11.
j) Add one more row to the above table assume your own data
k) To display the names of all the students whose name starts with letter
“M”.
8. Mr. mittal is using a table with following columns:

Name, Class, Stream_Id, Stream_name

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.

Select Name,class from students where stream_name=NULL OR


stream_name=”%computers”;

Help Mr. Mittal to run the query by removing the error and write correct query.

F 061, Rev 01, dtd 10th March 2020


Unit-MySQL Functions
A Functions is a predefined command set .
it performs some operation and returns a single value
Mysql used functions for manipulate data.

Mysql Functions are divided in to two types.

Single Row Function /scalar function Multiple Row Function /group function

1)Numeric Function /Math function /Aggregate function


POW() power()
MOD()
ROUND() Sum()
Max()
2) character functions/string Functions /Text function
Min()
LENGTH()
INSTR() Count() count (*)
UCASE() / UPPER() Avg()
LCASE() / LOWER()
MID() / SUBSTRING() / SUBSTR()
LEFT()
RIGHT()
LTRIM()
RTRIM()
TRIM()
concat()

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.

Eg: Q:Write mysql command to display the value of 2 to the power of 3


Ans: select pow(2,3); output: 8 2 * 2*2 =8

Q:Writemysql command to display the value of -2 to the power of 3


Ans: Select pow(-2,3) ; output:-8 -2* -2* -2=-8

2.Round( ): rounds a number to a specified number of decimal places.


QWrite my sql command to round the value 7.3
Ans:Select round(7.3); output:7

Q: Find the output:

Select round(7.6); output:8

Select round(7.5); output:8

Round( ) function with index number


Eg:856.265
Find the output:
Before decimal point index number -1,-2,-3 etc)

Select round(856.265,-1); output:860 nearest to 10(0 – 4, 5-10)


Select round(856.265,-2); output:900 nearest to 100( 0- 49, 50-100)
Select round(856.265,-3); output:1000 nearest to 1000(0- 499 ,500-1000)

After decimal point index number 1,2,3 etc)


Select round(856.265,0); output:856
Select round(856.265,1); output:856.3
Select round(856.265,2) ; output:856.27
Select round(856.265,3); output:856.265
Select round(856.265,4); output:856.2650
Select round(856.23,1) ; output:856.2
Next number is 5 or above 5 add 1 to the rounded number

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.

Select mod(49 ,8); output:1 8 49


48

From CBSE SAMPLE QUESTION PAPER 1

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

Math Functions with table


Consider the table product pname Price Discount Quantity Location
monitor 345.50 30 2 doha
cpu 875.75 20 3 wakra
keyboard 75.25 10 2 doha
Write mysql queries for the following
1)Display price of all the products round it off zero decimal paces.
Ans:select round(price,0) from product;
2)Display price of all the products round it off one place before the decimal of the location doha
Ans:select round(price,-1) from product where location=”Doha”;
Find the output:
1)select power(quantity,2)from product;
Output:

2) Select mod(quantity,4) from product where pname=”cpu”;


Output:3
Character/String /Text Functions
1)LENGTH( ) : Returns the length of a string in bytes/no.of characters in string.

Q:Write mysql command to display number of characters in the string “INFORMATICS”


Ans:SELECT LENGTH(‘INFORMATICS’);

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

2) INSTR( ): Returns the index of the position of


first occurrence of substring in the given
string. Q:Write mysql command to display the position of subsring “mat” in the String
“informatics”

Ans: SELECT INSTR(‘Informatics’,’mat’);


1 2 3 4 5 6 7 8 9 10 11
I nf o r m a t i c s

output bcz(since ‘m’ of ‘mat’ is at 6th place)


INSTR(‘Informatics’,’mat’)
6
Find the output:
1)select instr(“computer”,”on”); output:0

3) LOWER( )/ LCASE( ): Converts a string in to lower-case


Q1:write mysql command to display the given strings “INFORMATICS” in lowercase
Ans:SELECT LOWER(‘INFORMATICS”); Output: informatics

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”

Ans: : SELECT RIGHT(‘INFORMATICS PRACTICES’, 3);


Output:CES

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

Find the output:


1) SELECT MID(‘INFORMATICS PRACTICES’,3); output: FORMATICS PRACTICES’
2) SELECT MID(‘INFORMATICS PRACTICES’,-6,3); output:CTI

8)LTRIM( ) : Removes leading spaces(leftside space from the string).


Example :SELECT LTRIM(” INFORMATICS”);
Output: INFORMATICS
9)RTRIM( ): Removes trailing spaces(rightside space from the string).
Example : SELECT RTRIM(“INFORMATICS “);
Output: INFORMATICS
10)TRIM( ) : Removes leading and trailing (leftside and rightside)spaces.
Example: SELECT TRIM(“ INFORMATICS “);
Output: INFORMATICS
11)CONCAT function:returns concatenated string.it adds two or more strings.
Eg: select concat(“my” ,”sql”); output: mysql
Find the output:( CBSE sample paper)
1)select left(“Mysql language”,5) output:Mysql

2)select instr(“computer”,”put”); output:4

3)select mid(“cbse exam”,6,4); output:exam

4)select lower(“CLASS party”); output:class party

5)select length(“cbse board”)+5 output:15

6)select instr(“hardware”,”soft”); output:0

Write mysql command for the following(CBSE sample paper)


1)Display the position of occurrence of the substring “on” in the string “wonderful” Ans: select instr(“wonderful”,”on”);

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

5)To convert your email id xyz@gmail.com in lower case Ans:select lower(“xyz@gmil.com);

6)To remove leading space from the string “my country” Ans: select ltrim(“ my country”);

7)consider the given string “As You Know More”

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

8)consider the string “wonderful” .Write mysql command to display

a)ful Ans: select right(“wonderful”,3); or select mid(“wonderful”,7,3);

b)wonder Ans:select left(“wonderful”,6); or select mid(“wonderful”,1,6);

9)
Ans: i)YOU GROW MORE II)Grow
String function with table:
Name Class Address
Arun xi Doha
Rohan xi Wakra

Write mysql command for the following:


1)Display all students name in uppercase Ans:select ucase(Name) from student;

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;

Find the output:


1)select mid(name,3) from student;

Mid(Name,3)
un
han

2)select ucase(address) from student;

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.

Q:Write mysql command to display the current date and time.


Ans: Select now( ); Output:
now( )
2023-09-10 07:16:19

2)curdate():returns current system date


Q:Write mysql command to display the current date
Ans: select curdate( );
Output: 2023-09-10

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

2) Display name of the month from current date


Ans:select monthname(now( ));

3)Display name of the month from your date of birth.


Ans:select monthname(“2015-03-15”);

Date function with table


Ename DOB
Arjun 2020-03-23
akash 2013-11-12
Rohith 2005-05-18

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;

Find the output:


1)select month(DOB) from Employee; 2)select monthname (Dob) from Employee;
1)Predict the output of the following queries: Answers:
a)select instr('kendriya vidyalaya sangathan','a'); 1) a)8
b)select upper(substr(''motivation”4,5)); b)IVATI
c)select instr('kendriya vidyalaya sangathan',' '); c)9
d)select trim( leading “!” from “!!! Welcome!!!”); d)Welcome!!!
e) select instr(“Welcome”,”ing”); e)0
f)select left('kendriya vidyalaya sangathan',6);
g)Select Length("Data Science")+3;
f)kendri
h)Select Month(‘2020-08-12’)+14; g)15
i)select round(29.21),round(32.76); h)22
j)select ucase(rignt(“pollution”,3)); i)29 33
k)select pow(2,3)+round(234.45,1)+mod(11,2); j)ION
2)Considering the same string “fortuner” Write SQL commands to display: k)243.5
a. the position of the substring ‘for’ in the string “fortuner”
8+234.5+1=243.5
b. the last 4 letters of the string
2)a)select instr(“fortuner”,”for”);
c.write the command to display “fort”
3)Consider the decimal number x with value 9945.8853. Write commands b)select right(“fortuner”,4);
in SQL to: c)select left(“fortuner”,4);
a)Round it off up to 2 decimal places.
b)Round it to 2 places before the decimal. 3) a)select round(x,2);
4)Write the SQL functions which will perform the following operations: b)select round(x,-2);
a) To display the name of the day of the current date.
b)To remove spaces from the beginning of a string, “ Python ”.
c)To display the name of the month eg, January or February from your
4)a)select dayname(now());
date of birth. b)select ltrim(“ python”);
d)Write query to remove leading spaces of string ‘ kendriya’. c)select monthname(“2005-05-21”);
e) Display the position of occurrence of string ‘OL’ in string d)select ltrim(“ kendriya”);
“rollnoinschool”
e)select instr(“rollnoinschool”,”oL”);
5)consider the table:salesman 5)
Write SQL queries using SQL functions to perform the following operations:
a)select sname,round(bonus,0) from salesman;
b)select instr(sname,”ta”) from salesman;
c)select mid(sname,2,4) from salesman;
d)select monthname(dateofjoin) from salesman;
e)select ucase(sanme) from salesman;
a)Display salesman name and bonus after rounding off to zero decimal
places. find the output
b)Display the position of occurrence of the string “ta” in salesman names. a) b)2018
c)Display the four characters from salesman name starting from second
character.
d)Display the month name for the date of join of salesman
e)Display salesman names in capital letters.
Find the output:
1)select round(bonus,-1) from salesman;
2)select select year(dateofjoin) from salesman where salary=50000;
6)Ms.Saumya is working on a MySQL table named ‘Hotel’ having following
structure:
6)a)select right(userid,2) from Hotel;
b)select lcase(name) from hotel;
c)select substring(City,3,3,) from Hotel;
d)select rtrim(name) from Hotel;
e)select trim(City) from Hotel;

She need to perform following task on the table:


a)To fetch last 2 characters from the user_id column.
b)To display the values of name column in lower case.
c)To display 3 characters from 3rd place from the column city.
d)remove trailing space of all students name.
e)remove leading and trailing space of all cities.
Multiple Row Functions (Aggregate Functions )
Aggregate functions also called multiple row functions or group functions.

The different types of aggregate functions are:


(q: Explain any two aggregate functions or group function)
1)max( ):it returns the maximum/highest value in the given column.
2)min( ):it returns the minimum/lowest value in the given column.
3)sum( ): it returns the sum of values in the given column
4) avg( ):it returns the average of the values under the specified column.
5)count ( ):it returns the total number of values in the specified column.it will
not count null values.
Count(*) returns the total number of values including null values .

(Q:differentiate between count() and count(*) function)

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

Q2: Display lowest salary from the table.


A: Select min(Salary) from Employee; output:2000

Q3: Display total salary from the table.


A: Select sum(Salary) from Employee; output:12000
Q4: Display average salary from the table.
A: Select avg(Salary) from Employee; output:12000/3 =4000

Q5:display number of unique salary from the table.


A: select count( distinct salary) from Employee; output:2

Q6:display number of Employeeid from the table.


A:select count(EmployeeId) from Employee; output: 4

Q6:display number of salary from the table.


A:select count(Salary) from Employee; output: 3
Q7:display number of employees from the table.
A:select count(*) from Employee; output: 4
Students

Aggragate function using where clause: class Name Mark


Write MySQL command for the following based on Student table:
XII Akash 30
XI Arun 60
Q: Display total marks of students in class XII XII Sunil 60

A: Select sum(mark) from students where class =”XII”; XI Mrudul NULL

Output:90 XI Mohammed 40

Q: Display minimum marks of students in class xi


A: Select min(mark) from students where class=”XI”;
Output: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

5)SELECT count(distinct salary ) FROM Emp; output:3


7)SELECT count(*) FROM Emp; output:5

Aggregate function and single row function with where clause


Table:student
Write mysql command for the following
Name class Gender mark Fees DOB City
Arun Xi M 30 365.50 2005-05-23 Jaipur
vivek Xi M 50 278.85 2008-03-14 kanpur
Rohith Xii M Null 398.27 2009-11-20 Delhi
Meena Xii F 70 857.28 2007-03-05 Jaipur
Anjali xi F 80 285.75 2008-05-09 Delhi
1)Display total marks of all female students.
A: select sum(Mark) from student where Gender=”F”;
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”;
Aggregate function and single row function with where clause Table:student
Name class Gender mark Fees DOB City
Arun Xi M 30 365.50 2005-05-23 Jaipur
vivek Xi M 50 278.85 2008-03-14 kanpur
Rohith Xii M Null 398.27 2009-11-20 Delhi
Meena Xii F 70 857.28 2007-03-05 Jaipur
Anjali xi F 80 285.75 2008-05-09 Delhi
Write mysql command for the following
1)Display total marks of all female students. Sum(mark)
A: select sum(mark) from student where gender=”F”; 150

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

6)display number of students those who don’t have marks.


Count(*)
A: select count(* ) from student where mark is null;
1

7)display average marks of class xi students. Avg(mark)


A: select avg(mark) from student where class=”xi”; 53.3

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;

12)Display name of the month of date of birth of all female students.


A: select monthname(dob) from student where gender=”f”;

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

Arun Xi M 30 365.50 2005-05-23 Jaipur


vivek Xi M 50 278.85 2008-03-14 kanpur
Rohith Xii M Null 398.27 2009-11-20 Delhi
Meena Xii F 70 857.28 2007-03-05 Jaipur
Anjali xi F 80 285.75 2008-05-09 Delhi

Q1)Select mid(name1,3) , Fees from student where class=”Xi”; Mid(name,1,3)


Aru
Viv
Ans:
Anj

Q2)select round(fees,-2) from student where right(city,1)=”i”; Mid(name,1,3)


Aru
Viv
Ans:
Anj

Q3)select min(mark),max(mark) from student where class=”xi”;


Ans:
min(mark) Max(mark
Q4)select length(name) from student where city=”Jaipur”; 30 80
Aggregate function with group by clause
Q:Define Group by clause:
Group by command: it is used to group the rows together that contain the same values in a specific column.
Note: wise /each /with respect to /perticular will mention in the question you can use group by command

Q1:Display city wise total marks of all the students


Ans:select city,sum(mark) from stydent group by city;
Name class Gender mark Fees DOB City

Arun Xi M 30 365.50 2005-05-23 Jaipur


vivek Xi M 50 278.85 2008-03-14 kanpur
Rohith Xii M Null 398.27 2009-11-20 Delhi
Meena Xii F 70 857.28 2007-03-05 Jaipur
Anjali xi F 80 285.75 2008-05-09 Delhi

Q2) display minimum marks of the students with respect to the class
Ans:select class,min(mark) from student group by class;

3)Display average marks secured by each gender.


Ans:select gender,avg(mark) from student group by gender;

Name class Gender mark Fees DOB City

Arun Xi M 30 365.50 2005-05-23 Jaipur


vivek Xi M 50 278.85 2008-03-14 kanpur
Rohith Xii M Null 398.27 2009-11-20 Delhi
Meena Xii F 70 857.28 2007-03-05 Jaipur
Anjali xi F 80 285.75 2008-05-09 Delhi
Aggregate function with where and group by clause
If where and order by command in one query first priority where condition then group by clause.
1)Display name and marks of all the students with respect to their gender whose marks more than 50

Ans:select gender,name,mark from student where mark>50 group by gender;

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;

Aggregate function with group by and having clause


1)Display name and marks of all the students with respect to their gender whose minimum marks more than 50

Ans:select gender,name,mark from student group by gender having min(mark)>50;

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;

Q:Define Having clause with example


The HAVING clause is used with aggregate functions in a specified condition with group by clause instead of
WHERE clause.
The having clause is always used after the group By clause.
Having clause is used in connection with group by clause.

Hint : any condition with aggregate function(min,max,sum,avg,count) use having clause.

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

Find the output:


a)select avg(price) from garment;
b)select min(price),max(price) from garment;
c)select gname,colour from garment where category=”M”;
d)select category,min(price) from garment group by category;
e)select category,count(*) from garment group by category;

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;

4)Sumit writes the following commands with respect to a table STUDENT


Command 1 : SELECT COUNT(*) from STUDENT;
Command 2 : SELECT COUNT(marks) from STUDENT ;
He gets the output as 6 and 5 respectively. Explain the output with justification

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

1)Differentiate between count(*) and count()


Count(*) Count()
COUNT(*) will count all the rows in the COUNT(column name) will count all
table, including NULL values the rows in the specified column
Eg: select count(*) from student; while excluding NULL values.
Eg: select count(Mark) from student;

2)Differentiate between group by and order by


Group by statement is used to group the Order by keyword sort the result-set either in
rows that have the same value. It is often ascending or in descending order.
used with aggregate functions for Eg:select * from emp order by Salary desc;
example:AVG(), MAX(), COUNT(), MIN() etc.
Eg:select city,Salary from Emp group by City;

3)Differentiate between where condition and having condition

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;

4)Differentiate between delete and drop command


Delete: It is a DML command used Drop: it is a DDL command used to
to delete the records from the table. delete complete table along with its
Eg:Delete from student; structure from the database.
Eg:Drop table student;

5)Differentiate between alter and update command


Alter:alter command is used to change Update:it is a DML command used to change
the structure of the table, like adding or modify the data of the table.
new column,delete one column and
changing the datatype. Eg:update student set mark=100;
Eg:alter table student add age int;
MySQL Sql joins:
Primary key:it is a column in a table uniquely identify tuples in a relation
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the
PRIMARY KEY in another table.

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

Icode Iname Price colour Vcode Vcode Vname


S001 Refrigerator 20000 Blue P01
P01 Sagar
S002 Mobile Phone 45000 Black P02
P02 Manoj
S003 LCD 60000 Silver P03
P03 Anu
S004 Washing 12000 Silver P04
P04 Jacob
Machine

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

Q2:Display all the details of SAM and Vendor table.

SELECT * FROM SAM,VENDOR WHERE SAM. Vocde = Vendor.Vcode;


Or
SELECT * FROM SAM S, VENDOR V WHERE S.Vocde = V.Vcode;
Output:

Icode Iname Price colour Vcode Vcode Vname


S001 Refrigerator 20000 Blue P01
P01 Sagar
S002 Mobile Phone 45000 Black P02
P02 Manoj
S003 LCD 60000 Silver P03
S004 Washing 12000 Silver P04 P03 Anu
Machine P04 Jacob
Q3:Display Icode,Iname and Vname from the above tables.

Ans:SELECT icode,iname,vname FROM SAM S, VENDOR V


WHERE S.Vcode = V.Vcode

Output: Icode Iname Vname


S001 Refrigerator Sagar
S002 Mobile Phone Manoj
S003 LCD Anu
S004 Washing Machine Jacob

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”

SELECT icode,iname,vname FROM SAM S, VENDOR V


A:
WHERE S.Vcode = V.Vcode and Iname=”refrigerator”;
Output:
Icode Iname Vname
S001 Refrigerator Sagar

Q6.Display iname,icode,vname and price of all the product whose price more than
20000

SELECT iname,icode,vname,price FROM SAM S, VENDOR V


WHERE S.Vcode = V.Vcode and price >20000;

Iname Icode Vname Price


Mobile Phone S002 Manoj 45000
LCD S003 Anu 60000

Q7)Display vendor name and names of all items manufactured by vendors whose
vcode is P03

SELECT vname,iname FROM SAM S, VENDOR V


WHERE S.Vcode = V.Vcode and v.vcode=”P03”
8)Display price and vname from the above table whose price in the range 10000 and
20000.

Ans: SELECT price,vname FROM SAM S, VENDOR V


WHERE S.Vocde = V.Vcode and price between 10000 and 20000;

9)Display icode, price and vcode of vendors from the above table whose iname
contains “h”

Ans: SELECT icode,price,vcode FROM SAM S, VENDOR V


WHERE S.Vcode = V.Vcode and iname like “%h%”;
10)Display all the details of vendors in descending order of vendors name.
Ans:select * from vendor order by vname desc;
11)Modify all the price of item increased by 100.
Ans: update sam set price=price+100;

Find the output:


Table:Sam Table:Vendor
Icode Iname Price colour Vcode Vcode Vname
S001 Refrigerator 20000 Blue P01
P01 Sagar
S002 Mobile Phone 45000 Black P02 P02 Manoj
S003 LCD 60000 Silver P03 P03 Anu
S004 Washing Machine 12000 Silver P04 P04 Jacob
Find the output:
1)select iname,price,vname from sam,vendor where sam.vcode =vendor.vcode and vname like
“%M%”;
Iname Price vname
LCD 60000 Manoj
2)select * from sam s,vendor v where s .vcode =v.vcode and vcode=”P03”;
Icode Iname Price colour Vcode Vcode Vname
S003 LCD 60000 Silver P03 P03 Anu
3)select icode ,price,vname sam s,vendor v where s .vcode =v.vcode and price>20000;
Icode Price Vname
S004 12000 Jacob
Competency based questions:(CBSE sample question paper)
Q1:

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

S5 SIMI FAIZAL 1450000 103

Write SQL queries for the following :


() Todisplay SalesmanlD, names of salesmen, LocationlD with corresponding location names.
(i) To display namzes of salesmen, sales and corresponding location names who have achieved Sales more
than, 1300000.
() To display names of those salesmen who have 'SINGH in their names.
(to) Identify Primary key in the table SALES. Give reason for your choice.
(o) Write SQL command to change the LocationlD to 104 of the Salesman with ID as S3 in the table
'SALES. [CBSE OD 15]

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)

Table : SAMS Table : VENDOR


ICode IName Price Colour VCode VNAME
VCode
SO01
Refrigerator 20000 Blue P01 PO1 Satish
SO02 Mobile Phone 45000 Black P02 P02 Manoj
SO03 LCD Subodh
60000 Silver PO3 P03
S004
Washing Machine 12500 Smoke P01 P04
Jacob
SO05 Air conditioner 16000 White PO3
"Refrigerator"
() To display ICode, IName and vendors, who manufacture 20000
VName of all the Morethan
price codeis"PO8.
(i) To display IName, ICode,
VName and Price of allthe products whose
vendor whose
(1ii) To display vendor names and by
names of all items manufactured
OPERATIONS
AND SET
JOINS
117
Solution.
() SELECT ICode, IName, VName (ii) SELECT IName, ICode, VName, Price
FROM SAMS S, VENDOR V
FROM SAMS S, VENDOR V
WHERE S.VCode = V.Vcode
WHERE S.VCode -V.Vcode
and IName = "Refrigerator" : and Price > 20000;
IName FROM SAMS S, VENDOR V
(ii) SELECT VName,
WHERE S.Vcode = V.Vcode
and V.VCode = "PO3":
two tables :
. Iua Database there are [CBSE D 12]
Table : ITEM Table : BRAND

Item Code Item Name Price Item Code Brand Name


Refrigerator 90,000 111 LG
111
Television 75,000 222 Sony
222
333 HCL
333 Computer 42,000
444 IFB
444 Washing Machine 27,000

Write MySQL queries for the following :


Name of those Items, whose Price is
() To display Iten_Code, Item_Name and corresponding Brand
between 20000 and 40000 (both values inclusive).
item, which has Item_Namne as "Computer".
(1) To display ltem_Code, Price and Brand_Name of the
Solution.
() SELECT I.Item Code, Item Name, Brand_ Name
FROM Item I, Brand B
WHERE I.Item Code = B.Item Code
AND Price BETWEEN 20000 AND 40000 ;

(") SELECT I.Item Code, Price, BrandName


FROM Item I, Brand B
WHERE I.Item Code = B.Item Code
AND Item Name = "Computer
M.E.S INDIAN SCHOOL, DOHA - QATAR
Worksheet 9 2023- 2024

Section: Boys’ & Girls’ Date : 02-10-2023

Class & Div.:12th (All Divisions) Subject: Informatics Practices


Lesson / Topic: Database concepts and the Structured Query Language
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

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

F 061, Rev 01, dtd10th March 2020


7) Madhu, a database administrator needs to display class wise maximum marks of
all classes above 8th class. She is encountering an error in following query.
SELECT CLASS, MAX(MARKS) FROM STUDENT GROUP BY CLASS
WHERE CLASS>8;
Help her in identifying error and write correct query by suggesting possible
corrections.

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;

9) Consider the table WORKER given below:


WNO NAME DOJ DOB GENDER DCODE
1001 Goerge K 2013-09-02 1991-09-01 MALE D01

1002 RymaSen 2012-12-11 1990-12-15 FEMALE D03

1003 Mohitesh 2013-02-03 1987-09-04 MALE D05

1007 Anil Jha 2014-01-17 1984-10-19 MALE D04

1004 Manila Sahai 2012-12-09 1986-11-14 FEMALE D01

1005 R Sahay 2013-11-18 1987-03-31 MALE D03

1006 Jaya Priya 2014-06-09 1985-06-23 FEMALE D05

Note: DOJ refers to date of joining and DOB refers to date of birth of workers

i) Display the number of workers in each department code having number


of workers more than 1
ii) Display the number of male workers who joined after January 1st 1986
iii) Display the number of female workers working in each department
iv) Add one more column departname to the above table.
OR

F 061, Rev 01, dtd10th March 2020


Distinguish between single row function and aggregate function in MYSQL.
Write one example of each.
10) Write suitable SQL query for the following:
(i) Display 3 characters extracted from the 5th left character from string
“INFORMATICS”.
(ii) Display the position of occurrence of string ‘WORLD’ in string “HELLO
WORLD”.
(iii) Round off the value 23.52 to zero decimal place.
(iv) Display the remainder of 50 divided by 3.
(v) Remove all the expected leading spaces from the column ‘NAME’ of
table ‘STUDENT’.
OR
Explain the following SQL functions using suitable examples.
(a) LEFT()
(b) LTRIM()
(c) INSTR()
(d) YEAR()
(e) MOD()
11) Table: SchoolBus
Rtno Area_covered Capacity Noofstudents Distance Transporter charges
1 Vasantkunj 100 120 10 Shivam Travel 100000
2 Hauzkhas 80 80 10 Anand Travel 95000
3 Pitampura 60 55 30 Anand Travel 60000
4 Rohini 100 90 35 Shivam Travel 75000
5 Yamuna 50 60 30 Anand Travel 55000
vihar
Write the SQL commands for the following:
i) To display five characters from the 2nd character of the transporter.
ii) To show transporter wise average charges for all routes having average charges
more than 60000.
iii) To show Transporter wise total no. of students travelling.
iv) To display total number of students whose charges is more than 55000.
v) To display total distance being covered.
OR
To show transporter wise minimum charges for all routes

F 061, Rev 01, dtd10th March 2020

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy