sql statement,functions and joins

35

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

Post on 26-Jun-2015

378 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Sql statement,functions and joins
Page 2: Sql statement,functions and joins

Yasir musthafa [email protected]

mwww.facebook.com/

yasirmusthafatwitter.com/yasirmusthafappin.linkedin.com/in/

yasirmusthafapp8891396749

Sql statements ,functions and joins

Page 3: Sql statement,functions and joins

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 4: Sql statement,functions and joins

Sql statements

• Query or command to perform a task in a database

3 type1. DDL2. DML3. DCL

Page 5: Sql statement,functions and joins

DDL

Data definition language• Create• Alter• Drop• truncate

Page 6: Sql statement,functions and joins

Create

Syntax in mysqlCREATE TABLE table_name

(column_name1 data_type(size),column_name2 data_type(size),....);Example

create table tbl_stock(pk_int_stock_id int auto_increment,vchr_name varchar(20),int_quantity int,int_price int,primary key(pk_int_stock_id));

Page 7: Sql statement,functions and joins

Alter

Syntax in mysqlAlter table table_name modify column

column_name data_type;Examplealter table tbl_stock modify column int_price

float;

Page 8: Sql statement,functions and joins

Drop

Syntax in mysqlDrop table table_name;Example Drop table tbl_student;

Page 9: Sql statement,functions and joins

Truncate

Syntax in mysqlTruncate table table_name;Example Truncate table tbl_student;

Page 10: Sql statement,functions and joins

DML

Data manipulation language• Select• Insert• Update• delete

Page 11: Sql statement,functions and joins

Select

Syntax in mysqlSelect * from table_name;Example Select * from tbl_stock;

Page 12: Sql statement,functions and joins

Insert

Syntax in mysqlinsert into table_name values( );Example insert into tbl_stock values(NULL,"mouse",10,500,1);

Page 13: Sql statement,functions and joins

Update

Syntax in mysqlUpdate table_name set column_name=Example • update tbl_stock set int_price=int_price+1.50;

Page 14: Sql statement,functions and joins

delete

Syntax in mysqlDelete from table_name; Example Delete from tbl_stock;

Difference between delete and truncate??

Page 15: Sql statement,functions and joins

DCL

Data control language• Grant• Revoke• Commit• rollback

Page 16: Sql statement,functions and joins

Grant

Syntax in mysqlGRANT privilege_type ON table_name TO

‘user_name'@'localhost';Example GRANT select ON tbl_supplier TO

'john'@'localhost';

Page 17: Sql statement,functions and joins

Revoke

Syntax in mysqlREVOKE privilege_type ON table_name FROM

‘user_name'@'localhost';Example REVOKE select ON tbl_supplier FROM

'suhail'@'localhost';

Page 18: Sql statement,functions and joins

functions

• Built in function-perform calculation on data1. Aggregate function2. Scalar function

Page 19: Sql statement,functions and joins

Aggregate function

• Return a single value for all the values in the column after calculation

AVG()Mysql syntaxSelect avg(coulmn_name) from table_name;ExampleSelect avg(int_salary) from employee;

COUNT(),FIRST(),LAST(),MAX(),MIN(),SUM()

Page 20: Sql statement,functions and joins

Group by

• To group the result set by one or more column• Often used in conjunction with aggregate

functionSyntax in mysqlSELECT column_name, aggregate_function(column_name)

FROM table_nameGROUP BY column_name;

ExampleSelect vchr_city,sum(mark) from tbl_student group by vchr_city;

Page 21: Sql statement,functions and joins

Scalar function

• Return single value for each value in a columnUCASE()Syntax in mysqlSelect UCASE (column_name) from table_name;ExampleSelect UCASE(first_name) from tbl_student;

• LCASE(),MID(),LEN(),ROUND(),NOW(),FORMAT()

Page 22: Sql statement,functions and joins

Joins

• Used to combine rows from two or more tables

Different joins• inner join• Left join• Right join

Page 23: Sql statement,functions and joins

