sql statements , functions & joins

32

Upload: baabtracom-first-coding-school-in-india

Post on 20-May-2015

386 views

Category:

Technology


1 download

DESCRIPTION

Sql 
statements , functions & joins

TRANSCRIPT

Page 1: Sql statements , functions  &  joins
Page 2: Sql statements , functions  &  joins

Maneesha K [email protected]/

usernametwitter.com/usernamein.linkedin.com/in/Maneesha7559813984

SQL STATEMENTS , FUNCTIONS & JOINS

Page 3: Sql statements , functions  &  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 statements , functions  &  joins

SQL STATEMENTS A query/command to perform a task

in a database.

Page 5: Sql statements , functions  &  joins

CREATEALTER Data Definition Language(DDL)DROP

INSERTUPDATE Data Manipulation Language (DML)DELETE

GRANTREVOKE Data Control Language(DCL)

Page 6: Sql statements , functions  &  joins

DDL - Data Definition Language

1. CreateTo Create a Database.

Syntax :create database databasename;

Eg :create database student;

2. UseSyntax :

use databasename;

Eg :use student;

Page 7: Sql statements , functions  &  joins

To Create Table

Syntax :create table <TableName>( ColumnName1 Datatype(Size) Constraint , ColumnName2 Datatype(Size) Constraint , -------------------------------------------------------);

Eg : create table tbl_student(pk_int_roll_no int(4) not null primary key,vchr_name varchar(20),char_gender char(2));

Page 8: Sql statements , functions  &  joins

3. Alter TableTo change the structure of the table.

alter table tbl_student add (int_marks int(2));alter table tbl_student drop column char_gender;alter table tbl_student change vchr_name vchr_name

varchar(30);

4. Drop TableTo remove the tables.

Syntax :drop table tablename;

Eg :drop table tbl_student;

Page 9: Sql statements , functions  &  joins

DML - Data Manipulation Language

1. Insert To insert rows into a table.

Syntax :insert into tablename(columnname1,columnname2,…)values(value1,value2,…);

Eg :insert into tbl_student(pk_int_roll_no,vchr_name,int_marks)values(1,’Anu’,96);

Page 10: Sql statements , functions  &  joins

2. Select

To extract data from the database.(SELECT identifies what columns FROM identifies which table)

Syntax :

select * / [distinct]column_name from table_name where [condition];

Eg :

•select * from tbl_student;•select pk_int_roll_no,vchr_name from tbl_student;•select vchr_name from tbl_student where int_marks>80;

Page 11: Sql statements , functions  &  joins

3. Update

To change the contents(rows /columns) of the table.

Syntax :update tablename set columnname=valueswhere condition;

Eg : update tbl_student set int_marks=89 where pk_int_roll_no=2;

Page 12: Sql statements , functions  &  joins

4. Delete

To delete rows from the table.

Syntax :delete from tablename where [condition];

Eg :delete from tbl_student where int_marks<40;

Page 13: Sql statements , functions  &  joins

DCL - Data Controll Language1. Grant

Syntax : grant privilage_type on tablename to

‘user_name'@'localhost';

Eg :grant all on tbl_student to ‘manu’@’localhost’;

Page 14: Sql statements , functions  &  joins

2 . Revoke

Syntax : revoke privilege_type on table_name from

‘user_name'@'localhost';

Eg :revoke select on tbl_student from ‘manu'@'localhost';

Page 15: Sql statements , functions  &  joins

FUNCTIONS

Page 16: Sql statements , functions  &  joins

SQL - Functions

SQL functions are used for performing calculations on data.

Types of Functions

Aggregate FunctionsScalar Functions

Page 17: Sql statements , functions  &  joins

Aggregate Functions

•The functions that can be applied to all rows in table or to a subset of the table specified by the where clause.

•Functions return single value for a group of rows in the table.

•Sum()•Avg()•Min()•Max()•Count()

Page 18: Sql statements , functions  &  joins

pk_int_roll_no vchr_name int_marks

1 Anu 96

2 Veena 89

3 Amal 79

4 Reema 92

•select max(int_marks),min(int_marks),avg(int_marks) from tbl_student;

Max(int_marks) min(int_marks) avg(int_marks)96 79 81.7

Page 19: Sql statements , functions  &  joins

•select count(pk_int_roll_no)as total_students from tbl_student;

total_students4

Page 20: Sql statements , functions  &  joins

Group By

To group the result set by one or more column.Often used in conjunction with aggregate function.Syntax :

Select column_name, aggregate_function(column_name)from table_namegroup by column_name;

Eg :Select vchr_name,sum(int_marks) from tbl_student group by vchr_name;

Page 21: Sql statements , functions  &  joins

Scalar function These functions return a single value for each values of a particular column given as input.

•Lcase()•Ucase()•Round()Eg :Select ucase(vchr_name) from tbl_student;Select lcase(vchr_name) from tbl_student;Select round(int_marks) from tbl_student where pk_int_roll_no=3;

Page 22: Sql statements , functions  &  joins

JOINS

Page 23: Sql statements , functions  &  joins

JOINS

To combine rows from two or more tables.

Types of Joins ::1. Inner join2. Outer join

a) Left joinb) Right join

Page 24: Sql statements , functions  &  joins

INNER JOIN

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

Syntax :select column_name(s) from table1 join table2 on table1.column_name=table2.column_name;

Page 25: Sql statements , functions  &  joins

select stud_name,dept_name from tbl_student join tbl_deptwhere dept_id=fk_int_dept_id;

stud_id stud_name fk_int_dept_id

1 Anu 1

2 Veena 3

3 Abhi 2

4 John 1

dept_id dept_name

1 CS

2 EC

3 QS

stud_name dept_name

Anu CS

Veena QS

Abhi EC

John CS

Inner

join

Page 26: Sql statements , functions  &  joins

LEFT OUTER JOIN

Returns all rows from the left table (table1), with the matching rows in the right table (table2).Syntax :

select column_name(s)from table1left join table2on table1.column_name=table2.column_name;

Page 27: Sql statements , functions  &  joins

select stud_name,dept_name from tbl_student left join tbl_dept where dept_id=fk_int_dept_id;

stud_id stud_name fk_int_dept_id

1 Anu 1

2 Veena 3

3 Abhi 4

4 John 1

dept_id dept_name

1 CS

2 EC

3 QS

left

join

stud_name dept_name

Anu CS

Veena QS

Abhi NULL

John CS

Page 28: Sql statements , functions  &  joins

RIGHT OUTER JOIN Return all rows from the right table, and the matched

rows from the left table.Syntax :

select column_name(s)from table1right join table2on table1.column_name=table2.column_name;

Page 29: Sql statements , functions  &  joins

select stud_name,dept_name from tbl_student lright join tbl_dept where dept_id=fk_int_dept_id;

stud_id stud_name fk_int_dept_id

1 Anu 1

2 Veena 3

3 Abhi 4

4 John 1

dept_id dept_name

1 CS

2 EC

3 QS

Right

join

stud_name dept_name

Anu CS

Veena QS

Abhi NULL

John CS

Page 30: Sql statements , functions  &  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 31: Sql statements , functions  &  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 32: Sql statements , functions  &  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