Sql statements , functions & joins

Post on 20-May-2015

379 views 1 download

Tags:

description

Sql 
statements , functions & joins

Transcript of Sql statements , functions & joins

Maneesha K Kmaneeshakk16@gmail.comwww.facebook.com/

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

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

SQL STATEMENTS A query/command to perform a task

in a database.

CREATEALTER Data Definition Language(DDL)DROP

INSERTUPDATE Data Manipulation Language (DML)DELETE

GRANTREVOKE Data Control Language(DCL)

DDL - Data Definition Language

1. CreateTo Create a Database.

Syntax :create database databasename;

Eg :create database student;

2. UseSyntax :

use databasename;

Eg :use student;

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));

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;

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);

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;

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;

4. Delete

To delete rows from the table.

Syntax :delete from tablename where [condition];

Eg :delete from tbl_student where int_marks<40;

DCL - Data Controll Language1. Grant

Syntax : grant privilage_type on tablename to

‘user_name'@'localhost';

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

2 . Revoke

Syntax : revoke privilege_type on table_name from

‘user_name'@'localhost';

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

FUNCTIONS

SQL - Functions

SQL functions are used for performing calculations on data.

Types of Functions

Aggregate FunctionsScalar Functions

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()

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

•select count(pk_int_roll_no)as total_students from tbl_student;

total_students4

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;

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;

JOINS

JOINS

To combine rows from two or more tables.

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

a) Left joinb) Right join

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;

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

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;

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

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;

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

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.

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

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: info@baabtra.com

Contact Us