BMS Institute of Technology and Management: Department of Master of Computer Applications
BMS Institute of Technology and Management: Department of Master of Computer Applications
(Affiliated to VTU)
Post Box No. 6443, Doddaballapura Main Road, Yelahanka, Bengaluru – 560 064.
SQL – Structured Query Language – provides an interface to the relational database system. It is
developed by IBM in 1970s.
Components of SQL
1. DDL: Data Definition Language – is a set of SQL commands used to create, modify, delete the
database structure but not the data.
DDL Commands
1. Create – Creates the objects in the database
2. Alter – Alters the structure of the database
3. Drop – Deletes the objects from the database
4. Truncate – Remove all the records from the table including all the spaces allocated for record.
5. Grant – Give the users access privileges to the database.
6. Revoke – Withdraw the access privileges
2. DML: Data Manipulation Language is a set of SQL commands that allows changing the data within
the database.
DML Commands
1. Insert – Insert data into the table
2. Update – Update the existing data within the table
3. Delete – Delete all the records from the table, the space allocated remains
DCL Commands
1. Commit – Save the work done
2. Savepoint – Identify the point in a transaction
3. Rollback – Restore the database to the original position since the last commit
Creation of a table
Syntax: create table <tablename> (<col-name1><datatype>(size), <col-name2><datatype>(size));
Select command
1. To select all columns and selected rows
select * from <tablename> where <condition>;
2. To select all rows and selected columns
select <colname1, colname2,…> from <tablename>;
3. To select all rows and all columns
select * from <tablename>;
Logical Operators
1. AND – select <col-name1, col-name2> from <tablename> where <cond1> and <cond2>;
2. OR – select <col-name1, col-name2> from <tablename> where <cond1> or <cond2>;
3. NOT – select * from <tablename> where not (cond1 or cond2);
Range searching
1. Between – select <colname1, colname2> from <tablename> where <colname1> between ‘expr1’
and ‘expr2’;
2. select <colname1, colname2> from <tablename> where <colname1> not between ‘expr1’ and
‘expr2’;
Pattern matching
The like predicate allows for comparison of one string value with the other string value, which is not
identical. This is achieved by using wild card characters % and _
% sign – select * from <tablename> where <colname> like ‘2%’; -------------------------Example
_ sign – select * from <tablename> where color like ‘_ _ _ e%; ---------------------------Example
Aggregate functions
1. avg(n) – select avg (<colname>) from <tablename>;
2. min(n) - select min (<colname>) from <tablename>;
3. max(n) - select max (<colname>) from <tablename>;
4. count(expr) – select count (<colname>) from <tablename>;
5. sum(expr) - select sum (<colname>) from <tablename>;
Numeric functions
1. abs(n) – select abs(-15) from dual;
2. power(m,n) – select power(4, 4) as power from dual;
3. round (n,[m]) – select round(23.765) from dual;
4. sqrt(n) – select sqrt(36) from dual;
String functions
1. lower(char) – select lower(‘XYZ’) from dual;
2. initcap(char) – select initcap(‘lab’) from dual;
3. upper(char) – select upper(‘dbms’) from dual;
4. length(char) – select length(‘dbms’) from dual;
5. substr(char,m,[n]) – select substr(‘woman’, 3,5) from dual;
6. ltrim(char, [set]) – select ltrim (‘bread’, ‘b’) from dual;