Friday, July 24, 2020

Unit-III Database Query using SQL


SQL FUNCTIONS

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

Mathematical functions –Perform operation over numeric value.

POWER() – power() returns the value of a number raised to the power of another number. The synonym of power() is pow(). 

Syntax - pow(m,n)
where 'm' A number which is the base of the exponentiation and 'n' A number which is the exponent of the exponentiation. 

Example :-

Mysql> select pow(2,3); 
Mysql>8 
Mysql> select pow(2.37,3.45); 
Mysql>19.6282……

ROUND() – the round() function returns a number rounded to a certain number of decimal places. 

Syntax - ROUND(column_name,decimals) 

where column_name -Required the field to round. 
decimals -Required, Specifies the number of decimals to be returned.

Example :-

Mysql> select round(454.352,2); 
Mysql>454.35
Mysql> select round(454.352,0); 
Mysql>454

MOD() – The MOD() function returns the remainder of one number divided by another. The following shows the syntax of the MOD() function: 

Syntax - MOD(dividend,divisor) 

Dividend - is a literal number or a numeric expression to divide. 
Divisor- is a literal number or a numeric expression by which to divide the dividend. 

Example:-

Mysql> SELECT MOD(11,3); 
Mysql>2 
Mysql> SELECT MOD(10.5,3); 
Mysql>1.5


Text functions

Text functions- Perform operation over string values. 

UPPER() – UPPER(str) Returns the string str with all characters changed to uppercase. 

