Yasir musthafa pp
yasirmusthafapp@gmail.com
www.facebook.com/yasirmusthafa
twitter.com/yasirmusthafapp
in.linkedin.com/in/...
Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
docu...
Sql statements
• Query or command to perform a task in a
database
3 type
1. DDL
2. DML
3. DCL
DDL
Data definition language
• Create
• Alter
• Drop
• truncate
Create
Syntax in mysql
CREATE TABLE table_name
(
column_name1 data_type(size),
column_name2 data_type(size),
....
);Exampl...
Alter
Syntax in mysql
Alter table table_name modify column
column_name data_type;
Example
alter table tbl_stock modify col...
Drop
Syntax in mysql
Drop table table_name;
Example
Drop table tbl_student;
Truncate
Syntax in mysql
Truncate table table_name;
Example
Truncate table tbl_student;
DML
Data manipulation language
• Select
• Insert
• Update
• delete
Select
Syntax in mysql
Select * from table_name;
Example
Select * from tbl_stock;
Insert
Syntax in mysql
insert into table_name values
( );
Example
insert into tbl_stock values
(NULL,"mouse",10,500,1);
Update
Syntax in mysql
Update table_name set column_name=
Example
• update tbl_stock set int_price=int_price+1.50;
delete
Syntax in mysql
Delete from table_name;
Example
Delete from tbl_stock;
Difference between delete and truncate??
DCL
Data control language
• Grant
• Revoke
• Commit
• rollback
Grant
Syntax in mysql
GRANT privilege_type ON table_name TO
‘user_name'@'localhost';
Example
GRANT select ON tbl_supplier ...
Revoke
Syntax in mysql
REVOKE privilege_type ON table_name FROM
‘user_name'@'localhost';
Example
REVOKE select ON tbl_supp...
functions
• Built in function-perform calculation on data
1. Aggregate function
2. Scalar function
Aggregate function
• Return a single value for all the values in the
column after calculation
AVG()
Mysql syntax
Select av...
Group by
• To group the result set by one or more column
• Often used in conjunction with aggregate
function
Syntax in mys...
Scalar function
• Return single value for each value in a column
UCASE()
Syntax in mysql
Select UCASE (column_name) from t...
Joins
• Used to combine rows from two or more
tables
Different joins
• inner join
• Left join
• Right join
Inner join
• Returns all rows when there is at least one
match in BOTH tables
Syntax in mysql
Select column_name(s) from t...
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 CC300 5
3 EC10...
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 MEC...
Left join
• returns all rows from the left table (table1), with the matching
rows in the right table (table2).
• Syntax in...
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 CC10...
The result will be
Vchr_dept_na
me
Vchr_class_na
me
Pk_int_dept_i
d
int_dept_id
CS CS100 1 1
EC EC100 2 2
EE NULL 3 NULL
M...
Right join
• Return all rows from the right table, and the matched rows from the left
table
Syntax in mysql
SELECT column_...
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 ME...
The result will be
Vchr_dept_na
me
Vchr_class_na
me
Pk_int_dept_i
d
int_dept_id
CS CS100 1 1
EC EC100 2 2
MECH MECH100 4 4...
Thank you
Want to learn more about programming or Looking to become a good programmer?
Are you wasting time on searching so many con...
Follow us @ twitter.com/baabtra
Like us @ facebook.com/baabtra
Subscribe to us @ youtube.com/baabtra
Become a follower @ s...
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, Near Bus Sta...
Sql statement,functions and joins
Upcoming SlideShare
Loading in...5
×

Sql statement,functions and joins

