The document discusses Structured Query Language (SQL). It introduces SQL and provides information on its architecture, commands, data types, and use for data warehousing. SQL is described as a language for storing, manipulating, and retrieving data in relational database management systems. Common SQL commands are listed as CREATE, SELECT, INSERT, UPDATE, DELETE, and DROP.
3. Structured Query Language
Prof. K. Adisesha (Ph. D)
3
Structured Query Language (SQL):
SQL is Structured Query Language, which is a computer language for
storing, manipulating and retrieving data stored in relational database.
Structured Query Language and it helps to make practice on SQL commands which
provides immediate results.
SQL is the standard language for Relation Database System.
All relational database management systems like MySQL, MS Access, and Oracle,
Sybase, Informix, and SQL Server use SQL as standard database language.
4. Structured Query Language
Prof. K. Adisesha (Ph. D)
4
A Brief History of SQL:
A Relational database management system (RDBMS) is a database management
system (DBMS) that is based on the relational model as introduced by E. F. Codd.
1970 − Dr. Edgar F. "Ted" Codd of IBM is known as the father of relational
databases. He described a relational model for databases.
1974 − Structured Query Language appeared.
1978 − IBM worked to develop Codd's ideas and released a product named System/R.
1986 − IBM developed the first prototype of relational database and standardized by
ANSI. The first relational database was released by Relational Software which later
came to be known as Oracle.
5. Structured Query Language
Prof. K. Adisesha (Ph. D)
5
Structured Query Language (SQL):
SQL is Structured Query Language, which is a computer language for
storing, manipulating and retrieving data stored in relational database.
6. Structured Query Language
Prof. K. Adisesha (Ph. D)
6
Structured Query Language (SQL):
When you are executing an SQL command for any RDBMS, the system determines the
best way to carry out your request and SQL engine figures out how to interpret the task.
There are various components included in the process.
These components are:
Query Dispatcher
Optimization Engines
Classic Query Engine
SQL Query Engine
7. Structured Query Language
Prof. K. Adisesha (Ph. D)
7
Need for SQL:
SQL is Structured Query Language used for storing, manipulating and
retrieving data stored in relational database.
Allows users to create and drop databases and tables.
Allows users to describe the data.
Allows users to define the data in database and manipulate that data.
Allows users to access data in relational database management systems.
Allows embedding within other languages using SQL modules, libraries & pre-
compilers.
Allows users to set permissions on tables, procedures, and views
8. Structured Query Language
Prof. K. Adisesha (Ph. D)
8
SQL Commands:
The standard SQL commands to interact with relational databases are CREATE,
SELECT, INSERT, UPDATE, DELETE and DROP.
These commands can be classified into groups based on their nature:
DDL - Data Definition Language
DML - Data Manipulation Language
DCL - Data Control Language
TCL – Transaction Control Language
9. Structured Query Language
Prof. K. Adisesha (Ph. D)
9
Data Definition Language (DDL):
DDL defines the conceptual schema providing a link between the logical and the
physical structure of the database.
The functions of the Data Definition Language (DDL) are:
It defines the physical characteristics of each record, filed in the record, field’s data type,
field's length, field’s logical name and also specify relationship among those records.
Describes the schema and subschema.
Indicates the keys of records.
Provides data security measures.
Provides for the logical and physical data independence.
10. Structured Query Language
Prof. K. Adisesha (Ph. D)
10
Data Definition Language (DDL):
DDL defines the conceptual schema providing a link between the logical and
the physical structure of the database.
Few of the basic commands for DDL are:
Command Description
CREATE
Creates a new table, a view of a table, or other object in the
database.
ALTER Modifies an existing database object, such as a table
DROP
Deletes an entire table, a view of a table or other objects in the
database.
RENAME Renames an existing database object, such as a table
11. Structured Query Language
Prof. K. Adisesha (Ph. D)
11
Data Manipulation Language(DML):
DML provides the data manipulation techniques like selection, insertion,
deletion, updation, modification, replacement, retrieval, sorting and display of
data or records.
DML facilitates use of relationship between the records.
DML provides for independence of programming languages by supporting
several high-level programming languages like COBOL, PL/1 and C++.:
12. Structured Query Language
Prof. K. Adisesha (Ph. D)
12
Data Manipulation Language(DML):
DML provides the data manipulation techniques like selection, insertion,
deletion, updation, modification, replacement, retrieval, sorting and display of
data or records.
Few of the basic commands for DML are:
Command Description
INSERT Creates a record.
UPDATE Modifies records.
DELETE Deletes records.
13. Structured Query Language
Prof. K. Adisesha (Ph. D)
13
Data Control Language (DCL):
These SQL commands are used for providing security to database objects.
Few of the basic commands for DCL are:
Command Description
GRANT
Gives a privilege to user.
Such as setting Passwords to Database Users
REVOKE Takes back privileges granted from user.
14. Structured Query Language
Prof. K. Adisesha (Ph. D)
14
Transaction Control Language (TCL):
It includes commands to control the transactions in a database system.
Few of the basic commands for TCL are:
Command Description
SELECT Retrieves certain records from one or more tables
syntax:
>SELECT * FROM table_name;
15. Data Types in SQL
Prof. K. Adisesha (Ph. D)
15
Data Types in SQL:
The following are the most common data types of SQL
Data Data Type From To
Numeric
int -2,147,483,648 2,147,483,647
numeric -10^38 +1 10^38 -1
float -1.79E + 308 1.79E + 308
real -3.40E + 38 3.40E + 38
String
Char
Maximum length of 8,000
characters
Fixed length non-Unicode characters
varchar Maximum of 8,000 characters. Variable-length non-Unicode data
16. Data Types in SQL
Prof. K. Adisesha (Ph. D)
16
Data Types in SQL:
The following are the most common data types of SQL
Data Data Type From To
Date/
Time
datetime Jan 1, 1753 Dec 31, 9999
smalldatetime Jan 1, 1900 Jun 6, 2079
date Stores a date like June 30, 2020
time Stores a time of day like 12:30 P.M.
timestamp
Stores a database-wide unique number that gets updated every
time a row gets updated
17. Operator in SQL
Prof. K. Adisesha (Ph. D)
17
Operator in SQL:
An operator is a reserved word or a character used primarily in an SQL
statement's WHERE clause to perform operation(s), such as comparisons and
arithmetic operations.
These Operators are used to specify conditions in an SQL statement and to
serve as conjunctions for multiple conditions in a statement.
Arithmetic operators
Comparison operators
Logical operators
Operators used to negate conditions
18. Arithmetic Operators
Prof. K. Adisesha (Ph. D)
18
Arithmetic Operators in SQL:
The following are the most common Arithmetic Operators of SQL: Given a=10, b=20
Operator Description Example
(Addition) + Adds values on either side of the operator a + b will give 30
(Subtraction) - Subtracts right hand operand from left hand operand. a - b will give -10
(Multiplication) * Multiplies values on either side of the operator. a * b will give 200
(Division) / Divides left hand operand by right hand operand. b / a will give 2
(Modulus) %
Divides left hand operand by right hand operand and
returns remainder.
b % a will give 0
19. Operator Description Example
=
Checks if the values of two operands are equal or not, if yes then
condition becomes true.
(a = b) is False
!= , < >
Checks if the values of two operands are equal or not, if values are
not equal then condition becomes true..
(a != b) is true
(a <> b) is true
>, >=
Checks if the value of left operand is greater than the value of right
operand, if yes then condition becomes true.
(a > b) is False
(a >= b) is False
< , <=
Checks if the value of left operand is less than or equal to the value of
right operand, if yes then condition becomes true.
(a < b) is true
(a <= b) is true
! > , !<
Checks if the value of left operand is not less than the value of right
operand, if yes then condition becomes true.
(a !< b) is false
(a !> b) is true
Comparison Operators
Prof. K. Adisesha (Ph. D)
19
Comparison Operators in SQL:
The following are the most common Comparison Operators of SQL: Given a=10, b=20
20. Logical Operators
Prof. K. Adisesha (Ph. D)
20
Logical Operators in SQL:
The following are the most common Logical Operators of SQL:
Operator Description
ALL, ANY
The ALL operator is used to compare a value to all values in another value set.
The ANY operator is used to compare a value to any applicable value in the list on condition
AND, OR The AND, OR operator allows the existence of multiple conditions in an SQL statement's
NOT The NOT operator reverses the meaning of the logical operator with which it is used
BETWEEN
The BETWEEN operator is used to search for values that are within a set of values, given the
minimum value and the maximum value.
EXISTS
The EXISTS operator is used to search for the presence of a row in a specified table that meets a
certain criterion.
UNIQUE he UNIQUE operator searches every row of a specified table for uniqueness
21. SQL - Expressions
Prof. K. Adisesha (Ph. D)
21
Expressions in SQL:
An expression is a combination of one or more values, operators and SQL
functions that evaluate to a value..
These SQL EXPRESSIONs are like formulae and they are written in query
language.
There are different types of SQL expressions are:
Boolean
Numeric
Date
22. SQL - Expressions
Prof. K. Adisesha (Ph. D)
22
Boolean Expressions in SQL:
SQL Boolean Expressions fetch the data based on matching a single value.
Following is the syntax:
SELECT column1, column2, .. columnN
FROM table_name
WHERE SINGLE VALUE MATCHING EXPRESSION;
Example showing the usage of various SQL Boolean Expressions −
SQL> SELECT * FROM CUSTOMERS WHERE SALARY = 20000;
23. SQL - Expressions
Prof. K. Adisesha (Ph. D)
23
Numeric Expression in SQL:
These expressions are used to perform any mathematical operation in any query.
The numerical expression is used for a mathematical expression or any formula
Following is the syntax:
SELECT numerical expression as OPERATION_NAME
[FROM table_name
WHERE CONDITION] ;
Example showing the usage of various SQL Numeric Expressions −
SQL> SELECT (15 + 6) AS ADDITION;
SQL> SELECT COUNT(*) AS "RECORDS" FROM CUSTOMERS;
24. SQL - Expressions
Prof. K. Adisesha (Ph. D)
24
Date Expressions in SQL:
These expressions are used to calculate and return the Date and Time operation in any
query.
Date Expressions return current system date and time values
Following is the syntax:
SQL> SELECT CURRENT_TIMESTAMP;
Example showing the usage of various SQL Numeric Expressions −
SQL> SELECT GETDATE();
25. SQL - Database
Prof. K. Adisesha (Ph. D)
25
Managing Database in SQL:
Always the database name should be unique within the RDBMS
CREATE Database:
The SQL CREATE DATABASE statement is used to create a new SQL database
Syntax: CREATE DATABASE DatabaseName;
DROP or DELETE Database
The SQL DROP DATABASE statement is used to drop an existing database in SQL schema
Syntax: DROP DATABASE DatabaseName;
SELECT Database, USE Statement
The SQL USE statement is used to select any existing database in the SQL schema
Syntax: USE DatabaseName;
26. SQL - CREATE Table
Prof. K. Adisesha (Ph. D)
26
SQL CREATE TABLE Statement:
Creating a basic table involves naming the table and defining its columns and each
column's data type.
The SQL CREATE TABLE statement is used to create a new table
Syntax: Example:
27. SQL - Drop Table
Prof. K. Adisesha (Ph. D)
27
SQL Drop TABLE Statement:
The SQL DROP TABLE statement is used to remove a table definition and all the data,
indexes, triggers, constraints and permission specifications for that table..
The DROP TABLE statement is used to drop an existing table in a database
Deleting a table will result in loss of complete information stored in the table!
Syntax: Example:
SQL TRUNCATE TABLE
The TRUNCATE TABLE statement is used to delete the data inside a table, but not the table
itself.
Syntax:
28. SQL - Alter Table
Prof. K. Adisesha (Ph. D)
28
SQL Alter TABLE Statement:
The ALTER TABLE statement is used to add, delete, or modify columns in an existing
table.
ALTER TABLE - ADD Column
To add a column in a table, use the following syntax:
ALTER TABLE - DROP Column
To drop a column in a table, use the following syntax:
ALTER TABLE - ALTER/MODIFY Column
To drop a column in a table, use the following syntax:
29. SQL Constraints
Prof. K. Adisesha (Ph. D)
29
SQL Create Constraints:
Constraints can be specified when the table is created with the CREATE TABLE
statement, or after the table is created with the ALTER TABLE statement.
SQL constraints are used to specify rules for the data in a table
The following constraints are commonly used in SQL:
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of a NOT NULL and UNIQUE.
FOREIGN KEY - Uniquely identifies a row/record in another table
CHECK - Ensures that all values in a column satisfies a specific condition
DEFAULT - Sets a default value for a column when no value is specified
30. SQL Constraints
Prof. K. Adisesha (Ph. D)
30
SQL Create Constraints:
Constraints can be specified when the table is created with the CREATE TABLE
statement, or after the table is created with the ALTER TABLE statement.
SQL constraints are used to specify rules for the data in a table
Constraints can be column level or table level.
Syntax: Example:
31. SQL INSERT Statement
Prof. K. Adisesha (Ph. D)
31
SQL INSERT INTO Statement:
The INSERT INTO statement is used to insert new records in a table.
It is possible to write the INSERT INTO statement in two ways.
The first way specifies both the column names and the values to be inserted
Syntax:
If you are adding values for all the columns of the table, you do not need to specify the column
names in the SQL query.
Syntax:
32. SQL SELECT Statement
Prof. K. Adisesha (Ph. D)
32
SQL SELECT Statement:
The SELECT statement is used to select data from a database.
The data returned is stored in a result table, called the result-set.
If you want to select all the fields available in the table, use the following syntax:
The SELECT DISTINCT statement is used to return only distinct (different) values..
Syntax:
The WHERE clause is used to extract only those records that fulfill a specified condition.
Syntax:
33. SQL Joins
Prof. K. Adisesha (Ph. D)
33
SQL JOIN:
A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
Different types of the JOINs in SQL:
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records
from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in either left or right
table
34. SQL Joins
Prof. K. Adisesha (Ph. D)
34
SQL INNER JOIN:
The INNER JOIN keyword selects records that have matching values in both tables.
(INNER) JOIN: Returns records that have matching values in both tables
35. SQL Joins
Prof. K. Adisesha (Ph. D)
35
SQL LEFT (OUTER) JOIN:
The LEFT JOIN keyword returns all records from the left table (table1), and the
matched records from the right table (table2).
The result is NULL from the right side, if there is no match.
LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records from
the right table
36. SQL Joins
Prof. K. Adisesha (Ph. D)
36
SQL RIGHT JOIN:
The RIGHT JOIN keyword returns all records from the right table (table2), and the
matched records from the left table (table1).
The result is NULL from the left side, when there is no match.
RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched records
from the left table
37. SQL Joins
Prof. K. Adisesha (Ph. D)
37
SQL FULL OUTER JOIN:
The FULL OUTER JOIN keyword returns all records when there is a match in left
(table1) or right (table2) table records.
FULL OUTER JOIN can potentially return very large result-sets.
FULL (OUTER) JOIN: Returns all records when there is a match in either left or right table
38. SQL ORDER BY
Prof. K. Adisesha (Ph. D)
38
SQL ORDER BY Keyword:
The ORDER BY keyword is used to sort the result-set in ascending or descending
order.
The ORDER BY keyword sorts the records in ascending order by default. To sort the
records in descending order, use the DESC keyword.
39. SQL GROUP BY
Prof. K. Adisesha (Ph. D)
39
SQL GROUP BY:
he GROUP BY statement groups rows that have the same values into summary rows,
like "find the number of customers in each country".
The GROUP BY statement is often used with aggregate functions (COUNT, MAX,
MIN, SUM, AVG) to group the result-set by one or more columns.
40. SQL Functions
Prof. K. Adisesha (Ph. D)
40
SQL MIN() and MAX() Functions:
The MIN() function returns the smallest value of the selected column.
MIN() Syntax:
The MAX() function returns the largest value of the selected column.
MAX() Syntax:
41. SQL Functions
Prof. K. Adisesha (Ph. D)
41
SQL COUNT(), AVG() and SUM() Functions:
The COUNT() function returns the number of rows that matches a specified
criterion.
The AVG() function returns the average value of a numeric column.
The SUM() function returns the total sum of a numeric column.