0% found this document useful (0 votes)
32 views16 pages

DDL

The document describes SQL commands for data definition language (DDL) and data manipulation language (DML). It discusses the CREATE, DROP, ALTER, RENAME, and TRUNCATE commands under DDL, which are used to define and modify database schemas and objects. Under DML, it covers the SELECT, INSERT, UPDATE, and DELETE commands, which are used to retrieve and modify data within databases. Examples are provided for each command type.

Uploaded by

Aayush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views16 pages

DDL

The document describes SQL commands for data definition language (DDL) and data manipulation language (DML). It discusses the CREATE, DROP, ALTER, RENAME, and TRUNCATE commands under DDL, which are used to define and modify database schemas and objects. Under DML, it covers the SELECT, INSERT, UPDATE, and DELETE commands, which are used to retrieve and modify data within databases. Examples are provided for each command type.

Uploaded by

Aayush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Table of Contents

About SQL....................................................................................................................................................2
Introduction:.................................................................................................................................................2
SQL Commands:...........................................................................................................................................2
REPORT DISCUSSION:..............................................................................................................................2
Data Definition Language (DDL):............................................................................................................................................................3
1. Create Command:........................................................................................................................................................................3
2. Drop Command:...........................................................................................................................................................................4
3. Alter command:............................................................................................................................................................................5
4. RENAME Command:..................................................................................................................................................................6
5. TRUNCATE Command:..............................................................................................................................................................6
Data Manipulation Language (DML):......................................................................................................................................................8
1. SELECT Command:....................................................................................................................................................................8
2. INSERT Command:.....................................................................................................................................................................9
3. UPDATE Command:..................................................................................................................................................................10
4. DELETE Command:.................................................................................................................................................................10
Aggregate Function:................................................................................................................................................................................11
1. Count():.......................................................................................................................................................................................11
2. Sum ():.........................................................................................................................................................................................11
3. AVG ():.........................................................................................................................................................................................12
4. Min () / Max ():...........................................................................................................................................................................13
SQL Join:..................................................................................................................................................................................................14
1. Inner Join:...................................................................................................................................................................................14
2. Full Join:.....................................................................................................................................................................................14
3. Left Join:.....................................................................................................................................................................................15
4. Right Join:...................................................................................................................................................................................16
About SQL
Introduction:
SQL is a short-form of the structured query language, and it is
pronounced as S-Q-L or sometimes as See-Quell.
Structured query language (SQL) is a programming language for storing and
processing information in a relational database. A relational database stores
information in tabular form, with rows and columns representing different data
attributes and the various relationships between the data values.

SQL Commands:

REPORT SUMMARY:
In this report we describe about Data Definition
Language (DDL) and Data Manipulation Language (DML) commands of SQL.
Data Definition Language (DDL):
The DDL Commands in Structured Query
Language are used to create and modify the schema of the database and its objects.
The syntax of DDL commands is predefined for describing the data. The commands
of Data Definition Language deal with how the data should exist in the database.
Five commands of DDL:
1. Create command
2. Drop command
3. Alter command
4. Truncate command
5. Rename command

1. Create Command:
CREATE is a DDL command used to create databases,
tables, triggers and other database objects.

Syntax of creating database:


CREATE Database Database_Name;

Syntax of creating table:


CREATE Database Table_Name
(
column1 data_type (size of the column),
column2 data_type (size of the column),
columnN data_type (size of the column)
);

Example:
// Create a database table named “employee_tbl” in “Employee” database.
CREATE DATABASE Employee;
CREATE TABLE employee_tbl (
E_ID. Varchar (20),
E_FName Varchar (20),
E_Lname Varchar (20),
Age Int,
Salary Int);

OUTPUT:

2. Drop Command:
DROP is a DDL command used to delete/remove the
database objects from the SQL database. We can easily remove the entire
table, view, or index from the database using this DDL command.

Syntax to drop database:


DROP DATABASE Database_Name;

Syntax to drop table:


DROP TABLE Table_Name;
Example:
// Example to drop Database and Table.
DROP DATABASE employee_details
DROP TABLE employee_details
3. Alter command:
ALTER is a DDL command which changes or modifies the
existing structure of the database, and it also changes the schema of database
objects.
Syntax to add column:
ALTER TABLE tbl_name ADD c_name datatype;
Syntax to drop column:
ALTER TABLE tbl_name DROP Column1, column2, …., column_N;
Syntax to modify column:
ALTER TABLE table_name MODIFY (column_name datatype(size));

Example:
// Add table column in table employee_tlb
ALTER TABLE employee_tbl ADD email Varchar (30);

OUTPUT:

// Drop column from table employee_tlb


ALTER TABLE employee_tbl DROP E_Lname;

OUTPUT:
// Modify column in table employee_tlb
ALTER TABLE employee _tbl MODIFY (E_ID int);
OUTPUT:

4. RENAME Command:
RENAME is a DDL command which is used to change
the name of the database table.

Syntax:
RENAME TABLE Old_Table_Name TO New_Table_Name;

Example:
RENAME TABLE employee_tbl TO Employee_Details;

OUTPUT:

5. TRUNCATE Command:
TRUNCATE is another DDL command which
deletes or removes all the records from the table.
Syntax:
TRUNCATE TABLE Table_Name;
Example:
TRUNCATE TABLE Employee_Details
OUTPUT:
Data Manipulation Language (DML):
The DML commands in Structured
Query Language change the data present in the SQL database. We can easily
access, store, modify, update and delete the existing records from the database
using DML commands.
Following are four commands of DML:
1. SELECT Command
2. INSERT Command
3. UPDATE Command
4. DELETE Command