378

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
378
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Sql statement,functions and joins"

  1. 1. Yasir musthafa pp yasirmusthafapp@gmail.com www.facebook.com/yasirmusthafa twitter.com/yasirmusthafapp in.linkedin.com/in/yasirmusthafapp 8891396749 Sql statements ,functions and joins
  2. 2. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  3. 3. Sql statements • Query or command to perform a task in a database 3 type 1. DDL 2. DML 3. DCL
  4. 4. DDL Data definition language • Create • Alter • Drop • truncate
  5. 5. Create Syntax in mysql CREATE 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) );
  6. 6. Alter Syntax in mysql Alter table table_name modify column column_name data_type; Example alter table tbl_stock modify column int_price float;
  7. 7. Drop Syntax in mysql Drop table table_name; Example Drop table tbl_student;
  8. 8. Truncate Syntax in mysql Truncate table table_name; Example Truncate table tbl_student;
  9. 9. DML Data manipulation language • Select • Insert • Update • delete
  10. 10. Select Syntax in mysql Select * from table_name; Example Select * from tbl_stock;
  11. 11. Insert Syntax in mysql insert into table_name values ( ); Example insert into tbl_stock values (NULL,"mouse",10,500,1);
  12. 12. Update Syntax in mysql Update table_name set column_name= Example • update tbl_stock set int_price=int_price+1.50;
  13. 13. delete Syntax in mysql Delete from table_name; Example Delete from tbl_stock; Difference between delete and truncate??
  14. 14. DCL Data control language • Grant • Revoke • Commit • rollback
  15. 15. Grant Syntax in mysql GRANT privilege_type ON table_name TO ‘user_name'@'localhost'; Example GRANT select ON tbl_supplier TO 'john'@'localhost';
  16. 16. Revoke Syntax in mysql REVOKE privilege_type ON table_name FROM ‘user_name'@'localhost'; Example REVOKE select ON tbl_supplier FROM 'suhail'@'localhost';
  17. 17. functions • Built in function-perform calculation on data 1. Aggregate function 2. Scalar function
  18. 18. Aggregate function • Return a single value for all the values in the column after calculation AVG() Mysql syntax Select avg(coulmn_name) from table_name; Example Select avg(int_salary) from employee; COUNT(),FIRST(),LAST(),MAX(),MIN(),SUM()
  19. 19. Group by • To group the result set by one or more column • Often used in conjunction with aggregate function Syntax in mysql SELECT column_name, aggregate_function(column_name) FROM table_name GROUP BY column_name; Example Select vchr_city,sum(mark) from tbl_student group by vchr_city;
  20. 20. Scalar function • Return single value for each value in a column UCASE() Syntax in mysql Select UCASE (column_name) from table_name; Example Select UCASE(first_name) from tbl_student; • LCASE(),MID(),LEN(),ROUND(),NOW(),FORMAT()
  21. 21. Joins • Used to combine rows from two or more tables Different joins • inner join • Left join • Right join
  22. 22. Inner join • Returns all rows when there is at least one match in BOTH tables Syntax in mysql Select column_name(s) from table1 join table2 on table1.column_name=table2.column_name;
  23. 23. 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 CC300 5 3 EC100 2 4 MECH100 4 Example select 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;
  24. 24. 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
  25. 25. Left join • returns all rows from the left table (table1), with the matching rows in the right table (table2). • Syntax in mysql SELECT column_name(s) FROM table1 LEFT JOIN table2 ON table1.column_name=table2.column_name;
  26. 26. 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;
  27. 27. The result will be Vchr_dept_na me Vchr_class_na me Pk_int_dept_i d int_dept_id CS CS100 1 1 EC EC100 2 2 EE NULL 3 NULL MECH MECH100 4 4
  28. 28. Right join • Return all rows from the right table, and the matched rows from the left table Syntax in mysql SELECT column_name(s) FROM table1 RIGHT JOIN table2 ON table1.column_name=table2.column_name;
  29. 29. 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;
  30. 30. The result will be Vchr_dept_na me Vchr_class_na me Pk_int_dept_i d int_dept_id CS CS100 1 1 EC EC100 2 2 MECH MECH100 4 4 NULL CC100 NULL 5
  31. 31. Thank you
  32. 32. 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.
  33. 33. 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
  34. 34. Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Cafit Square, Hilite Business Park, Near Pantheerankavu, Kozhikode Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com Contact Us
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×