Professional Documents
Culture Documents
SQL is a database language designed for the retrieval and management of data in a relational
database. ... Five types of SQL queries are
it is considered to be a subset of SQL (Structured Query Language). SQL often uses imperative verbs
with normal English such as sentences to implement database modifications. Hence, DDL does not
show up as a different language in an SQL database, but does define changes in the database
schema.
It is used to establish and modify the structure of objects in a database by dealing with descriptions of
the database schema. Unlike data manipulation language (DML) commands that are used for data
modification purposes, DDL commands are used for altering the database structure such as creating
new tables or objects along with all their attributes (data type, table name, etc.).
Commonly used DDL in SQL querying are CREATE, ALTER, DROP, and TRUNCATE.
Create
This command builds a new table and has a predefined syntax. The CREATE statement syntax is:
For example:
CREATE TABLE Employee (Employee Id INTEGER PRIMARY KEY, First name CHAR (50) NULL,
Last name CHAR (75) NOT NULL);
The mandatory semi-colon at the end of the statement is used to process every command before it. In
this example, the string CHAR is used to specify the data type.
In this example, the string CHAR is used to specify the data type. Other data types can be DATE,
NUMBER, or INTEGER.
Alter
An alter command modifies an existing database table. This command can add up additional column,
drop existing columns and even change the data type of columns involved in a database table.
For example:
In this example, we added a unique primary key to the table to add a constraint and enforce a unique
value. The constraint “employee_pk” is a primary key and is on the Employee table.
Drop
A drop command is used to delete objects such as a table, index or view. A DROP statement cannot be
rolled back, so once an object is destroyed, there’s no way to recover it.
For example:
Truncate
Similar to DROP, the TRUNCATE statement is used to quickly remove all records from a table.
However, unlike DROP that completely destroys a table, TRUNCATE preserves its full structure to be
reused later.
For example:
In this example, we’re marking all the extents of the Employee table for deallocation, so they’re
considered empty for reuse.
Other statements
Other commonly used commands include RENAME and COMMENT. The first one is used with the
ALTER TABLE statement to change the name of an object (table, column, etc.). COMMENT is used to
add single line, multi-line and in-line comments.
A data manipulation language (DML) is a family of computer languages including commands permitting
users to manipulate data in a database. This manipulation involves inserting data into database tables,
retrieving existing data, deleting data from existing tables and modifying existing data. DML is mostly
incorporated in SQL databases. SELECT is the most widely used DML command in SQL.
SELECT: This command is used to retrieve rows from a table. The syntax is
UPDATE: This command modifies data of one or more records. An update command syntax is
INSERT: This command adds one or more records to a database table. The insert command syntax is
DELETE: This command removes one or more records from a table according to specified conditions.
1. GRANT COMMAND
In the above example, user 'ABC' has been given permission to view and modify the records in the
'employee' table.
2. REVOKE COMMAND
REVOKE UPDATE
ON employee
FROM ABC;
GRANT REVOKE
GRANT command allows a user to perform REVOKE command disallows a user to perform
certain activities on the database. certain activities.
It grants access privileges for database objects to It revokes access privileges for database objects
other users. previously granted to other users.
Example: Example:
1. COMMIT COMMAND
2. SAVEPOINT COMMAND
• SAVEPOINT command is used for saving all the current point in the processing of a
transaction.
• It marks and saves the current point in the processing of a transaction.
Syntax:
SAVEPOINT <savepoint_name>
Example:
SAVEPOINT no_update;
• It is used to temporarily save a transaction, so that you can rollback to that point whenever
necessary.
3. ROLLBACK COMMAND
Example:
ROLLBACK TO SAVEPOINT no_update;
4. SET TRANSACTION
• SET TRANSACTION is used for placing a name on a transaction.
Syntax:
SET TRANSACTION [Read Write | Read Only];
ROLLBACK COMMIT
ROLLBACK command is used to undo the The COMMIT command is used to save the modifications
changes made by the DML commands. done to the database values by the DML commands.
It rollbacks all the changes of the current It will make all the changes permanent that cannot be
transaction. rolled back.
Syntax: Syntax:
DELETE FROM table_name ROLLBACK COMMIT;
Data Query Language (DQL) is part of the base grouping of SQL sub-languages. ... DQL
statements are used for performing queries on the data within schema objects. The purpose of DQL
commands is to get the schema relation based on the query passed to it. Although often considered
part of DML, the SQL SELECT statement is strictly speaking an example of DQL. When adding FROM
or WHERE data manipulators to the SELECT statement the statement is then considered part of the
DML.
The SQL Set operation is used to combine the two or more SQL SELECT statements.
1. Union
2. UnionAll
3. Intersect
4. Minus
1. Union
o The SQL Union operation is used to combine the result of two or more SQL SELECT queries.
o In the union operation, all the number of datatype and columns must be same in both the tables
on which UNION operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.
Syntax
Example:
NAME
1 Jack
2 Harry
3 Jackson
The Second table
ID NAME
3 Jackson
4 Stephan
5 David
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
2. Union All
Union All operation is equal to the Union operation. It returns the set without removing duplication and
sorting the data.
Syntax:
ID NAME
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
3. Intersect
o It is used to combine two SELECT statements. The Intersect operation returns the common rows
from both the SELECT statements.
o In the Intersect operation, the number of datatype and columns must be the same.
o It has no duplicates and it arranges the data in ascending order by default.
Syntax
Example:
ID NAME
3 Jackson
4. Minus
o It combines the result of two SELECT statements. Minus operator is used to display the rows
which are present in the first query but absent in the second query.
o It has no duplicates and data arranged in ascending order by default.
Syntax:
Example
ID NAME
1 Jack
2 Harry
A Subquery or Inner query or a Nested query is a query within another SQL query and embedded
within the WHERE clause. A subquery is used to return data that will be used in the main query as a
condition to further restrict the data to be retrieved.
OR
A Subquery or Inner query or a Nested query is a query within another SQL query and embedded within
the WHERE clause.
OR
A subquery is used to return data that will be used in the main query as a condition to further restrict the
data to be retrieved.
Subqueries can be used with the SELECT, INSERT, UPDATE, and DELETE statements along with the
operators like =, <, >, >=, <=, IN, BETWEEN, etc.
Types of SQL Subqueries
• Single Row Subquery. Returns zero or one row in results.
• Multiple Row Subquery. Returns one or more rows in results.
• Multiple Column Subqueries. Returns one or more columns.
• Correlated Subqueries. ...
• Nested Subqueries.
Nested Query –
In Nested Query, Inner query runs first, and only once. Outer query is executed with result from Inner
query.Hence, Inner query is used in execution of Outer query.
Example –
• Orders (OrderID, CustomerID, OrderDate);
1. COUNT FUNCTION
o COUNT function is used to Count the number of rows in a database table. It can work on both
numeric and non-numeric data types.
o COUNT function uses the COUNT(*) that returns the count of all the rows in a specified table.
COUNT(*) considers duplicate and Null.
Syntax
1. COUNT(*)
2. or
3. COUNT( [ALL|DISTINCT] expression )
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Example: COUNT()
Skip Ad
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
Output:
10
1. SELECT COUNT(*)
2. FROM PRODUCT_MAST;
3. WHERE RATE>=20;
Output:
Output:
Output:
Com1 5
Com2 3
Com3 2
Output:
Com1 5
Com2 3
2. SUM Function
Sum function is used to calculate the sum of all selected columns. It works on numeric fields only.
Syntax
1. SUM()
2. or
3. SUM( [ALL|DISTINCT] expression )
Example: SUM()
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST;
Output:
670
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3;
Output:
320
1. SELECT SUM(COST)
2. FROM PRODUCT_MAST
3. WHERE QTY>3
4. GROUP BY COMPANY;
Output:
Com1 150
Com2 170
Output:
Com1 335
Com3 170
3. AVG function
The AVG function is used to calculate the average value of the numeric type. AVG function returns the
average of all non-Null values.
Syntax
1. AVG()
2. or
3. AVG( [ALL|DISTINCT] expression )
Example:
1. SELECT AVG(COST)
2. FROM PRODUCT_MAST;
Output:
67.00
4. MAX Function
MAX function is used to find the maximum value of a certain column. This function determines the largest
value of all selected values of a column.
Syntax
1. MAX()
2. or
3. MAX( [ALL|DISTINCT] expression )
Example:
1. SELECT MAX(RATE)
2. FROM PRODUCT_MAST;
30
5. MIN Function
MIN function is used to find the minimum value of a certain column. This function determines the smallest
value of all selected values of a column.
Syntax
1. MIN()
2. or
3. MIN( [ALL|DISTINCT] expression )
Example:
1. SELECT MIN(RATE)
2. FROM PRODUCT_MAST;
Output:
10
Most Important SQL Commands and Statements
1. Select Statement
2. Update Statement
3. Delete Statement
Let's discuss each statement in short one by one with syntax and one example:
This SQL statement creates the new index in the SQL database table.
1. ON employee_details (First_Name);
This SQL statement deletes the existing index of the SQL database table.
This example deletes the index idx_First_Name from the SQL database.
This SQL statement selects the existing SQL database. Before performing the operations on the
database table, you have to select the database from the multiple existing databases.
1. USE Company;
NULL VALUES
What are null values ? How dbms deals with null values?
The fields can take on null values if they are not declared as not null. We can restrict the insertion of null
vales for the field declaring that field as not null. It means the field cannot take null values. For primary
key constraint, the field which is declared as primary key is also declared as not null. It is done implicitly
in DBMS.