Basic SQL COMMANDS
MUHAMMED MASHAHIL P
ASSISTANT PROFESSOR
CAS VAZHAKKAD
Sqlstatements
• Query or command to perform atask ina
database
4 type
1. DDL
2. DML
3. DCL
4. TCL
DDL
Data definitionlanguage
• Create
• Alter
• Drop
• truncate
Create
Syntax in mysql
CREATETABLEtable_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)
);
Alter
Syntax inmysql
Alter table table_name modifycolumn
column_name data_type;
Example
alter table tbl_stock modify columnint_price
float;
Drop
Syntax inmysql
Drop table table_name;
Example
Drop table tbl_student;
Truncate
Syntax inmysql
Truncate table table_name;
Example
Truncate table tbl_student;
DML
Data manipulationlanguage
• Select
• Insert
• Update
• delete
Select
Syntax inmysql
Select * from table_name;
Example
Select * from tbl_stock;
Insert
Syntax inmysql
insert into table_namevalues
( );
Example
insert into tbl_stockvalues
(NULL,"mouse",10,500,1);
Update
Syntax inmysql
Update table_name set column_name=
Example
• update tbl_stock set int_price=int_price+1.50;
delete
Syntax inmysql
Delete from table_name;
Example
Delete from tbl_stock;
Difference between delete andtruncate??
DCL
Data control language
• Grant
• Revoke
Grant
Syntax inmysql
GRANTprivilege_type ONtable_name TO
‘user_name'@'localhost';
Example
GRANTselect ONtbl_supplier TO
'john'@'localhost';
Revoke
Syntax inmysql
REVOKEprivilege_type ONtable_name FROM
‘user_name'@'localhost';
Example
REVOKEselect ONtbl_supplier FROM
'suhail'@'localhost';
TCL
TransactionControl language
• Commit
• rollback
functions
• Built in function-perform calculation ondata
1. Aggregate function
2. Scalarfunction
Aggregatefunction
• Return asingle value for all the values inthe
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()
Groupby
• Togroup the result set by one or morecolumn
• Often used in conjunction with aggregate
function
Syntax in mysql
SELECTcolumn_name, aggregate_function(column_name)
FROMtable_name
GROUPBYcolumn_name;
Example
Select vchr_city,sum(mark) from tbl_student group byvchr_city;
Scalarfunction
• Return single value for eachvalue in acolumn
UCASE()
Syntax in mysql
Select UCASE(column_name) fromtable_name;
Example
Select UCASE(first_name) from tbl_student;
• LCASE(),MID(),LEN(),ROUND(),NOW(),FORMAT()
Joins
• Usedto combine rows from two or more
tables
Different joins
• inner join
• Left join
• Right join
Innerjoin
• Returns all rows when there is at leastone
match in BOTHtables
Syntax in mysql
Select column_name(s) from table1 join table2on
table1.column_name=table2.column_name;
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 ontbl_dept.pk_int_dept_id=tbl_classes.int_dept_id;
Theresult setwill be
Vchr_departmen
t_ name
Vchr_class_name Pk_int_dept_id int_dept_id
CS CS100 1 1
EC EC100 2 2
MECH MECH100 4 4
Leftjoin
• returns all rows from the left table (table1), with the matching
rows in the right table(table2).
• Syntax inmysql
SELECTcolumn_name(s)
FROMtable1
LEFTJOINtable2
ONtable1.column_name=table2.column_name;
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 LEFTjoin tbl_classes on
tbl_dept.pk_int_dept_id=tbl_classes.int_dept_id;
Theresult 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
Rightjoin
• Return all rows from the right table, and the matched rows from the left
table
Syntax in mysql
SELECTcolumn_name(s)
FROMtable1
RIGHTJOINtable2
ONtable1.column_name=table2.column_name;
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 RIGHTjoin tbl_classes on
tbl_dept.pk_int_dept_id=tbl_classes.int_dept_id;
Theresult 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
Thankyou

Basic sql Commands