Inner join

• Returns all rows when there is at least one match in BOTH tables

Syntax in mysqlSelect column_name(s) from table1 join table2 on

table1.column_name=table2.column_name;

Page 24: Sql statement,functions and joins

Pk_int_dept_id Vchr_dept_name

1 CS

2 EC

3 EE

4 MECH

pk_int_class_id Vchr_class_name int_dept_id1 CS100 1

2 CC300 5

3 EC100 2

4 MECH100 4

Exampleselect vchr_dept_name,vchr_class_name,pk_int_dept_id,int_dept_id from tbl_dept join tbl_classes on tbl_dept.pk_int_dept_id=tbl_classes.int_dept_id;

Page 25: Sql statement,functions and joins

The result set will be

Vchr_department_name

Vchr_class_name Pk_int_dept_id int_dept_id

CS CS100 1 1

EC EC100 2 2

MECH MECH100 4 4

Page 26: Sql statement,functions and joins

Left join

• returns all rows from the left table (table1), with the matching rows in the right table (table2).

• Syntax in mysqlSELECT column_name(s)

FROM table1LEFT JOIN table2ON table1.column_name=table2.column_name;

Page 27: Sql statement,functions and joins

Pk_int_dept_id Vchr_dept_name

1 CS

2 EC

3 EE

4 MECH

Pk_int_class_id Vchr_class_name int_dept_id

1 CS100 1

2 EC100 2

3 CC100 5

4 MECH100 4

select vchr_dept_name,vchr_class_name,pk_int_dept_id,int_dept_id from tbl_dept LEFT join tbl_classes on tbl_dept.pk_int_dept_id=tbl_classes.int_dept_id;

Page 28: Sql statement,functions and joins

The result will be

Vchr_dept_name

Vchr_class_name

Pk_int_dept_id

int_dept_id

CS CS100 1 1

EC EC100 2 2

EE NULL 3 NULL

MECH MECH100 4 4

Page 29: Sql statement,functions and joins

Right join

• Return all rows from the right table, and the matched rows from the left table

Syntax in mysqlSELECT column_name(s)

FROM table1RIGHT JOIN table2ON table1.column_name=table2.column_name;

Page 30: Sql statement,functions and joins

Pk_int_dept_id Vchr_dept_name

1 CS

2 EC

3 EE

4 MECH

Pk_int_class_id Vchr_class_name int_dept_name

1 CS100 1

2 EC100 2

4 MECH100 4

select vchr_dept_name,vchr_class_name,pk_int_dept_id,int_dept_id from tbl_dept RIGHT join tbl_classes on tbl_dept.pk_int_dept_id=tbl_classes.int_dept_id;

Page 31: Sql statement,functions and joins

The result will be

Vchr_dept_name

Vchr_class_name

Pk_int_dept_id

int_dept_id

CS CS100 1 1

EC EC100 2 2

MECH MECH100 4 4

NULL CC100 NULL 5

Page 32: Sql statement,functions and joins

Thank you

Page 33: Sql statement,functions and joins

Want to learn more about programming or Looking to become a good programmer?

Are you wasting time on searching so many contents online?

Do you want to learn things quickly?

Tired of spending huge amount of money to become a Software professional?

Do an online course @ baabtra.com

We put industry standards to practice. Our structured, activity based courses are so designed to make a quick, good software professional out of anybody who holds a passion for coding.

Page 34: Sql statement,functions and joins

Follow us @ twitter.com/baabtra

Like us @ facebook.com/baabtra

Subscribe to us @ youtube.com/baabtra

Become a follower @ slideshare.net/BaabtraMentoringPartner

Connect to us @ in.linkedin.com/in/baabtra

Give a feedback @ massbaab.com/baabtra

Thanks in advance

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 35: Sql statement,functions and joins

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Cafit Square,Hilite Business Park,Near Pantheerankavu,Kozhikode

Start up VillageEranakulam,Kerala, India.Email: [email protected]

Contact Us