mysql> SELECT UPPER(‘india'); 
Mysql> ‘INDIA' 

Note: UCASE(str)-UCASE() is a synonym for UPPER(). 

LOWER(str)-Returns the string str with all characters changed to lowercase 

mysql> SELECT LOWER('JAIPUR'); 
Mysql> 'jaipur’

Note: LCASE(str) LCASE() is a synonym for LOWER()

SUBSTRING

Syntax 1:
SUBSTRING(str,pos) 
The forms without a len argument return a substring from string str starting at position pos. 

mysql> SELECT SUBSTRING(‘practically',5); 
-> 'tically' 

Syntax 2: 
SUBSTRING(str FROM pos), 
The forms with a len argument return a substring len characters long from string str, starting at position pos. 

mysql> SELECT SUBSTRING('informatics ' FROM 4); 

-> ‘ormatics' 


Syntax 3: 
SUBSTRING(str,pos,len)

mysql> SELECT SUBSTRING('Quadratically',5,6); 
-> 'ratica' 


The forms that use FROM are standard SQL syntax. It is also possible to use a negative value for pos. 
In this case, the beginning of the substring is pos characters from the end of the string, rather than the beginning. 


mysql> SELECT SUBSTRING(‘Ankita', -3); 
-> 'ita' 


mysql> SELECT SUBSTRING(‘Ankita', -5, 3); 
-> 'nki'

LENGTH(str) - Returns the length of the string str 

mysql> SELECT LENGTH('text'); 
-> 4 

LEFT(str,len) - Returns the leftmost len characters from the string str, or NULL if any argument is NULL. 

mysql> SELECT LEFT(‘Toolbar',4); 
-> ‘Tool‘ 

RIGHT(str,len)-Returns the rightmost len characters from the string str, or NULL if any argument is NULL. 

mysql> SELECT RIGHT(‘Toolbar',3); 
-> 'bar'

INSTR(str,substr)-Returns the position of the first occurrence of substring substr in string str. 

mysql> SELECT INSTR(‘Toobarbar','bar'); 
-> 4
mysql> SELECT INSTR('xbar',‘ybar'); 
-> 0

LTRIM(str)-Returns the string str with leading space characters removed. 

mysql> SELECT LTRIM(' Toolbar'); 
-> ‘Toolbar‘ 

RTRIM(str)-Returns the string str with trailing space characters removed. 

mysql> SELECT RTRIM(‘Toolbar '); 
-> ‘Toolbar‘ 

TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)- Returns the string str with all remstr prefixes or suffixes removed. If none of the specifiers BOTH, LEADING, or TRAILING is given , BOTH is assumed. 

mysql> SELECT TRIM(' tool '); 
-> 'tool' 
mysql> SELECT TRIM(LEADING 'x' FROM 'xxtoolxx'); 
-> ‘toolxx' 
mysql> SELECT TRIM(BOTH 'x' FROM 'xxtoolxx'); 
-> ‘tool' 
mysql> SELECT TRIM(TRAILING 'x' FROM ‘toolxx'); 
-> ‘tool'

Date functions

Date functions- Perform operation over date values. 

NOW()-Returns the current date and time as a value in 'YYYY-MM-DD hh:mm:ss' or YYYYMMDDhhmmss format, depending on whether the function is used in string or numeric context. 
mysql> SELECT NOW(); 
-> '2020-04-05 23:50:26' 
mysql> SELECT NOW() + 0; 
-> 20200415235026.000000 
Here +0 means +0 second 

DATE(expr)-Extracts the date part of the date or datetime expression expr. 

mysql> SELECT DATE('2003-12-31 01:02:03'); 
-> '2003-12-31'

MONTH(date)-Returns the month for date, in the range 1 to 12 for January to December, or 0 for dates such as '0000-00-00' or '2008-00-00' that have a zero month part. 

mysql> SELECT MONTH('2008-02-03'); 
-> 2 

MONTHNAME (date)-Returns the full name of the month for date. 

mysql> SELECT MONTHNAME('2008-02-03'); 
-> 'February‘ 

YEAR(date)-Returns the year for date, in the range 1000 to 9999, or 0 for the “zero” date. 

mysql> SELECT YEAR('1987-01-01'); 
-> 1987 

DAY(date)-Returns the day of the month for date, in the range 1 to 31, or 0 for dates such as '0000-00-00' or '2008-00-00' that have a zero day part. 

mysql> SELECT DAYOFMONTH('2007-02-03'); 
-> 3 

DAYNAME(date)-Returns the name of the weekday for date. 

mysql> SELECT DAYNAME('2007-02-03'); 
-> 'Saturday'



Aggregate Functions

Aggregate Functions & NULL- Perform operation over set of values. 

Sum()
Min()
Max()
AVG()
Count()

Consider a table Emp having following records as Null values are excluded while (avg)aggregate function is used .
              
              Table EMP
Emp Code         Name      Sal 
E1               Mohak     NULL 
E2               Anuj      4500 
E3               Vijay     NULL 
E4               Vishal    3500 
E5               Anil      400

SQL Queries 
mysql> Select Sum(Sal) from EMP; 
-> 12000
mysql> Select Min(Sal) from EMP; 
->3500
mysql> Select Max(Sal) from EMP;
-> 4500 
mysql> Select Count(Sal) from EMP; 
-> 3
mysql> Select Avg(Sal) from EMP;
-> 4000 
mysql> Select Count(*) from EMP;
-> 5 

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

Group by: 

Group by clause is used to divide the table into logical groups and we can perform aggregate functions in those groups. in this case aggregate function will return output for each group. 

Consider the following table student1

mysql>select sum(marks) from student1 group by age;
-> 195
   284

HAVING Clause

if we want to filter or restrict some rows from the output produced by GROUP BY then we use HAVING clause. it is used to put condition of group of rows. with HAVING clause we can use aggregate functions also.
WHERE clause is used before GROUP BY . with WHERE  we cannot use aggregate functions.

select max(marks) from student1 group by age having count(*)>1;
-> Max(marks)
   98
   98

ORDER BY 

by default records will come in the output in the same order in which it was entered. To see the output rows in sorted or arragned in ascending or descending order SQL provide order by clause. by default output will be ascending order(ASC) to see output in descending order we use DESC clause with order by.

select * from student1 order by name;

select * from student1 order by name  desc;


Assignment

Quiz1


Previous Years CBSE Board Examination Questions

2 Marks Questions

Question 1.
Write the output of the following SQL queries:
(i) SELECT RIGHT (‘software’, 2);
(ii) SELECT INSTR (‘twelve’, 'l');
(iii) SELECT DAYOFMONTH (‘2014-03-01’);
(iv) SELECT ROUND  (76.987,2); 


Answer:

(i) re
(ii) 4
(iii) 01
(iv) 76.99


Question 2.
There is a column Salary in a Table EMPLOYEE. The following two statements are giving different outputs. What may be the possible reason? 
SELECT COUNT(*) FROM EMPLOYEE;
SELECT C0UNT(Salary) FROM EMPLOYEE;

Answer:
SELECT COUNT (*) FROM EMPLOYEE:
This statement returns the number of records in the table.
SELECT COUNT(Salary) FROM EMPLOYEE;
This statement returns the number of values (NULL values will not be counted) of the specified column.

Question 3.

A table FLIGHT has 4 rows and 2 columns and another table AIRHOSTESS has 3 rows and 4 columns. How many rows and columns will be there if we obtain the cartesian product of these two tables? 


Answer:

Total number of rows will be 12 and total number of columns 6.

Question 4. 
What is the purpose of GROUP BY clause in MySQL? How is it different from ORDER BY clause? 

Answer:

The GROUP BY clause can be used to combine all those records that have identical value in a particular field or a group of fields. Whereas, ORDER BY clause is used to display the records either in ascending or descending order based on a particular field. For ascending order ASC is used and for descending order, DESC is used. The default order is ascending order.


Question 5.  
Shanya Khanna is using a table EMPLOYEE. It has the following columns:
Admno, Name, Agg, Stream [column Agg contains Aggregate marks]
She wants to display highest Agg obtained in each Stream.
She wrote the following statement:
SELECT Stream, MAX(Agg) FROM EMPLOYEE;
But she did not get the desired result. Rewrite the above query with necessary changes to help her get the desired output.
Answer:
SELECT Stream, MAX(Agg)
FROM EMPLOYEE
GROUP BY Stream;

Question 6.
State difference between date functions NOW() and SYSDATE() of MySql.
Answer:
Differences between Now() and SYSDATE() of MySql are as follows:
Informatics Practices Class 12 Important Questions Chapter 11 SQL Functions and Table Joins 15

7 Marks Questions

Question 7. Consider the following table named SBOP with details of account holders. Write commands of MySql for (i) to (iv) and output for (v) to (vii).
TABLE SBOP
Informatics Practices Class 12 Important Questions Chapter 11 SQL Functions and Table Joins 1 (i) To display Accountno, Name and DateOfopen of account holders having transactions more than 8. (ii) To display all information of account holders whose transaction value is not mentioned. (iii) To add another column Address with datatype and size as VARCHAR(25). (iv) To display the month day with reference to DateOfopen for all the account holders. 
(v) SELECT COUNT (*) FROM SBOP;
(vi) SELECT Name, Balance FROM SBOP WHERE Name LIKE “%i”;
(vii) SELECT ROUND (Balance,-3) FROM SBOP
       WHERE Accountno="SB-5” ;

Answer:
(i) SELECT Account no, Name, DateOfopen FROM SBOP WHERE Transaction > 8:
(ii) SELECT * FROM SBOP WHERE Transaction IS NULL;
(iii) ALTER TABLE SBOP ADD Address VARCHAR(25);
(iv) SELECT DAY0FM0NTH(DateOfopen), Name FROM SBOP:
Informatics Practices Class 12 Important Questions Chapter 11 SQL Functions and Table Joins 16

Question 8. Consider the following table named EXAM with details of marks. Write command of MySQL for (i) to (iv) and output for (v) to (vii). Table EXAM Informatics Practices Class 12 Important Questions Chapter 11 SQL Functions and Table Joins 2 (i) To display all information of the students of humanities in descending order of percentage. (ii) To display Adno, Name, Percentage and Stream of those students whose name is less than 6 characters long. (iii) To add another column Bus_Fees with datatype and size as Decimal(8,2). (iv) To increase percentage by 2% of all the Humanities students. (All India 2014)
(v) SELECT COUNT(*) FROM EXAM;
(vi) SELECT SName, Percentage FROM EXAM WHERE Name LIKE “N%”;
(vii) SELECT ROUND (Percentage ,0) FROM EXAM WHERE Adno=“R005”;
Answer:
(i) SELECT * FROM EXAM WHERE Stream = ‘Humanities’
    ORDER BY Percentage DESC;
(ii) SELECT Adno, SName, Percentage, Stream FROM EXAM
     WHERE LENGTH(SName)<6;
(iii) ALTER TABLE EXAM
      ADD Bus_Fees DECIMALS, 2) ;
(iv) UPDATE EXAM
     SET Percentage = Percentage + 2 
      WHERE Stream = ‘Humanities’;
Informatics Practices Class 12 Important Questions Chapter 11 SQL Functions and Table Joins 17
(vi) The given query will result in an error as there is no column named         Name in table EXAM.
Informatics Practices Class 12 Important Questions Chapter 11 SQL Functions and Table Joins 18

Question 9. Consider the table SUPPLIER given below. Write commands in MySQL for (i) to (iv) and output for (v) to (vii). (i)
TABLE SUPPLIER
Informatics Practices Class 12 Important Questions Chapter 11 SQL Functions and Table Joins 3 (iii) To count distinct City in the table. (iv) To insert a new row in the table SUPPLIER.        110, “Bournvita”,’ ABC’, 170, ‘Delhi’, 40.00 (Delhi 2012)
(v) SELECT Pname FROM SUPPLIER WHERE Supname IN ('Bread', 'Maggt');
(vi) SELECT COUNTCDISTINCT City) FROM SUPPLIER;
(vii) SELECT MAX(Price) FROM SUPPLIER WHERE City = ‘Kol kata’;
Answer:
(i) SELECT Pname FROM SUPPLIER WHERE Pname LIKE ‘B%’ ORDER BY Price;
(ii) SELECT Scode, Pname, City  FROM SUPPLIER WHERE Qty < 150;
(iii) SELECT COUNT(DISTINCT City) FROM SUPPLIER;
(iv) INSERT INTO SUPPLIER VALUES      (110,‘Bournvita’,‘ABC’,170,‘Delhi’,40.00);
Informatics Practices Class 12 Important Questions Chapter 11 SQL Functions and Table Joins 19
Question 10. Consider the table PERSONS given below. Write commands in SQL for (i) to (iv) and write output for (v) to (viii).
TABLE PERSONS
Informatics Practices Class 12 Important Questions Chapter 11 SQL Functions and Table Joins 5 (i) Display the SurName, FirstName and City of people residing in Udhamwara city. (ii) Display the Person IDs (PID), Cities and Pincode of persons in descending order of Pincode. (iii) Display the FirstName and City of all the females getting Basic salaries above 40000. (iv) Display FirstName and BasicSalaries of all the persons whose first name start with ‘G’. 
(v) SELECT SurName FROM PERSONS WHERE BasicSalary>= 50000;
(vi) SELECT SUM (BasicSalary) FROM PERSONS WHERE Gender = ‘F’;
(vii) SELECT Gender, MIN (BasicSalary) FROM PERSONS GROUP BY Gender;
(viii) SELECT Gender, COUNT (*) FROM PERSONS GROUP BY Gender;
Answer:
(i) SELECT SurName, FirstName, City FROM PERSONS WHERE City = ‘Udhamwara’;
(ii) SELECT PID, City. PinCode FROM PERSONS ORDER BY Pincode DESC;
(iii) SELECT FirstName, City FROM PERSONS WHERE Gender = ‘F’ AND
       BasicSalary > 40000;
(iv) SELECT FirstName, BasicSalary FROM PERSONS WHERE FirstName LIKE ‘G%’;
Informatics Practices Class 12 Important Questions Chapter 11 SQL Functions and Table Joins 21

Wednesday, May 6, 2020

Data Visualization


Data Visualization

INTRODUCTION:


When data is shown in the form of pictures, it becomes easy for the user to understand it. So representing the data in the form of pictures or graph is called “data visualization”. 
It represents patterns, trends, correlations etc. In data and thereby helps decision makers to understand the meaning of data for making decision in business.

Several data visualization libraries are available in Python, namely Matplotlib, Seaborn, and Folium etc.

Matplotlib is a python library which provides many interfaces and function to present data in 2D graphics. We can say, Matplotlib is a high quality plotting library of Python.



Matplotlib library offers many different collections of sub modules; Pyplot is one such sub module.



Pyplot is a collection of methods within Matplotlib library which allows user to construct 2D plots easily.


Following features are provided in matplotlib library for data visualization.
  • Drawing–plots can be drawn based on passed data through specific functions.
  • Customization–plots can be customized as per requirement after specifying it in the arguments of the functions. Like color,style (dashed,dotted), width; adding label,title,and legend in plots can be customized.
  • Saving–After drawing and customization plots can be saved for future use.



Installing and importing Matplotlib-


With Standard Installation : Next we need to install it by giving following command:

python –m pip install –U pip 
python –m pip install –U matplotlib
or
pip install matplotlib in command prompt
To use Pyplot for data visualization, we have to first import it in our python environment.

import matplotlib.pyplot as plt


Types of plot using matplotlib

  • LINE PLOT
  • BAR GRAPH
  • HISTOGRAM
  • PIE CHART
  • FREQUENCY POLYGON
  • BOX PLOT
  • SCATTER PLOT

Line Plot:

A line plot / chart is a graph that shows the frequency of data occurring along a number line.


The line plot is represented by a series of data points connected with a straight line. Generally line plots are used to display trends over time.

A line plot or line graph can be created using the plot()function available in pyplot library. We can, not only just plot a line but we can explicitly define the grid, the x and y axis scale and labels,title and display options etc.



1.Simple Line Draw:




2. Setting label of x and y axis , adding title  



3. Changing the line color , line width and line style




4. Changing the Marker Type, Size and color



Bar Graph
A bar graph is used to represents data in the form of vertical or horizontal bars.  It is useful to compare the quantities.

Example-1  

Changing Width, Color in Bar Chart
Example-2 



Example-3



Horizontal Bar Graph:

barh() is used to draw horizontal bar graph




Multiple Bar Graph:

To draw multiple bar chart:

  • Decide the no. of X points, we can use arange() or linspace() function to find no. of points based on the length of values in sequence.
  • Decide the thickness of each bar and accordingly adjust X point on X-axis
  • Give different color to different data ranges
  • The width remains the same for all ranges being plotted
  • Call plot() for each data range


    Example-4




           Example-5

Pie Chart



A pie chart shows a circle that is divided into sectors and each sector represents a proportion of the whole.

Pie Charts shows proportions and percentages between categories,by dividing a circle into proportional segments/parts. Each arc length represents a proportion of each category, while the full circle represents the total sum of all the data,equal to 100%.

import matplotlib.pyplot as plt
#Data to plot
labels='Candidate1','Candidate2','Candidate3','Candidate4'
votes=[315,130,245,210]
sizes=votes
colors=['gold','yellowgreen','lightcoral','lightskyblue']
explode=(0.1,0,0,0)#explode 1st slice
#Plot
plt.pie(sizes,explode=explode,labels=labels,colors=colors,
autopct='%1.1f%%',shadow=True,startangle=140)
plt.axis('equal')
plt.show()

OUTPUT:

The pie chart drawn using the Matplotlib.pyplot can be customized of its several aspects:-

  • Sometimes we want to emphasize on one or more slice and show them little pulled out. This feature is called explode in pie chart ·If we want to explode or stand out 2nd and 3rd slice out of 5 slices to 0.2 and 0.3 unit respectively , explode will be [0,0.2,0.3,0,0]. The value of explode vary from 0.1 to 1 to show that how much a slice will come out of pie chart. 
  • The start angle parameter rotates the pie chart by the specified number of degrees.The rotation is counter clockwise and performed on X Axis of the pie chart.
  • Shadow effect can be provided using the shadow parameter of the pie()function. Passing True will make a shadow appear below the rim of the pie chart. By default value of shadow is False and there will be no shadow of the pie chart.
  • Shadow= True indicates that the pie chart should be displayed with a shadow. This will improve the look of the chart.
  • The wedges of the pie chart can be further customized using the wedge prop parameter. A python dictionary with the name value pairs describing the wedge properties like edge color,line width can be passed as the wedge prop argument.
  • By setting the frame argument to True, the axes frame is drawn around the pie chart.
  • Autopct parameter of the arc()function control s how the percentages are displayed in the wedges. Either format string starting with a% can be specified or a function can be specified.
  • autopct : allows to view percentage of share in a pie chart-

    The option autopct=’%.1f %%’ indicates how to display the percentages on the slices. Here %.1 shows that the percentage value should be displayed with 1 digit after decimal point. The next two % symbols indicates that only one symbol is to be displayed. 
  • e.g.,%.1f will display percentage values in the format 25.0,35.2 and soon. %.2f%% will display percentage values in the format 50.25,75.5 and soon.

Histogram


Histogram shows distribution of values. Histogram is similar to bar graph but it is useful to show values grouped in bins or intervals.

Histogram provide s a visual interpretation of numerical data by showing the number of data points that fall within a specified range of values(“bins”). It is similar to a vertical bar graph but without gaps between the bars.

For example- we can collect the age of each employee in a office and show it in the form of a histogram to know how many employees are there in the range 0-10 years, 10-20 years and so on. For this we can create histogram like this- 



Example: 2




Frequency Polygons

Frequency polygon is a way for understanding the shape of distributions. It connects the top center point of each bins and then we get the relative frequency polygon. It has the same purpose as the histogram have but is used specially for comparing sets of data.




Box Plot


A Box plot is graphical representation of the five number summary of given data set. It includes-
1. Maximum
2. Minimum
3. 1st Quartile
4. ND Quartile (Median)
5. 3RD Quartile

Example: 1
Example: 2



Scatter Chart

A scatter plot is a type of plot that shows the data as a  collection of points in the form of dots, and shows the  relationship between two variables - one plotted along the x-  axis and the other plotted along y-axis.
Syntax- Scatter(x, y, color, marker
Marker-  is  a  symbol  (style)  for  representing  data  point.  Following is a list of valid marker style-
Marker
Description
‘s’
Square Marker
‘o’
Circle Marker
‘d’
Diamond Marker
‘x’
Cross Marker
‘+’
Plus Marker
‘^’
Triangle down
‘v’
Triangle Up

Example: 1

Example: 2




How to save plot


For future use we have to save the plot.To save any plot savefig() method is used . plot scan be saved like pdf,svg,png,jpg file formats.
plt.savefig('line_plot.pdf')
plt.savefig('line_plot.svg')
plt.savefig('line_plot.png')
Parameter for saving plots .e.g.
plt.savefig('line_plot.jpg', dpi=300, quality=80, optimize=True, progressive=True)


Which Export Format to Use?

The export as vector-based SVG or PDF files is generally preferred over bitmap-based PNG or JPG files as they are richer formats, usually providing higher quality plots along with smaller file sizes.

Example: 

****************************************************

Assignment 


1. Which function is used to pass arguments for drawing bar graph in python?
2. Name the function which is used to draw horizontal bar graph in Python?
3. What is use of legend function in bar graph?
4. What is use of xlim() function in bar graph?
5. What is use of xticks() function in bar graph?