DDL Commands of SQL
DDL Commands of SQL
1. Create Table
This statement is used to create the structure of a table.
We can specify the various fields along wih their data types, size
Syntax:
Create table <table_name>
( Column1 <data_type>(size) constraint,
Column2 <data_type>(size) constraint,
………………………………………
………………………………………
);
data type.
particular field.
Example:
We have to create a table named Student with following
structure:
Primary
roll number 3
Key
name varchar 20
dateofbirth date
Class varchar 20
2. Alter Table
This statement is used to modify the structure of a table. We can
We can also specify the data type, size, constraints and domain of
We can increase the size of any field at any time but we should be
loss.
Syntax 1:
be added to table.
column.
data type.
particular field.
Example 1:
student.
Example 2:
student table.
Syntax 2:
Example
25.
Syntax 3:
Alter table
<table_name>
drop column
Column_name ,
……………………………………
…
……………………………………
…
);
deleted.
Example:
Above command will the delete the field address from the table
student permanently.
3. Drop Table
This statement is used to delete the structure of a table as well as
Syntax:
1. Insert
This command used to insert a new record in an existing table.
Syntax:
inserted
into field1,field2,…….
Example 1:
student.
Example 2:
2. update
This command is used to modify an exiting record . We can modify
more than one field at a time and we can also modify those
Syntax: –
Update <table_name>
set column_name1= expression1 ,
column_name2= expression2 , ……..
where <condition >;
columnname1,columnname2.
modified.
Example 1:
Above statement ill make all the values in roll field to be 106.
Example 2:
Update student set fees=fees+100;
Example 3:
where fees>4500.
3. Delete
This command is used to delete all the records or an exiting record
Syntax:
Delete
from<table_name>
where <condition >;
Example 1
Above command will delete all the records from table named
student.
Example 2
Syntax:
* represents that all the records from the table should be shown
on the screen.
are to be shown.
Example 1
Above command will show all the records from table named
student.
Example 2
Above command will show contents of fields named roll and name
for those records from table named student which have fees more
than 4000.
Example 3
a particular field.
Example
select distinct class from emp;
2. IN Clause
This clause is used to specify a range of values. This clause is used
Example
Select * from student where class in (‘xi’,’xii’);
OR
3. BETWEEN Clause
The BETWEEN clause allows to check if an expression is within a
range of two values. The values can be text, date, or numbers. The
OR
shown where value of fees is between 1000 and 2000. Both values
4. LIKE Clause
The LIKE clause is used in a WHERE clause to search for a specified
pattern in a column.
WHERE Name LIKE ‘A%’ Finds values that start with “A”
WHERE Name LIKE ‘%s’ Finds values that end with “s”
student where name field contains values starting with A for e.g.
This command will show all those records from table student where
VARUN, KARUNA.
This command will show all those records from table student where
This command will show all those records from table student where
name field contains values starting from A and ending with N and
5. IS NULL Clause
NULL means empty. It is neither Zero nor blank but it means “not
Example
i.. select * from student where fees is null;
This command will show all the records from student table where
6. ORDER BY Clause
This clause is basically used to sort the records as per a particular
Example
i. select * from student order by name;
OR
This command will show all the records from student table in ascending order of field
“name”.
This command will show all the records from student table in descending order of field
“name”.
7. GROUP BY Clause
This clause can be used with select statement. This clause is used
Example:
i. Select class, sum(fees) from student group by job ;
This command will show the classes along with sum of fees
count(class)>3;
This command will show the classes along with sum of fees
SQL Functions
i. SUM
This function is used to find the sum of values stored in a numeric
field of a table.
Syntax:
SUM(Field_Name)
Example:
select sum(fees) from student;
This command will compute the sum of values stored in fees field
of student table.
ii AVG:
This function is used to find the average of values stored in a
Syntax:
AVG(Field_Name)
Example:
select avg(fees) from student;
iii. MIN
This function is used to find the minimum value among the values
Syntax:
MIN(Field_Name)
Example:
select MIN(fees) from student;
iv. MAX
This function is used to find the maximum value among the values
Syntax:
MAX(Field_Name)
Field_Name is the name of any numeric field on which we want to apply MIN Function.
Example:
select MAX(fees) from student;
v. COUNT
This function is used to count the number of records in a particular
Syntax
count(Expression/*)
Expression may be the name of any field of table or any expression based on a field of a
table.
Example
i. select count(fees) from student;
This command will show the number of non null values stored in fees field of student table.
This command will show the total number of records stored in student table.
SQL Joins
It is the way to combine records of more than one table in which
I. EQUI JOIN
This type of join is used to combine records from tables where the
common field.
Example:
Table: Foods
+---------+--------------+-----------+
+---------+--------------+-----------+
| 1 | Chex Mix | 16 |
| 6 | Cheez-It | 15 |
| 2 | BN Biscuit | 15 |
| 4 | Pot Rice | 15 |
+---------+--------------+-----------+
Table : Company
+------------+---------------+
| COMPANY_ID | COMPANY_NAME |
+------------+---------------+
| 15 | Jack Hill |
| 16 | Akas Foods |
| 17 | Foodies. |
| 19 | sip-n-Bite. |
+------------+---------------+
SELECT FOODS.ITEM_ID,FOOD.ITEM_NAME,
COMPANY.COMPANY_ID,COMPANY.COMPANY_NAME
FROM FOODS,COMPANY
WHERE FOODS.COMPANY_ID=COMPANY.COMPANY_ID;
OUTPUT
+---------+--------------+-----------+--------------+
+---------+--------------+-----------+--------------+
+---------+--------------+-----------+--------------+
Output will show only those records from both the tables where
company_id is same.
ii. NATURAL JOIN
A natural join is a type of equi join which occurs implicitly by
comparing all the same names columns in both tables. The join
result has only one column for each pair of equally named
columns.
+---------+--------------+-----------+
| 1 | Chex Mix | 16 |
| 6 | Cheez-It | 15 |
| 2 | BN Biscuit | 15 |
| 4 | Pot Rice | 15 |
+---------+--------------+-----------+
Table : Company
+------------+---------------+
| COMPANY_ID | COMPANY_NAME |
+------------+---------------+
| 15 | Jack Hill |
| 16 | Akas Foods |
| 17 | Foodies. |
| 19 | sip-n-Bite. |
+------------+---------------+
SELECT *
FROM foods NATURAL JOIN company;
OUTPUT
+---------+--------------+-----------+--------------+
+---------+--------------+-----------+--------------+
+---------+--------------+-----------+--------------+
Output will show only those records from both the tables where
company_id is same.