0% found this document useful (0 votes)
37 views

Basic SQL Commands

DML,DCL,DDL,TCL COMMANDS FUNCTIONS Aggregate function Scalar function INNER OR OUTER JOINS(LEFT,RIGHT)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Basic SQL Commands

DML,DCL,DDL,TCL COMMANDS FUNCTIONS Aggregate function Scalar function INNER OR OUTER JOINS(LEFT,RIGHT)
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Basic SQL COMMANDS

MUHAMMED MASHAHIL P
ASSISTANT PROFESSOR
CAS VAZHAKKAD
Sqlstatements

• Query or command to perform a task ina


database
4 type
1. DDL
2. DML
3. DCL
4. TCL
DDL

Data definitionlanguage
• Create
• Alter
• Drop
• truncate
Create
Syntax in mysql
CREATETABLE 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)
);
Alter

Syntax in mysql
Alter table table_name modify column
column_name data_type;
Example
alter table tbl_stock modify columnint_price
float;
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 manipulationlanguage
• Select
• Insert
• Update
• delete
Select

Syntax in mysql
Select * from table_name;
Example
Select * from tbl_stock;
Insert

Syntax in mysql
insert into table_namevalues
( );
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
Grant

Syntax in mysql
GRANTprivilege_type ON table_name TO
‘user_name'@'localhost';
Example
GRANTselect ON tbl_supplier TO
'john'@'localhost';
Revoke

Syntax in mysql
REVOKEprivilege_type ON table_name FROM
‘user_name'@'localhost';
Example
REVOKEselect ON tbl_supplier FROM
'suhail'@'localhost';
TCL

Transaction Control language


• Commit
• rollback
functions

• Built in function-perform calculation ondata


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 avg(coulmn_name) from table_name;
Example
Select avg(int_salary) from employee;

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

• To group the result set by one or more column


• Often used in conjunction with aggregate
function
Syntax in mysql
SELECTcolumn_name, aggregate_function(column_name)
FROM table_name
GROUPBYcolumn_name;
Example
Select vchr_city,sum(mark) from tbl_student group byvchr_city;
Scalarfunction

• Return single value for each value in acolumn


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()
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 leastone
match in BOTHtables
Syntax in mysql
Select column_name(s) from table1 join table2 on
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 on tbl_dept.pk_int_dept_id=tbl_classes.int_dept_id;
The result set will be

Vchr_departmen Vchr_class_name Pk_int_dept_id int_dept_id


t_ name
CS CS100 1 1
EC EC100 2 2
MECH MECH100 4 4
Left join
• returns all rows from the left table (table1), with the matching
rows in the right table(table2).
• Syntax in mysql
SELECTcolumn_name(s)
FROM table1
LEFTJOINtable2
ON 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 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;
The result will be

Vchr_dept_na Vchr_class_na Pk_int_dept_i int_dept_id


me me d
CS CS100 1 1
EC EC100 2 2
EE NULL 3 NULL
MECH MECH100 4 4
Right join
• Return all rows from the right table, and the matched rows from the left
table
Syntax in mysql
SELECTcolumn_name(s)
FROM table1
RIGHTJOINtable2
ON 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_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;
The result will be

Vchr_dept_na Vchr_class_na Pk_int_dept_i int_dept_id


me me d
CS CS100 1 1
EC EC100 2 2
MECH MECH100 4 4
NULL CC100 NULL 5
Thankyou

You might also like