introduction to mysql part 2

33
Recall • What is a database? • What is DBMS? • Examples for DDL,DML,DCL statements • How can I retrieve a particular row from a table?

Upload: baabtracom-no-1-supplier-of-quality-freshers

Post on 26-Jun-2015

2.870 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Introduction to mysql part 2

Recall

• What is a database?• What is DBMS?• Examples for DDL,DML,DCL

statements• How can I retrieve a particular row

from a table?

Page 2: Introduction to mysql part 2

Introduction to MySQL

Grouping, Scalar and aggregate functions, Joins

Day 2,Week 5

Page 3: Introduction to mysql part 2

SQL Functions

• SQL has many built-in functions for performing calculations on data. It can be broadly classified in to two

–Aggregate Functions– Scalar functions

Page 4: Introduction to mysql part 2

SQL Functions• Aggregate Functions– SQL aggregate functions return a single

value, calculated from all the values in a column.

• Select AVG(int_price) from tbl_stock; // returns 8

• Select SUM(int_price) from tbl_stock; // returns 33

• Select MIN(int_price) from tbl_stock; // returns 2

• Select COUNT(vchr_product) from tbl_stock; // returns 4

Pk_int_id Vchr_product Int_price

1 Pen 10

2 Book 15

3 Eraser 2

4 Pencil 6

Tbl_stock Select SUM(int_price) from tbl_stock;

Returns a single value

Page 5: Introduction to mysql part 2

SQL Functions• Scalar Functions– SQL scalar functions return a single value for

each values of a particular column given as input.

• Select UCASE(vchr_product) from tbl_stock; // returns each column value in capital letter

• Select LCASE(vchr_product) from tbl_stock; // returns each column value in small letter

• Select ROUND(int_price) from tbl_stock; // returns each column value in a rounded figure

Pk_int_id Vchr_product Int_price

1 Pen 10

2 Book 15

3 Eraser 2

4 Pencil 6

Tbl_product

Returns a value

Returns a value

Returns a value

Returns a value

Page 6: Introduction to mysql part 2

Grouping Data

• GROUP BY allows you to take your result set, group it into logical groups and then run aggregate queries on the groups.

• You could for instance select all employees, group them by their workplace location and calculate the average salary. This would give you the average salary of an employee at a given location in your database.Emp_id Emp_name Emp_age Emp_email int_salary vchr_place

1000 Deepak 24 [email protected] 10000 Cochin

1001 Aneesh 23 [email protected] 20000 Calicut

1002 Naveen 25 [email protected] 15000 Cochin

1003 Jacob 25 [email protected] 13000 Calicut

Tbl_employee

Avg = 12500

Avg = 11500

Page 7: Introduction to mysql part 2

Select vchr_place, avg(int_salary) from tbl_employee group by

vchr_place;

Example

Result :

Emp_id Emp_name Emp_age Emp_email int_salary vchr_place1000 Deepak 24 [email protected] 10000 Cochin

1001 Aneesh 23 [email protected] 20000 Calicut

1002 Naveen 25 [email protected] 15000 Cochin

1003 Jacob 25 [email protected] 13000 Calicut

Tbl_employee

Vchr_place Avg(int_alary)

Cochin 12500

Calicut 11500

Page 8: Introduction to mysql part 2

The HAVING Clause• The HAVING clause was added to SQL because the

WHERE keyword could not be used with aggregate functions.

• An Sql statement can have both ‘where’ clause and ‘having’ clause. Where filteres data before grouping.Having filters data after grouping

• Syntax: • SELECT column_name,

aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_nameHAVING aggregate_function(column_name) operator value;

Page 9: Introduction to mysql part 2

Example

Select vchr_place, avg(int_salary) from tbl_employee group by vchr_place having avg(int_salary)>12000;

Vchr_place Avg(int_alary)

Cochin 12500

Calicut 11500

Vchr_place Avg(int_alary)

Cochin 12500

Page 10: Introduction to mysql part 2

Joins

Page 11: Introduction to mysql part 2

Where do we use joins?

• An SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.• Join helps to write single query to fetch data from multiple tables so as to meet the business requirement/generate reports

Page 12: Introduction to mysql part 2

Types Of joins

• Inner join• Outer Join–Left Outer Join–Right Outer Join

Page 13: Introduction to mysql part 2

Inner Join

• The INNER JOIN keyword selects all rows from both tables as long as there is a match between the columns in both tables.

• Syntax– SELECT column_name(s) FROM

table1 INNER JOIN table2 ON table1.column_name=table2.column_name;

Page 14: Introduction to mysql part 2

Pk_int_id Vchr_place

1 Mumbai

2 Kolkata

3 Bangalore

4 Cochin

Emp_id Emp_name fk_int_place_id

1000 Deepak 1

1001 Aneesh 3

1002 Naveen 2

1003 Jacob 5

Emp_id Emp_name Vchr_place1000 Deepak Mumbai

1001 Aneesh Banglore

1002 Naveen Kolkatta

Inner Join

Inner Join

SELECT emp_id,emp_name, vchr_place from tbl_employee Join tbl_place on fk_int_place_id=pk_int_id

