sqlppt
sqlppt
sqlppt
What is SQL?
• SQL stands for Structured Query Language. It is
used for storing and managing data in Relational
Database Management System (RDBMS).
• It is a standard language for Relational Database
System. It enables a user to create, read, update
and delete relational databases and tables.
• All the RDBMS like MySQL, Oracle, MS Access
and SQL Server use SQL as their standard
database language.
• SQL allows users to query the database in a
number of ways, using English-like statements.
What are the SQL?
SQL follows the following rules:
• Structure query language is not case sensitive.
Generally, keywords of SQL are written in uppercase.
Syntax:
REATE TABLE TABLE_NAME (COLUMN_NAME
DATATYPES[,....]);
Example:
CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email
VARCHA R2(100), DOB DATE);
(DDL)- Drop
Drop: It is used to delete both the structure and record
stored in the table.
Syntax:
DROP TABLE ;
Example:
DROP TABLE EMPLOYEE;
(DDL)- 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....);
Example:
ALTER TABLE STU_DETAILS ADD(ADDRESS
VARCHAR2(20)); ALTER TABLE STU_DETAILS MODIFY
(NAME VARCHAR2(20));
(DDL)- 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;
Example:
TRUNCATE TABLE EMPLOYEE;
Language
• DML commands are used to modify the database. It is
responsible for all form of CHANGESin the database.
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
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:
Syntax:
SELECT Column1,column-n FROM TABLES
expressions WHERE conditions;
Example:
S SELECT emp_nameFROM employee WHERE
age > 20;
SQL
Operator
SQL Comparison Operators:
Operator Description
+ It adds the value of both operands.
= It checks if two operands values are equal or not, if the values are
queal then condition becomes true.
!= It checks if two operands values are equal or not, if values are not
equal, then condition becomes true.
<> It checks if two operands values are equal or not, if values are not
equal then condition becomes true.
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
Between It is used to search for values that are within a set of values.
The following SQL creates a PRIMARY KEY on the "ID" column when the "Persons" table is created:
Persons
1 Hansen Ola 30
Table 2 Svendson Tove 23
3 Pettersen Kari 20
Order table
OrderID OrderNumbe PersonID
r
1 77895 3
2 44678 3
3 22456 2
4 24562 1
SQL FOREIGN KEY on CREATE TABLE
The following SQL creates a FOREIGN KEY on
the "PersonID" column when the "Orders" table
is created:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES
Persons(PersonID)
);
Exam
ple:
• 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.
Example:
SELECT *
FROM EMPLOYEE WHERE
ID IN (SELECT ID FROM
EMPLOYEE WHERE SALARY
> 4500);
Statement
• SQL subquery can also be used with the Insert statement. In the insert statement,
data returned from the subquery is used to insert into another table.
• In the subquery, the selected data can be modified with any of the
character, date functions.
Syntax:
INSERT INTO table_name (column1, column2, column3....)
SELECT * FROM table_name WHERE VALUE OPERATOR
Example:
INSERT INTO EMPLOYEE_BKP
SELECT * FROM EMPLOYEE
WHERE ID IN (SELECT ID
FROM EMPLOYEE);
Statement
The subquery of SQL can be used in conjunction with the
Update statement. When a subquery is used with the Update
statement, then either single or multiple columns in a table
can be updated.
Syntax:
UPDATE table SET column_name = new_value WHERE VALUE OPERATOR (SELECT
COLUMN_NAME FROM TABLE_NAME WHERE condition);
Example:
Let's assume we have an EMPLOYEE_BKP table available which is backup of
EMPLOYEE table. The given example updates the SALARY by .25 times in the
EMPLOYEE table for all employee whose AGE is greater than or equal to 29.
UPDATE EMPLOYEE
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 29);
Statement
The subquery of SQL can be used in conjunction with the
Delete statement just like any other statements mentioned
above.
Syntax:
DELETE FROM TABLE_NAME WHERE VALUE OPERATOR
(SELECT COLUMN_NAME FROM TABLE_NAME WHERE
condition);
Example:
Let's assume we have an EMPLOYEE_BKP table available which is backup of
EMPLOYEE table. The given example deletes the records from the EMPLOYEE table for
all EMPLOYEE whose AGE is greater than or equal to 29.
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.
INNER JOIN
LEFT JOIN
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;
THANKS!!!