SQL Worksheet
SQL Worksheet
8
ans. 27 select round(1454.45, -2); Ans. 1500
select pow(3,2); select round(1444.45, -3); Ans. 1000
ans. 9 select round(1544.45, -3); Ans. 2000
select pow(0,2); select mod(5,3) Ans. 2
select mod(5,4); Ans. 1
ans. 0
select mod(4,2); Ans. 0
select pow(2,0); select mod(0,0); Ans. NULL
ans.1 select mod(3,0); Ans. NULL
select pow(0,0); select mod(0,2); Ans. 0
ans. 1 SELECT UCASE('hello'); Ans. HELLO
select round(123.45,1); SELECT Upper('hello'); Ans. HELLO
ans.123.5 select lcase('HELLO'); Ans. hello
select LOWER('HELLO'); Ans. hello
select round(123.45,-1);
MID( ) : To extract a specified number of
ans.120 characters from the string. First parameter is
select round(123.45,0); the text/string. Second parameter is the
ans. 123 starting index and the third parameter is the
select round(153.45,2); number of characters required. (Note: index
ans.153.45 starts with 1 and not 0.)
select round(155.45,0); SELECT MID('ABCDEFGHIJKLMNOP', 1,4);
Ans. ABCD
ans. 155 SELECT MID('ABCDEFGHIJKLMNOP', 1);
select round(245,-2); Ans. ABCDEFGHIJKLMNOP
ans. 200 SELECT MID('ABCDEFGHIJKLMNOP', -2,-1);
select round(255,-2); Ans. nothing will be printed
ans. 300 SELECT MID('ABCDEFGHIJKLMNOP', -2,1);
select round(897, -3); Ans. 0
SELECT MID('ABCDEFGHIJKLMNOP', 5,1);
ans. 1000
Ans. E
select round(5897, -3); SELECT MID('ABCDEFGHIJKLMNOP', 0,4);
ans. 6000 Ans. Nothing will be displayed
select round(5297, -3); SELECT MID('ABCDEFGHIJKLMNOP', 3,4);
ans. 5000 Ans. CDEF
select round(5297,0); SELECT MID('ABCDEFGHIJKLMNOP', -4,2);
ans. 5297 Ans. MN
select round(5297.5,0);
ans. 5298
SELECT SUBSTRING('ABCDEFGHIJKLMNOP', 3,4);
Ans. CDEF
SELECT SUBSTRING('ABCDEFGHIJKLMNOP', 0,4); SELECT CONCAT(TRIM('HELLO '),
Ans. Nothing will display 'WORLD');
SELECT SUBSTRING('ABCDEFGHIJKLMNOP', 1,4);
Ans. ABCD Ans. HELLOWORLD
SELECT SUBSTRING('ABCDEFGHIJKLMNOP', 4,2);
Ans. DE NOW():- Function that returns the
SELECT SUBSTRING('ABCDEFGHIJKLMNOP', -4,2); current date and time.
Ans. MN
SUBSTR( ) : Same as that of MID( ) and
SUBSTRING( )
DATE():- This function returns the date
SELECT SUBSTR('ABCDEFGHIJKLMNOP', -4,3);
part from Date and time.
Ans. MNO
select date(now());
SELECT SUBSTR('ABCDEFGHIJKLMNOP', 1,3);
Ans. ABC
SELECT SUBSTR('ABCDEFGHIJKLMNOP', 4,3);
Ans. DEF MONTH() returns the month part of a
SELECT LENGTH('HELLO WORLD'); date.
Ans. 11 select month(now()); Ans. 11
SELECT LEFT('ABCDEFGHIJKLMNOP',1); select month('2024-03-20'); Ans. 3
Ans. A MONTHNAME() :-gives month name
SELECT LEFT('ABCDEFGHIJKLMNOP',2); from a date.
Ans. AB select monthname('2024-03-20'); Ans.
SELECT LEFT('ABCDEFGHIJKLMNOP',3); March
Ans. ABC YEAR():- Date function returns year part
SELECT LEFT('ABCDEFGHIJKLMNOP',-1); of a date.
NO CHARACTERS DISPLAY select year('2024-03-20'); Ans. 2024
SELECT RIGHT('ABCDEFGHIJKLMNOP',1); DAY():- Day function provide the day
Ans. P part of Date.
SELECT RIGHT('ABCDEFGHIJKLMNOP',2); select day('2024-03-20'); Ans. 20
Ans. OP
SELECT RIGHT('ABCDEFGHIJKLMNOP',-1); DAYNAME():- Returns the weekday
No characters display name of a date.
INSTR( ) : Checks whether the second string/text is
present in the first string. If present it returns the select dayname('2024-03-20'); Ans.
starting index.Otherwise returns 0. Wednesday
SELECT INSTR('ABCDEFGHIJKLMNOP','ABC');
SELECT WEEKDAY("2017-06-15");
Ans. 1
SELECT INSTR('ABCDEFGHIJKLMNOP','BC'); Ans. 3
Ans. 2
SELECT INSTR('ABCDEFGHIJKLMNOP','EFG');
Ans. 5
SELECT INSTR('ABCDEFGHIJKLMNOP','QRST');
Ans. 0
LTRIM( ) :To trim the spaces, if any, from the beginning of
the text.
SELECT LTRIM(' HELLO');
Ans. HELLO
RTRIM( ) : To trim the spaces, if any, from the end of the text
SELECT RTRIM('HELLO ');
SELECT CONCAT(RTRIM('HELLO'), 'WORLD'); Consider the following table named teacher.
Ans. HELLOWORLD
Aggregate functions:- It perform calculation
on multiple values and return a single value.
It is also know as Multi-row functions
MAX(),MIN(),AVG(),SUM(),COUNT()
Aggregate function will not consider
NULL values for calculation. Write SQL statement to display number of
Conside the following table named student. Teacher teaching each subject.
Ans. Select count(*) from teacher group by
subject;
The WHERE clause in MySQL is used with
SELECT, INSERT, UPDATE, and DELETE
queries to filter rows from the table or
relation. It describes a specific condition
when retrieving records from tables.
Display number of employees in each
MAX():- returns maximum value from a dataset. subject whose experience is more than 2.
Ans. 81 Ans. select count(*) from teacher where
MIN():- Returns the minimum value from a dataset. experience>2 group by subject;
Ans. 61 WHERE clause is coming before group by.
AVG():- Returns the average value from a dataset.
Ans. 71.5000 Having Clause The HAVING clause is often
SUM():- It returns the sum of values in a dataset. used with the GROUP BY clause to filter
Ans. 286 rows based on a specified condition. If we
COUNT(*):-Will return the total number of omit the GROUP BY clause, the HAVING
rows present in the Table. clause behaves like the WHERE clause
Ans. 5 Display the subject and number of teachers
COUNT( Field Name) will count the number in each subject where group count is more
of values(excluding NULL) present in the dataset. than 1. select subject, count(*) from teacher
Select count(mark) from student; group by subject having count(*)>1;
Ans. 4 HAVING clause is coming after group by
Here answer 4 is displyed even though 5 rows are
present in the student table because one NULL Display details of teacher whose tid more
value is present in the column named mark. than 3 select * from teacher having tid>3;
OR select * from teacher where tid>3;
Group By clause:- The GROUP BY statement HAVING clause is behaving like WHERE
groups rows that have the same values into clause in this example.
summary rows. The GROUP BY statement is often
used with aggregate functions ( COUNT() , ORDER BY Order by clause is used to
MAX() , MIN() , SUM() , arrange the rows in Ascending or
AVG() ) to group the result-set by one or Descending order of values in a Column.
more columns. Display the details of teachers ascending of
experience.
select * from teacher order by experience;
OR select * from teacher order by
experience ASC;
Display the teachers details in the Descending
order of experience.
select * from teacher order by experience DESC;
Questions
1. Which of the following would arrange the rows in ascending order in SQL.
a. SORT BY b. ALIGN BY c. GROUP BY d. ORDER BY
2. Prachi has given the following command to obtain the highest marks Select
max(marks) from student where group by class;
but she is not getting the desired result. Help her by writing the correct command.
a. Select max(marks) from student where group by class;
b. Select class, max(marks) from student group by marks;
c. Select class, max(marks) group by class from student;
d. Select class, max(marks) from student group by class;
3. Help Ritesh to write the command to display the name of the youngest student?
a. select name,min(DOB) from student ;
b. select name, max(DOB) from student ;
c. select name,min(DOB) from student group by name ;
d. select name,maximum(DOB) from student;
Ans. a. select Type, avg(Price) from Vehicle group by Type having Qty>20;
b. select Company, count(distinct Type) from Vehicle group by Company;
c. Select Type, sum(Price* Qty) from Vehicle group by Type;
Write commands in SQL for (i) and (ii) and output for (iii)
Worksheet 2
3. SELECT CONCAT(NAME,dept) AS 'NAME DEPT' FROM Employee;
4. SELECT LOWER(NAME) FROM Employee
5. select substr('ABCDEFGH', 3,4);
6. select substr('ABCDEFGH', -3,4);
7. SELECT UCASE('pqrxyz');
8. SELECT RTRIM('abcdefgh ');
9. select trim(' pqr Periodic Test 1 is over pqr ');
10. select instr('O P JINDAL SCHOOL, KHARSIA ROAD RAIGARH, JINDAL STEEL AND
POWER LIMITED','JINDAL');
11. SELECT LENGTH('O P JINDAL SCHOOL, RAIGARH');
12. SELECT LENGTH(12345 );
13. select left('JS12345/OPJS/XII/A',7);
14. select substr('JS12345/OPJS/XII/A',9,4);
15. select right('JS12345/OPJS/XII/A',5);
16. select MOD(23,5);
17. select power(3,4);
18. select ROUND(20.392,1);
19. select sign(-945);
20. select sqrt(25);
21. select truncate(129.345,1);
Worksheet 3
1. select pow(2,3), power(-2,3), pow(3,4);
2. select round(12345.789,2), round(1434.56,-1);
3. select round(62.789),round(6.89,0);
4. select truncate(466.789,1), truncate(645.56,-1);
5. select sqrt(81)+20 from dual;
6. select mod(23,2), mod(78,4);
7. select sign(15,-15), sign(-25), sign(70);
8. select char(65,67,69) from dual;
9. select concat(“info”,”rmatics”);
10. select concat(“ISM -”,concat(“xii”,”I”));
11. select lower(“INFORM”),lcase(“Class XII”);
12. select upper(“Class xii”),ucase(“informatics”);
13. select substring(“India is the Best”,3,2),substr(“Indian”,-2,1);
14. select length(trim(“ abcde defe “));
15. select instr(“Informatics”,”r”);
16. select length(“ab cde fge”);
17. select left(“Informatics”,4) from dual;
18. select right(“Informatics”,6);
19. select mid(“Indian School Muscat”,8,6);
20. Select curdate(), current_date();
21. Select date(now());
22. Select month(now());
23. Select year(“2012-02-21”);
24. Select dayname(now());
25. Select dayofmonth(“2011-03-23”);
26. Select dayofweek(now());
27. Select dayofyear(“2016-02-04”);
28. Select dayofyear(“2012-02-02”);
29. Select NOW(),SLEEP(3),SYSDATE();
WORKSHEET 1 ANSWERS
Select Round(546.345, -2);
Output: 500.00
Explanation: ROUND rounds the number to the nearest hundred (position -2).
Select SIGN(-341.45);
Output: -1
Explanation: SIGN returns -1 for negative numbers.
Select SIGN(0);
Output: 0
Explanation: SIGN returns 0 for zero.