SQL Commands
SQL Commands
SQL Commands
• SQL commands are instructions. It is used to
communicate with the database. It is also used to
perform specific tasks, functions, and queries of
data.
Syntax:
REATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
Example:
Data Definition Language (DDL)-
CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email
VARCHA R2(100), DOB DATE);
Drop
Drop: It is used to delete both the structure and record
stored in the table.
Syntax:
DROP TABLE ;
Example:
Data Definition Language (DDL)-
DROP TABLE EMPLOYEE;
ALTER
ALTER: It is used to alter the structure of the database. This
change could be either to modify the characteristics of an existing
attribute or probably to add a new attribute.
Syntax:
ALTER TABLE table_name ADD column_name COLUMN-definition;
ALTER TABLE MODIFY(COLUMN DEFINITION....);
Data Definition Language (DDL)-
Example:
ALTER TABLE STU_DETAILS ADD(ADDRESS VARCHAR2(20));
ALTER TABLE STU_DETAILS MODIFY (NAME VARCHAR2(20));
TRUNCATE
TRUNCATE: It is used to delete all the rows from the table and
free the space containing the table.
Syntax:
TRUNCATE TABLE table_name;
Data Definition Language (DDL)-
Example:
TRUNCATE TABLE EMPLOYEE;
Data Manipulation Language
• DML commands are used to modify the database. It is
responsible for all form of CHANGES in the database.
OR
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
Example:
INSERT INTO XYZ (Author, Subject) VALUES ("Sonoo", "DBMS");
Data Manipulation Language - UPDATE
Update: This command is used to update or modify the value of a
column in the table.
Syntax:
UPDATE table_name SET [column_name1= value1,...column_n
ameN = valueN] [WHERE CONDITION]
Example:
UPDATE students
SET User_Name = 'Sonoo'
WHERE Student_Id = '3'
Data Control Language
DCL commands are used to GRANT and TAKE BACK
authority from any database user.
Revoke
Data Control Language - Grant
GRANT: It is used to give user access privileges to a database.
Example:
GRANT SELECT, UPDATE ON MY_TABLE TO SOME_USER, ANOT
HER_USER;
Example:
- Rollback
ROLLBACK; SAVEPOINT SAVEPOINT_NA
ME;
Transaction Control Language
Example:
DELETE FROM CUSTOMERS
WHERE AGE = 25;
ROLLBACK;
Data Query Language
DQL is used to fetch the data from the database.
It uses only one command:
SELECT
a. SELECT: This is the same as the projection operation of
relational algebra. It is used to select the attribute based on
the condition described by WHERE clause.
Syntax:
SELECT expressions FROM TABLES WHERE conditions;
Example:
SELECT emp_name FROM employee WHERE age > 20;
SQL Operator
It checks if the left operand value is greater than right operand value,
> if yes then condition becomes true.
It checks if the left operand value is less than right operand value, if
< yes then condition becomes true.
It checks if the left operand value is greater than or equal to the right
>= operand value, if yes then condition becomes true.
SQL Arithmetic Operators
Operator Description
It checks if the left operand value is less than or equal to the right
<= operand value, if yes then condition becomes true.
It checks if the left operand value is not less than the right operand
!< value, if yes then condition becomes true.
It checks if the left operand value is not greater than the right operand
!> value, if yes then condition becomes true.
SQL Logical Operators
Operator Description
It compares a value to all values in another value set.
All
It allows the existence of multiple conditions in an SQL statement.
AND
It compares the values in the list according to the condition.
ANY
It is used to search for values that are within a set of values.
Between
It compares a value to that specified list value.
IN
It reverses the meaning of any logical operator.
NOT
It combines multiple conditions in SQL statements.
OR
It is used to search for the presence of a row in a specified table.
EXIST
It compares a value to similar values using wildcard operator.
LIKE
Example:
SQL> CREATE TABLE EMPLOYEE (
EMP_ID INT NOT NULL,
EMP_NAME VARCHAR (25) NOT NULL,
PHONE_NO INT NOT NULL,
ADDRESS CHAR (30),
PRIMARY KEY (ID)
);
• DESC EMPLOYEE;
• DELETE FROM table_name WHERE condition
• DROP TABLE "table_name";
• SELECT * FROM table_name;
• INSERT INTO TABLE_NAME VALUES (value1, value2, value 3, .... Value N);
• INSERT INTO TABLE_NAME[(col1, col2, col3,.... col N)] VALUES (value1, value2, valu e
3, .... Value N);
• UPDATE table_name SET column_name = value WHERE condition;
Example:
• UPDATE table_name SET column_name = value1, column_name2 = value
WHERE condition;
• DELETE FROM table_name WHERE some_condition;
Views in SQL
• Views in SQL are considered as a virtual table. A view
also contains rows and columns.
SQL Index
• Indexes are special lookup tables. It is used to retrieve data
from the database very fast.
• An Index is used to speed up select queries and where clauses.
But it shows down the data input with insert and update
statements. Indexes can be created or dropped without
affecting the data.
• An index in a database is just like an index in the back of a book.
Create Index statement
CREATE INDEX index_name
ON table_name (column1, column2, ...);
Unique Index statement
Syntax
CREATE UNIQUE INDEX index_name ON table_name (column1, colum
n2, ...);
Example
CREATE UNIQUE INDEX websites_idx ON websites (site_name);
Example:
SELECT *
FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE
WHERE SALARY > 4500);
Example:
INSERT INTO EMPLOYEE_BKP
SELECT * FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE);
Syntax
Example
SELECT column1, column2 FRO
SELECT COMPANY, COUNT(*)
M table_name
FROM PRODUCT_MAST
WHERE conditions
GROUP BY COMPANY
GROUP BY column1, column2
HAVING COUNT(*)>2;
HAVING conditions
ORDER BY column1, column2;
ORDER BY
• The ORDER BY clause sorts the result-set in ascending or
descending order.
• It sorts the records in ascending order by default. DESC
keyword is used to sort the records in descending order.
Syntax FROM CUSTOMER
SELECT column1, column2 ORDER BY NAME;
FROM table_name OR
WHERE condition
SELECT *
ORDER BY column1, column2...
AS FROM CUSTOMER
C|DESC; ORDER BY NAME
Example DESC;
SELECT *
SQL Aggregate Functions
COUNT FUNCTION
• 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. •
COUNT function uses the COUNT(*) that returns the count of all
the rows in a specified table. COUNT(*) considers duplicate and Null.
Syntax
COUNT(*) or COUNT( [ALL|DISTINCT] expression )
Example
SELECT COUNT(*) FROM PRODUCT_MAST;
SELECT COUNT(*) FROM PRODUCT_MAST; WHERE RATE>=20;
SELECT COUNT(DISTINCT COMPANY) FROM PRODUCT_MAST;
SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY;
SELECT COMPANY, COUNT(*) FROM PRODUCT_MAST GROUP BY COMPANY
HAVING COUNT(*)>2;
SUM FUNCTION
• Sum function is used to calculate the sum of all selected
columns. It works on numeric fields only.
Syntax
SUM() or SUM( [ALL|DISTINCT] expression )
Example
SELECT SUM(COST) FROM PRODUCT_MAST;
SUM() with WHERE
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3;
SUM() with GROUP BY
SELECT SUM(COST) FROM PRODUCT_MAST WHERE QTY>3 GROUP
BY COMPANY;
SUM() with HAVING
SELECT COMPANY, SUM(COST) FROM PRODUCT_MAST GROUP BY COM
PANY HAVING SUM(COST)>=170;
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
AVG() or AVG( [ALL|DISTINCT] expression )
Example
SELECT AVG(COST) FROM PRODUCT_MAST;
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
MAX() or MAX( [ALL|DISTINCT] expression )
Example
SELECT MAX(RATE) FROM PRODUCT_MAST;
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
MIN() or MIN( [ALL|DISTINCT] expression )
Example
SELECT MIN(RATE) FROM PRODUCT_MAST;
SQL JOIN
SQL, JOIN means "to combine two or more tables". In SQL, JOIN clause is used to
combine the records from two or more tables in a database.
RIGHT JOIN
FULL JOIN
INNER JOIN
In SQL, INNER JOIN selects records that have matching values in both tables as
long as the condition is satisfied. It returns the combination of all rows from both
the tables where the condition satisfies.
Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
Example
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
INNER JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
LEFT JOIN
The SQL left join returns all the values from left table and the matching values
from the right table. If there is no matching join value, it will return NULL.
Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
Example
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
LEFT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
RIGHT JOIN
In SQL, RIGHT JOIN returns all the values from the values from the rows of right
table and the matched values from the left table. If there is no matching in both
tables, it will return NULL.
Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
Example
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
RIGHT JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
FULL JOIN
In SQL, FULL JOIN is the result of a combination of both left and right outer join.
Join tables have all the records from both tables. It puts NULL on the place of
matches not found.
Syntax
SELECT table1.column1, table1.column2, table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
Example
SELECT EMPLOYEE.EMP_NAME, PROJECT.DEPARTMENT
FROM EMPLOYEE
FULL JOIN PROJECT
ON PROJECT.EMP_ID = EMPLOYEE.EMP_ID;
SQL Set Operation
The SQL Set operation is used to combine the two or
more SQL SELECT statements
Syntax
SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;
Example
SELECT * FROM First
UNION
SELECT * FROM Second;
Intersect Operation
• It is used to combine two SELECT statements. The Intersect operation
returns the common rows from both the SELECT statements.
• In the Intersect operation, the number of datatype and columns must be the
same.
• It has no duplicates and it arranges the data in ascending order by default.
Syntax
SELECT column_name FROM table1
INTERSECT
SELECT column_name FROM table2;
Example
SELECT * FROM First
INTERSECT
SELECT * FROM Second;
MINUSOperation
• 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.
• It has no duplicates and data arranged in ascending order by default.
Syntax
SELECT column_name FROM table1
MINUS
SELECT column_name FROM table2;
Example
SELECT * FROM First
MINUS
SELECT * FROM Second;