1. SELECT Command:
SELECT is the most important data manipulation
command in Structured Query Language. The SELECT command shows the
records of the specified table. It also shows the particular record of a particular
column by using the WHERE clause.
Syntax:
SELECT Column_name * FROM table_name;
The * retrieve all the data from table
SELECT Column_name * FROM table_name WHERE condition;
Example:
SELECT * FROM employee_details;
Example of select command with WHERE clause:
If you want to access all the
records of those employees whose Salary is 80 from the above table, then you
have to write the following DML command in SQL:
SELECT * FROM employee_details WHERE Salary = 30000;

2. INSERT Command:
INSERT is another most important data manipulation
command in Structured Query Language, which allows users to insert data in
database tables.
Syntax:
INSERT INTO TABLE_NAME (column1, column2, .... columnN)
VALUES (value1, value2, ... valueN);
Example:
INSERT INTO employee_tbl (E_ID, Name, Salary, Age) VALUES
(104, 'Anmol', 89, 19000),(105, 'Aayush', 40, 40000),(106, 'Pratiksha',
70, 30000);
Output:
3. UPDATE Command:
UPDATE is another most important data manipulation
command in Structured Query Language, which allows users to update or
modify the existing data in database table.

Syntax:
UPDATE Table_name
SET [column1= value1, …., columnN = valueN]
WHERE CONDITION;

Example:
UPDATE employee_details
SET Salary = 40000
WHERE Age = 70;

4. DELETE Command:
This command of Data Manipulation Language does
not delete the stored data permanently from the database. We use the WHERE
clause with the DELETE command to select specific rows from the table.

Syntax:
DELETE FROM Table_Name WHERE condition;

Example:
DELETE FROM employee_details WHERE E_ID = 104;
Aggregate Function:
An aggregate function in SQL performs a calculation on
multiple values and returns a single value. SQL provides many aggregate functions
that include avg, count, sum, min, max, etc. An aggregate function ignores NULL
values when it performs the calculation, except for the count function.
Various types of SQL aggregate functions are:
a. Count ()
b. Sum ()
c. Avg ()
d. Min () / Max ()

1. Count():
COUNT is a SQL aggregate function for counting the number of rows in
a particular column. COUNT is the easiest aggregate function to begin with
because verifying your results is extremely simple.

Syntax:
SELECT COUNT (column_name) FROM table_name;

Example:
SELECT COUNT (Salary) FROM employee_details;

2. Sum ():
SUM is a SQL aggregate function. that totals the values in a given
column. Unlike COUNT, you can only use SUM on columns containing
numerical values.
Syntax:
SELECT SUM (column_name) FROM table_name;
Example:
SELECT SUM (Salary) FROM employee_details;

3. AVG ():
AVG is a SQL aggregate function that calculates the average of a
selected group of values. It's very useful, but has some limitations. First, it can
only be used on numerical columns. Second, it ignores nulls completely.

Syntax:
SELECT AVG (column_name) FROM table_name;
Example:
SELECT AVG (Salary) FROM employee_details;
4. Min () / Max ():
MIN and MAX are SQL aggregation functions that return the
lowest and highest values in a particular column.

They're similar to COUNT in that they can be used on non-numerical


columns. Depending on the column type, MIN will return the lowest number,
earliest date, or non-numerical value as close alphabetically to "A" as
possible. As you might suspect, MAX does the opposite—it returns the
highest number, the latest date, or the non-numerical value closest
alphabetically to "Z."

Syntax:
SELECT MAX (column_name) FROM table_name;
FOR MIN,
SELECT MIN (column_name) FROM table_name;

Example:
SELECT MAX (Salary) FROM employee_details;

For MIN,
SELECT MIN(Salary) FROM employee_details;
SQL Join:
The join keyword is used in SQL statement to query data from two or
more table based on a relationship between certain columns in the tables.
Different types of SQL join:
1. Inner Join
2. Full Join
3. Left Join
4. Right Join

1. Inner Join:
The INNER JOIN keyword selects records that have matching
values in both tables.
Syntax:
SELECT c_name(s)
FROM table1
INNER JOIN table2
ON table1.c_name = table2.c_name;

Example:
SELECT Orders.O_Date, Customers.C_Name
FROM Customers
INNER JOIN Orders ON Customers.C_ID = Orders.C_ID;

OUTPUT:
C_Name O_Date
Alice 2023-01-01
Bob 2023-12-15
Eve 2022-06-10

2. Full Join:
FULL OUTER JOIN and FULL JOIN are the same. The
FULL OUTER JOIN keyword returns all records when there is a match in
left (table1) or right (table2) table records.
Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;

Example:
SELECT Customers.C_Name, Orders.O_ID
FROM Customers
FULL OUTER JOIN Orders ON Customers.C_ID=Orders.C_ID
ORDER BY Customers.C_Name;

OUTPUT:
C_Name O_ID
Alice 101
Bob 102
Eve 103

3. Left Join:
The LEFT JOIN keyword returns all records from the left
table (table1), and the matching records from the right table (table2). The
result is 0 records from the right side, if there is no match.
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;

Example:
SELECT Customers.C_Name, Orders.O_ID
FROM Customers
LEFT JOIN Orders ON Customers.C_ID = Orders.C_ID
ORDER BY Customers.C_Name;
OUTPUT:
C_Name O_ID
Alice 101
Bob 102
Eve 103

4. Right Join:
The RIGHT JOIN keyword returns all records from the right
table (table2), and the matching records from the left table (table1). The
result is 0 records from the left side, if there is no match.

Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Example:
SELECT Customers.C_Name, Orders.O_Date,
FROM Customers
RIGHT JOIN Orders
ON Cutomers.C_ID = Orders.O_ID
ORDER BY Orders.O_ID;

OUTPUT:
C_Name O_Date
Alice 2023-01-01
Bob 2023-12-15
Eve 2022-06-10

You might also like