DDL (Data Definition Language)
DDL (Data Definition Language)
com/sql/online-compiler/
https://www.geeksforgeeks.org/sql-ddl-dql-dml-dcl-tcl-commands/
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.
columnN datatype(size)
);
Roll_No int(2),
Name varchar(20)
);
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.
DROP
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).
To add data to the table, we use INSERT INTO, the syntax is as shown below:
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
WHERE condition;
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;
EXP:2
Write SQL queries using logical operations and operators
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');
……………………………………………………………………………………………………………………………………………
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
Example 2.
-- select customer_id and sum of amount from Orders
-- group the result by customer_id