Unit-4 DBMS SQL Notes
Unit-4 DBMS SQL Notes
SQL Command
SQL defines following data languages to manipulate data
of RDBMS.
Creating a Database
To create a database in RDBMS, create command is uses.
Following is the Syntax,
create database database-name;
Creating a Table
create command is also used to create a table. We can
specify names and datatypes of various columns
along.Following is the Syntax,
create table table-name
{
column-name1 datatype1,
column-name2 datatype2,
column-name3 datatype3,
column-name4 datatype4
};
create table command will tell the database system to
create a new table with given table name and column
information.
To Rename a column
Using alter command you can rename an existing column.
Following is the Syntax,
alter table table-name rename column
old-column-name to column-name;
Here is an Example for this,
alter table Student rename column
address to Location;
The above command will rename address column to
Location.
To Drop a Column
alter command is also used to drop columns also.
Following is the Syntax,
alter table table-name drop column
(column-name);
Here is an Example for this,
alter table Student drop
column(address);
The above command will drop address column from the
Student table
3. drop command
drop query completely removes a table from database.
This command will also destroy the table structure.
Following is its Syntax,
drop table table-name
Here is an Example explaining it.
drop table Student;
The above query will delete the Student table completely.
It can also be used on Databases. For Example, to drop a
database,
drop database Test;
The above query will drop a database named Test from
the system.
4.rename query
rename command is used to rename a table. Following is
its Syntax,
rename old-table-name to new-table-name
Here is an Example explaining it.
rename Student to Student-record;
The above query will rename Student table to Student-
record.
DML command
Data Manipulation Language (DML) statements are used
for managing data in database. DML commands are not
auto-committed. It means changes made by DML
command are not permanent to database, it can be rolled
back.
1) INSERT command
Insert command is used to insert data into a table.
Following is its general syntax,
INSERT into table-name
values(data1,data2,..)
Lets see an example,
Consider a table Student with following fields.
S_id S_Name age
INSERT into Student
values(101,'Adam',15);
The above command will insert a record into Student
table.
S_id S_Name age
101 Adam 15
2) UPDATE command
Update command is used to update a row of a table.
Following is its general syntax,
UPDATE table-name set column-name =
value where condition;
Lets see an example,
update Student set age=18 where
s_id=102;
S_id S_Name age
101 Adam 15
102 Alex 18
103 Chris 14
3) Delete command
Delete command is used to delete data from a table.
Delete command can also be used with condition to delete
a particular row. Following is its general syntax,
DELETE from table-name;
_____________________________________________
SELECT Query
Select query is used to retrieve data from a tables. It is the
most used SQL query. We can retrieve complete tables, or
partial by mentioning conditions using WHERE clause.
Wildcard operators
There are two wildcard operators that are used in like
clause.
Percent sign % : represents zero, one or more than
one character.
Underscore sign _ : represents only one character.
Example
SELECT * from Student where s_name like
'_d%';
The above query will return all records from Student
table where s_name contain 'd' as second character.
s_id s_Name age
101 Adam 15
Example
SELECT * from Student where s_name like
'%x';
The above query will return all records from Student
table where s_name contain 'x' as last character.
s_id s_Name age
102 Alex 18
Order By Clause
Order by clause is used with Select statement for
arranging retrieved data in sorted order. The Order by
clause by default sort data in ascending order. To sort data
in descending order DESC keyword is used with Order
by clause.
Syntax of Order By
SELECT column-list|* from table-name
order by asc|desc;
Group By Clause
Group by clause is used to group the results of a SELECT
query based on one or more columns. It is also used with
SQL functions to group the result from one or more
tables.
Syntax for using Group by in a statement.
SELECT column_name,
function(column_name)
FROM table_name
WHERE condition
GROUP BY column_name
Distinct keyword
The distinct keyword is used with Select statement to
retrieve unique values from the table. Distinct removes
all the duplicate records while retrieving from database.
Example
Consider the following Emp table.
eid Name age salary
401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 10000
404 Scott 44 10000
405 Tiger 35 8000
__________________________________
AND & OR operator
AND and OR operators are used with Where clause to
make more precise conditions for fetching data from
database by combining more than one condition together.
AND operator
AND operator is used to set multiple conditions with
Where clause.
Example of AND
Consider the following Emp table
eid name age salary
401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000
OR operator
OR operator is also used to combine multiple conditions
with Where clause. The only difference between AND
and OR is their behaviour. When we use AND to combine
two or more than two conditions, records satisfying all the
condition will be in the result. But in case of OR, atleast
one condition from the conditions specified must be
satisfied by any record to be in the result.
Example of OR
Consider the following Emp table
eid name age salary
401 Anu 22 5000
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000
SELECT * from Emp WHERE salary > 10000
OR age > 25
The above query will return records where either salary is
greater than 10000 or age greater than 25.
402 Shane 29 8000
403 Rohan 34 12000
404 Scott 44 10000
405 Tiger 35 9000
TCL command
Transaction Control Language(TCL) commands are used
to manage transactions in database.These are used to
manage the changes made by DML statements. It also
allows statements to be grouped together into logical
transactions.
Commit command
Commit command is used to permanently save any
transaaction into database.
Following is Commit command's syntax,
commit;
Rollback command
This command restores the database to last commited
state. It is also use with savepoint command to jump to a
savepoint in a transaction.
Following is Rollback command's syntax,
rollback to savepoint-name;
SQL Functions
SQL provides many built-in functions to perform
operations on data. These functions are useful while
performing mathematical calculations, string
concatenations, sub-strings etc. SQL functions are divided
into two catagories,
Aggregrate Functions
Scalar Functions
Aggregrate Functions
These functions return a single value after calculating
from a group of values.Following are some frequently
used Aggregrate functions.
1) AVG()
Average returns average value after calculating from
values in a numeric column.
Its general Syntax is,
SELECT AVG(column_name) from table_name
2) COUNT()
Count returns the number of rows present in the table
either based on some condition or without condition.
Its general Syntax is,
SELECT COUNT(column_name) from table-
name
Example of COUNT(distinct)
Consider following Emp table
eid name age salary
401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000
SQL query is,
SELECT COUNT(distinct salary) from emp;
Result of the above query will be,
count(distinct salary)
4
_______________________________________________
_______________
3. MAX()
MAX function returns maximum value from selected
column of the table.
Syntax of MAX function is,
SELECT MAX(column_name) from table-name
Example of MAX()
Consider following Emp table
eid name age salary
401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000
SQL query to find Maximum salary is,
SELECT MAX(salary) from emp;
Result of the above query will be,
MAX(salary)
10000
4) MIN()
MIN function returns minimum value from a selected
column of the table.
Syntax for MIN function is,
SELECT MIN(column_name) from table-name
Example of MIN()
Consider following Emp table,
eid name age salary
401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000
SQL query to find minimum salary is,
SELECT MIN(salary) from emp;
Result will be,
MIN(salary)
6000
5) SUM()
SUM function returns total sum of a selected columns
numeric values.
Syntax for SUM is,
SELECT SUM(column_name) from table-name
Example of SUM()
Consider following Emp table
eid name age salary
401 Anu 22 9000
402 Shane 29 8000
403 Rohan 34 6000
404 Scott 44 10000
405 Tiger 35 8000
SQL query to find sum of salaries will be,
SELECT SUM(salary) from emp;
Result of above query is,
SUM(salary)
41000
Join in SQL
SQL Join is used to fetch data from two or more tables,
which is joined to appear as single set of data. SQL Join is
used for combining column from two or more tables by
using values common to both tables. Join Keyword is
used in SQL queries for joining two or more tables.
Minimum required condition for joining table, is (n-1)
where n, is number of tables. A table can also join to itself
known as, Self Join.
Types of Join
The following are the types of JOIN that we can use in
SQL.
Inner
Outer
Left
Right
ID NAME
1 abhi
2 adam
4 alex
The class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
Cross JOIN query will be,
SELECT *
from class,
cross JOIN class_info;
The result table will look like,
ID NAME ID Address
1 abhi 1 DELHI
2 adam 1 DELHI
4 alex 1 DELHI
1 abhi 2 MUMBAI
2 adam 2 MUMBAI
4 alex 2 MUMBAI
1 abhi 3 CHENNAI
2 adam 3 CHENNAI
4 alex 3 CHENNAI
Outer JOIN
Outer Join is based on both matched and unmatched data.
Outer Joins subdivide further into,
Left Outer Join
Right Outer Join
Full Outer Join
Left Outer Join
The left outer join returns a result table with the matched
data of two tables then remaining rows of the left table
and null for the right table's column.
Left Outer Join syntax is,
SELECT column-name-list
from table-name1
LEFT OUTER JOIN
table-name2
on table-name1.column-name = table-
name2.column-name;
Left outer Join Syntax for Oracle is,
select column-name-list
from table-name1,
table-name2
on table-name1.column-name = table-
name2.column-name(+);