0% found this document useful (0 votes)
10 views11 pages

DDL (Data Definition Language)

The document provides an overview of SQL commands, focusing on Data Definition Language (DDL) and Data Manipulation Language (DML). It explains how to create, alter, and drop tables, as well as how to insert, update, and delete records within those tables. Additionally, it covers logical operators and the GROUP BY clause for organizing query results.

Uploaded by

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

DDL (Data Definition Language)

The document provides an overview of SQL commands, focusing on Data Definition Language (DDL) and Data Manipulation Language (DML). It explains how to create, alter, and drop tables, as well as how to insert, update, and delete records within those tables. Additionally, it covers logical operators and the GROUP BY clause for organizing query results.

Uploaded by

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

https://www.programiz.

com/sql/online-compiler/

https://www.geeksforgeeks.org/sql-ddl-dql-dml-dcl-tcl-commands/

DDL (Data Definition Language)

SQL CREATE TABLE Statement

A Table is a combination of rows and columns. For creating a table we have to define the structure of
a table by adding names to columns and providing data type and size of data to be stored in columns.

CREATE table table_name

Column1 datatype (size),

column2 datatype (size),

columnN datatype(size)

);

EX- create table student(

Roll_No int(2),

Name varchar(20)

);

ALTER TABLE ADD Column Statement in SQL

ADD is used to add columns to the existing table. Sometimes we may require to add additional
information, in that case, we do not require to create the whole database again, ADD comes to our
rescue.

ALTER TABLE ADD Column Statement Syntax:

ALTER TABLE table_name ADD (Columnname_1 datatype,


Columnname_2 datatype, …Columnname_n datatype);

EX- Alter table student add phone_num int(10);

DROP

DROP is used to delete a whole database or just a table.

In this article, we will be learning about the DROP statement which destroys objects like an existing
database, table, index, or view. A DROP statement in SQL removes a component from a relational
database management system (RDBMS).

DROP TABLE table_name;

Drop table student;

DML (Data Manipulation Language)

Insert Data into Table

To add data to the table, we use INSERT INTO, the syntax is as shown below:

//Below query adds data in specific column, (like Column1=Value1)//

Insert into Table_name(Column1, Column2, Column3)

Values (Value1, value2, value3);

//Below query adds data in table in sequence of column name(Value1 will be

added in Column1 and so on)//

Insert into Table_name

Values (Value1, value2, value3);


//Adding multiple data in the table in one go//

Insert into Table_name

Values (Value01, value02, value03),

(Value11, value12, value13),

(Value21, value22, value23),

(ValueN1, valueN2, valueN3)

EX- Insert into student Values(1, ‘Akash’);

Insert into student Values(2, ‘Ram’),(3, ‘Saroj’);

SQL | UPDATE Statement

The UPDATE statement in SQL is used to update the data of an existing table in the database. We can
update single columns as well as multiple columns using the UPDATE statement as per our
requirement.

In a very simple way, we can say that SQL commands(UPDATE and DELETE) are used to change the
data that is already in the database. The SQL DELETE command uses a WHERE clause.

Syntax

UPDATE table_name SET column1 = value1, column2 = value2,…

WHERE condition;

table_name: name of the table

column1: name of first , second, third column….

value1: new value for first, second, third column….

condition: condition to select the rows for which the

values of columns needs to be updated.

EX- UPDATE student set phone_no = 11111111 where roll_no = 1;

UPDATE student set phone_no = 11111111;

SQL | DELETE Statement

Existing records in a table can be deleted using the SQL DELETE Statement. We can delete a single
record or multiple records depending on the condition we specify in the WHERE clause.
DELETE FROM table_name WHERE some_condition;

EX- Delete from student where phone_no=1111111

EXP:2
Write SQL queries using logical operations and operators

Types of Logical Operators in SQL


Given below is the list of logical operators available in SQL.
AND Operator
The AND operator is used to combines two or more conditions but if it is true when all the
conditions are satisfied.
Query
SELECT * FROM employee WHERE emp_city = 'Allahabad' AND emp_country = 'India';

IN Operator
It is used to remove the multiple OR conditions in SELECT, INSERT, UPDATE, or DELETE. and We
can also use NOT IN to minimize the rows in your list and any kind of duplicate entry will be
retained.
Query
SELECT * FROM employee WHERE emp_city IN ('Allahabad', 'Patna');
LIKE Operator
In SQL, the LIKE operator is used in the WHERE clause to search for a specified pattern in a
column.
• % – It is used for zero or more than one character.
• _ – It is used for only one character means fixed length.
Query
SELECT * FROM employee WHERE emp_city LIKE 'P%';

NOT Operator
Query
SELECT * FROM employee WHERE emp_city NOT LIKE 'A%';

Note: A% means anything which starts with A. Above query means where City does not starts
with A.

OR Operator
The OR operator is used to combines two or more conditions but if it is true when one of the
conditions are satisfied.
Query
SELECT * FROM employee WHERE emp_city = 'Varanasi' OR emp_country = 'India';
BETWEEN Operator
The SQL BETWEEN condition allows you to easily test if an expression is within a range of
values (inclusive).
Query
SELECT * FROM employee WHERE emp_id BETWEEN 101 AND 104;

ALL Operator
The ALL operator returns TRUE if all of the subqueries values matches the condition.
All operator is used with SELECT, WHERE, HAVING statement.
Query
SELECT * FROM employee WHERE emp_id = ALL
(SELECT emp_id FROM employee WHERE emp_city = 'Varanasi');
ANY Operator
The ANY operator:
• It returns a boolean value as a result
• It returns TRUE if ANY of the subquery values match the condition
Query
SELECT * FROM employee WHERE emp_id = ANY
(SELECT emp_id FROM employee WHERE emp_city = 'Varanasi');

EXISTS Operator
In SQL, Exists operator is used to check whether the result of a correlated nested query is
empty or not.
Exists operator is used with SELECT, UPDATE, INSERT or DELETE statement.
Query
SELECT emp_name FROM employee WHERE EXISTS
(SELECT emp_id FROM employee WHERE emp_city = 'Patna');
SOME Operator
In SQL, SOME operators are issued with comparison operators (<,>,=,<=, etc) to compare the
value with the result of a subquery.
Query
SELECT * FROM employee WHERE emp_id < SOME
(SELECT emp_id FROM employee WHERE emp_city = 'Patna');

……………………………………………………………………………………………………………………………………………

Exp 3: SQL queried using GROUP BY functions

In SQL, the GROUP BY clause is used to group rows by one or more columns.
Example

-- select the item column and the count of order ids from the Orders table
-- group them by the item column

SELECT COUNT(order_id), item


FROM Orders
GROUP BY item;
Example:
Step 1: Create a table and insert content into the table
-- count the number of each country and group the rows by country
SELECT country, COUNT(*) AS number
FROM Customers
GROUP BY country;

Example 2.
-- select customer_id and sum of amount from Orders
-- group the result by customer_id

SELECT customer_id, SUM(amount) AS total


FROM Orders
GROUP BY customer_id;

You might also like