Professional Documents
Culture Documents
Revision Unit Test 1
Revision Unit Test 1
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.
Functions in SQL :
Functions are used to perform some particular task and it returns zero or
more values as a result. SQL functions
are categorized as Single row functions and Aggregate functions.
1) Single Row Functions :
Those functions which are applied on a single value and return a single
value are called Single Row Functions.
There are three categories of single row functions in SQL
1. Numeric Function
2. String Function
3. Date Function
1) Numeric Functions : Three commonly used numeric functions are
POWER( ), ROUND( ) and MOD( ). These functions are also called Math
Functions
Function Name Description Example with output
mysql> SELECT POWER(2,3);
Output:
8
POWER(X,Y) or Calculates X to the
POW(X,Y) power Y.
mysql> SELECT POWER(3,3);
Output:
27
ROUND(N,D) Rounds off number N mysql>SELECT
to D ROUND(2912.564, 2);
number of decimal Output:
2912.56
Ans.
1. 1245.2
2. 7564.25
3. 8925.9
4. 370
5. 570
6. 500
7. 27
8. 16
9. 2
10.3
String Functions : String functions can perform various operations on
alphanumeric data. String functions and their usage are:
Function Name Description Example with output
SELECT
UCASE(“Informatics”);
Output:
UCASE(string)
Converts string into INFORMATICS
OR
uppercase
UPPER(string)
SELECT UCASE(“MySQL”);
Output:
MYSQL
SELECT
LCASE(“Informatics”);
Output:
LOWER(string)
Converts string into informatics
OR
lowercase
LCASE(string)
SELECT LCASE(“MySQL”);
Output:
mysql
Returns a substring
SELECT MID(“Informatics”, 2,
of size n
4);
MID(string, pos, n) starting from the
OR specified position
Output:
SUBSTRING(string, (pos) of the string.
nfor
pos, n)
OR If n is not specified,
SELECT MID(“Practices”, 2);
SUBSTR(string, pos, it returns the
n) substring from the
Output:
position pos
ractices
till end of the string.
Select length(“Sumit”)
Return the length of
LENGTH(string)
the string. Output :
5
Returns N number SELECT LEFT(“Science”, 4);
of characters
LEFT(string, N)
from the left side of Output:
the string. Scie
SELECT
Returns N number
RIGHT(“csiplearninghub”, 3);
of characters
RIGHT(string, N)
from the right side
Output:
of the string.
hub
Returns the position
SELECT INSTR(“Informatics”,
of the first
“m”);
occurrence of the
Output:
substring in
6
INSTR(string, the given string.
substring
SELECT INSTR(“Informatics”,
Returns 0, if the
“am”);
substring is
Output:
not present in the
0
string.
SELECT LENGTH(LTRIM(“
DELHI”));
Output
Returns the given
5
string after
LTRIM(string)
removing leading
SELECT LENGTH(LTRIM(“
spaces.
DELHI ”));
Output
6
SELECT
LENGTH(RTRIM(“DELHI ”));
Output
Returns the given
5
string after
RTRIM(string)
removing trailing
SELECT LENGTH(RTRIM(“
spaces.
DELHI ”));
Output
6
Returns the given SELECT LENGTH(RTRIM(“
string after DELHI ”));
TRIM(string) removing both
leading and trailing Output
spaces. 5
Practice Questions :
Ans.
1. 5
2. 12-a
3. tabase
4. 10
5. Dat
6. SQL
7. database query using sql
8. 7
9. 7
10. 3
Date and Time Functions : These functions perform operations on date and time
data. Various Date and Time functions are :
Function Name Description Example with output
NOW() It returns the SELECT NOW();
current
system date and Output:
time. 2021-06-02 13:42:37
It returns the date
SELECT DATE(NOW());
part
DATE() from the given
Output:
date/
2021-06-02
time expression.
It returns the
SELECT MONTH(NOW());
month in
MONTH(date) numeric form
Output:
from the
6
date.
It returns the SELECT MONTHNAME(“”2021-
month 06-2”)
MONTHNAME(date
name from the
)
specified Output:
date. June
SELECT YEAR(“2021-06-2”)
It returns the year
YEAR(date) from
Output:
the date.
2021
SELECT DAY(“2021-06-2”)
It returns the day
DAY(date) part
Output:
from the date.
2
SELECT DAYNAME(“2021-06-
It returns the
2”)
name of
DAYNAME(date)
the day from the
Output:
date
Wednesday
Practice Questions :
Q1. Write the output of the following statements :
1. SELECT DAY(“2021-06-21”)
2. SELECT YEAR(“2020-06-2”)
3. SELECT MONTHNAME(“2021-05-2”)
4. SELECT MONTH(“2021-05-2”)
5. SELECT DAYNAME(DATE(NOW()))
Ans.
1. 21
2. 2020
3. May
4. 5
5. Name of Current Day
2) Aggregate Functions :
Aggregate functions are also called multiple row functions. These functions
work on a set of records as a whole, and return a single value for each column
of the records on which the function is applied.
Table : EMP
E_i
E_Salary
d
1 25000
2 20000
3 25000
4 15000
5 30000
GROUP BY in SQL :
GROUP BY clause groups the rows together that contain the same values in a
specified column. HAVING Clause in SQL is used to specify conditions on the
rows with GROUP BY clause.
Consider the following table :
Table : Bank
i
Salary Designation Bankname Cardtype
d
1 25000 Clerk SBI Credit
2 22000 Clerk SIB Debit
Receptionis
3 18000 Axis Credit
t
4 50000 Manager SIB Credit
5 45000 Manager Axis Debit
6 40000 Manager Axis Debit
Receptionis
7 15000 SBI Credit
t
OUTPUT :
sum(Salary
designation
)
47000 Clerk
33000 Receptionist
135000 Manager
Operations on Relations :
We can perform following operations on table :
1. Union
2. Intersection
3. Set Difference
1) Union (U) : This operation is used to combine the rows of two tables at a
time. If some rows are the same in both the tables, then the result of the Union
operation will show those rows only once.
Let us consider two relations Cricket and Hockey
Table : Cricket
Rollno Name Class
1 Amit 6
2 Anil 7
3 Sonam 8
Suma
4 7
n
Table : Hockey
Rollno Name Class
1 Amit 6
5 Sahil 7
3 Sonam 8
7 Ravi 7
1 Amit 6
2 Anil 7
3 Sonam 8
Suma
4 7
n
5 Sahil 7
7 Ravi 7
Cricket (U) Hockey
2) INTERSECT (∩) : Intersect operation is used to get the common tuples from
two tables. The output of INTERSECT operation on Cricket and Hockey table
(given above) is :
Rollno Name Class
1 Amit 6
3 Sonam 8
Cricket (∩) Hockey
3) Minus(-) : This operation is used to get tuples/rows which are in the first
table but not in the second table, and the operation is represented by the
symbol – (minus).
OUTPUT of Cricket (-) Hockey
Rollno Name Class
2 Anil 7
Suma
4 7
n
5 Sahil 7
7 Ravi 7
Cartesian Product :
Cartesian product combines tuples from two relations. It results in all pairs of
rows from the two relations, regardless of whether or not they have the same
values on common attributes.
Let us consider the two tables : Cricket and Hockey
Table : Cricket
Rollno Name Class
1 Amit 6
2 Anil 7
3 Sonam 8
Suma
4 7
n
Table : Hockey
Rollno Name Class
5 Sahil 7
3 Sonam 8
1 Amit 6 5 Sahil 7
2 Anil 7 5 Sahil 7
3 Sonam 8 5 Sahil 7
4 Suman 7 5 Sahil 7
1 Amit 6 3 Sonam 8
2 Anil 7 3 Sonam 8
3 Sonam 8 3 Sonam 8
4 Suman 7 3 Sonam 8
Cricket X Hockey
NOTE : Query to produce the above result is : Select * from Cricket, Hockey;
1 Suman 6
2 Ravi 6
3 Sonam 7
Table : Book
Admno B_id Subject
1 B101 English
2 B102 Math
3 B103 Science
Q1. Write a query to join two tables Student and Book on the basis of field
Admission Number.
Ans. Select * from Student, Book where Student.Admno = Book.Admno;
OR
Select * from Student Join Book on Student.Admno = Book.Admno
OR
Select * from Student Natural Join Book
Q2. Display Admission number, Student Name and Subject from table Student
and Book.
Ans. Select Admno, Name, Subject from Student, Book where Student.Admno =
Book.Admno;
Q3. Display Name and Class of a student who is having book of subject Math.
Ans. Select Name, Class, Subject from Student, Book where Student.Admno =
Book.Admno;
Summary :
A Function is used to perform a particular task and return a value as a result.
Multiple row functions work on a set of records as a whole and return a single value.
Numeric functions perform operations on numeric values and return numeric values.
String functions perform operations on character type values and return either
character or numeric
values.
Date and time functions allow us to deal with date type data values.
GROUP BY function is used to group the rows together that contain similar values in a
specified
column. Some of the group functions are COUNT, MAX, MIN, AVG and SUM.
Join is an operation which is used to combine rows from two or more tables based on
one or
more common fields between them.