Page 15: Introduction to mysql part 2

Left Outer Join

• The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows in the right table (table2). The result is NULL in the right side when there is no match.

SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;

Page 16: Introduction to mysql part 2

Left Outer Join

Pk_int_id Vchr_place

1 Mumbai

2 Kolkata

3 Bangalore

4 Cochin

Emp_id Emp_name fk_int_place_id1000 Deepak 1

1001 Aneesh 4

1002 Naveen 2

1003 Jacob 5

1004 Alex 6

Emp_id Emp_name Vchr_place1000 Deepak Mumbai

1001 Aneesh Cochin

1002 Naveen Kolkatta

1003 Jacob NULL

1004 Alex NULL

Left Join

SELECT emp_id,emp_name, vchr_place from tbl_employee Left Join tbl_place on fk_int_place_id=pk_int_id

Page 17: Introduction to mysql part 2

Right Outer Join• The RIGHT JOIN keyword returns all rows

from the right table (table2), with the matching rows in the left table (table1). The result is NULL in the left side when there is no match.

SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name;

Page 18: Introduction to mysql part 2

Right Outer Join

Pk_int_id Vchr_place

1 Mumbai

2 Kolkata

3 Bangalore

4 Cochin

5 Trivandrum

7 Delhi

Emp_id Emp_name fk_int_place_id1000 Deepak 1

1001 Aneesh 4

1002 Naveen 2

1003 Jacob 5

1004 Alex 6

Emp_id Emp_name Vchr_place1000 Deepak Mumbai

1001 Aneesh Cochin

1002 Naveen Kolkatta

1003 Jacob Trivandrum

NULL NULL Banglore

NULL NULL Delhi

Right Join

SELECT emp_id,emp_name, vchr_place from tbl_employee Right Join tbl_place on fk_int_place_id=pk_int_id

Page 19: Introduction to mysql part 2

Questions?“A good question deserve a

good grade…”

Page 20: Introduction to mysql part 2

Self Check !!

Page 21: Introduction to mysql part 2

Self Check !!

• Scalar function will return 0 or more values–True–False

Page 22: Introduction to mysql part 2

Self Check !!

• Scalar function will return 0 or more values–True–False

Page 23: Introduction to mysql part 2

Self Check !!

• Round() is an aggregate function–True–False

Page 24: Introduction to mysql part 2

Self Check !!

• Round() is an aggregate function–True–False

Page 25: Introduction to mysql part 2

Self Check !!

• Spot out the errors

Select dept,sum(salary), place from tbl_employee group by dept where sum(salary)>10000

Correct Answer:Select dept,sum(salary) from tbl_employee

group by dept having sum(salary)>10000

Page 26: Introduction to mysql part 2

Self Check !!

• Spot out the errors

Select dept,sum(salary), place from tbl_employee group by dept where sum(salary)>10000

Correct Answer:Select dept,sum(salary) from tbl_employee

group by dept having sum(salary)>10000

Result will be unexpected if display any columns without aggregate functions other than that given in group by

Where must be given before group by We cannot use aggregate functions with where condition

Page 27: Introduction to mysql part 2

Select id,student,dept from tbl_student join tbl_dept on

fk_dept_id =pk_int_id

Pk_int_id dept

1 Computer science

2 Electronics

3 Commerce

4 Art

id Student fk_dept_id1000 Ram 1

1001 Raju 4

1002 Mary 2

1003 Dona 5

1004 Lal 6

id Student dept1000 Ram Computer science

1001 Raju Art

1002 Mary Electronics

1003 Dona NULL

1004 Lal NULL

Query :

Page 28: Introduction to mysql part 2

Select id,student,dept from tbl_student left join tbl_dept on

fk_dept_id =pk_int_id

Pk_int_id dept

1 Computer science

2 Electronics

3 Commerce

4 Art

id Student fk_dept_id1000 Ram 1

1001 Raju 4

1002 Mary 2

1003 Dona 5

1004 Lal 6

id Student dept1000 Ram Computer science

1001 Raju Art

1002 Mary Electronics

1003 Dona NULL

1004 Lal NULL

Query :

Page 29: Introduction to mysql part 2

Self Check !!

• When to use inner join

– I want to display all the places and students from that places– I want to display all the students

and their places– I want to display only the students

of given places– I want to display the only students

from places given in another table

Page 30: Introduction to mysql part 2

Self Check !!

• When to use inner join

– I want to display all the places and students from that places– I want to display all the students

and their places– I want to display only the students

of given places– I want to display the only students

from places given in another table

Page 31: Introduction to mysql part 2

Self Check !!

• When to use Right join

A. Matching records from right table needs to be displayed

B. Mismatching records from right table needs to be displayed

C. Matching records from left table needs to be displayed

D. Mismatching records from left table needs to be displayed

E. Both A & BF. Both B & C

Page 32: Introduction to mysql part 2

Self Check !!

• When to use Right join

A. Matching records from right table needs to be displayed

B. Mismatching records from right table needs to be displayed

C. Matching records from left table needs to be displayed

D. Mismatching records from left table needs to be displayed

E. Both A & BF. Both B & C

Page 33: Introduction to mysql part 2

End of day