0% found this document useful (0 votes)
35 views

What Is SQL? - SQL Introduction

SQL is a language used to store, manipulate, and retrieve data in relational databases. It allows users to define and manage database structures, insert, update, query, and manipulate data. The document provides an overview of SQL, including the different types of SQL statements like DDL, DML, DCL, and data types like numeric, character, datetime. It also discusses database concepts like relationships, constraints, normalization and how SQL queries are processed.

Uploaded by

Ibrahimm Muslimm
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

What Is SQL? - SQL Introduction

SQL is a language used to store, manipulate, and retrieve data in relational databases. It allows users to define and manage database structures, insert, update, query, and manipulate data. The document provides an overview of SQL, including the different types of SQL statements like DDL, DML, DCL, and data types like numeric, character, datetime. It also discusses database concepts like relationships, constraints, normalization and how SQL queries are processed.

Uploaded by

Ibrahimm Muslimm
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 184

What is SQL?

- SQL Introduction
What is SQL?
SQL stands for Structured Query Language use for storing, manipulating and retrieving relational database data.
SQL queries to retrieve data from database same as you can adding and manipulating database data.

 SQL is a very powerful and diverse database language use to storing data into databases. SQL is loosely typed
language so you can learn easily.

 In this SQL tutorial, we use command line examples to know about executing speed of SQL. It's take very bit of
time for executing and retrieving result.

 SQL is a greater tool with web languages such as PHP, Python, Java, ASP et cetera to build dynamic web
applications.

Before starting SQL, relational databases have several point that are important to keep in mind.

1. RDBMS stands for Relational Database Management System.


2. Data Integrity : Store data only once and avoiding data duplication.
3. SQL Constraints : Constraints are the rules which are apply to table columns to store valid data and prevents the
user to storing/entering invalid data into table columns.
4. Better security : Assign grant or privilege to a individual User. Using this grant user can store confidential data into
table by using username or password.
5. Database Normalization : Database normalization is the process to store database data very efficiently. No need to
store same data more then one time and reduce the Data redundancy.
6. Different types of relationships : One to one, One to many, Many to many
1. One to one relationship : merging for two tables.
2. One to many relationship : create a foreign key from an parent table to the child table.
3. Many to many relationship : create a new relation table.

Keep in your mind...


 SQL is not case sensitive.
 But SQL Data is a case sensitive.

SQL Process Flow


When you execute SQL query come to a SQL Server. SQL Server work is managing database, load balancing,
transaction management etc. SQL server call to a actual physical database table and return the result. This process is
take small bit of time and depend of query and SQL server load.

Rules of Semicolon(;) or Forward Slash(/) in SQL


There is a huge difference in SQL between the meaning of a / and a ; because they work differently.

 SQL database systems require semicolon(;) at the end of statement to know it's ending.
 Use forward slash(/) once at the end of each script, to tell SQL that there is not more lines of code. you can't use forward
slash(/) at the middle of the script.

The ; means terminate the current statement execute it and store it to the "SQL buffer", whereas the / executes whatever
statement script is in the current "SQL buffer".
Type of SQL Statements
Type of SQL statements are divided into five different categories: Data definition language (DDL), Data manipulation
language (DML), Data Control Language (DCL), Transaction Control Statement (TCS), Session Control Statements
(SCS).

Data Definition Language (DDL)


Data definition statement are use to define the database structure or table.

Statement Description

CREATE Create new database/table.

ALTER Modifies the structure of database/table.

DROP Deletes a database/table.

TRUNCATE Remove all table records including allocated table spaces.

RENAME Rename the database/table.


Data Manipulation Language (DML)
Data manipulation statement are use for managing data within table object.

Statement Description

SELECT Retrieve data from the table.

INSERT Insert data into a table.

UPDATE Updates existing data with new data within a table.

DELETE Deletes the records rows from the table.

MERGE MERGE (also called UPSERT) statements to INSERT new records or UPDATE existing records depending
on condition matches or not.

LOCK TABLE LOCK TABLE statement to lock one or more tables in a specified mode. Table access denied to a other
users for the duration of your table operation.

CALL Statements are supported in PL/SQL only for executed dynamically. CALL a PL/SQL program or EXPLAIN
EXPLAIN PLAN PATH access the data path.

Data Control Language (DCL)


Data control statement are use to give privileges to access limited data.

Statement Description

GRANT Gives privileges to user for accessing database data.

REVOKE Take back for given privileges.

ANALYZE ANALYZE statement to collect statistics information about index, cluster, table.

AUDIT To track the occurrence of a specific SQL statement or all SQL statements during the user
sessions.

COMMENT Write comment to the data table.


Transaction Control Statement (TCS)
Transaction control statement are use to apply the changes permanently save into database.

Statement Description

COMMIT Permanent work save into database.

ROLLBACK Restore database to original form since the last COMMIT.

SAVEPOINT Create SAVEPOINT for later use ROLLBACK the new changes.

SET TRANSACTION SET TRANSACTION command set the transaction properties such as read-write/read only
access.

PL/SQL Transaction commit, rollback, savepoint, autocommit, Set Transaction read more.

Session Control Statement (SCS)


Session control statement are manage properties dynamically of a user session.

Statement Description
ALTER SESSION ALTER SESSION statement to modify conditions or parameters that are affect to your database
connection.

SET ROLE SET ROLE statement to enable or disable the roles that are currently enabled for the session.

Oracle SQL Data Types


Oracle database provide built-in SQL data types also you can make user define types for letter use as data types. Every
database define a column data type for each and every tables.

SQL data types are two type scaler type or non scalar type. Scalar SQL data types haven't internal components. It's like
linear data types. Whereas non scalar SQL data types have internal component to store multiple values and easily you
can manipulate the data.

Oracle SQL Data types


Built-in Oracle SQL data types are following,

 Numeric Datatypes
 Character Datatypes
 LONG and ROW Datatypes
 ROWID Datatypes
 Datetime Datatypes
 Large Object Datatypes
Number Datatypes
Following are numeric data types in SQL.

Datatype Description

NUMBER [ ( precision [, NUMBER data type use to store numeric data.


scale ] ) NUMBER data type have precision and scale.
Storage Range: Precision range(p) : 1 to 38 and Scale range(s) : -84 to 127
NUMBER Subtypes: This sub type supported ANSI, DB2, and SQL data type define different type
storage range.

ANSI, DB2 Datatypes Maximum Precision Oracle Data types

INTEGER 38 digits NUMBER(p,0)

INT 38 digits

SMALLINT 38 digits

FLOAT [ (size) ] 126 binary digits FLOAT(126)

DOUBLE PRECISION 126 binary digits FLOAT(126)


REAL 63 binary digits FLOAT(63)

DECIMAL[(precision [, scale ])] 38 digits NUMBER(p,s)

NUMERIC[(precision [, scale ])] 38 digits

FLOAT [ ( precision ) ] FLOAT data type is subtype of NUMBER datatype.


Storage Range: Precision range(p): 1 to 126
Example: col1 FLOAT(2)

BINARY_FLOAT BINARY_FLOAT datatype use binary precision (32-bit).


This data type requires 5 bytes including length byte.
Advantages: Arithmetic calculations fast and reduces the storage requirements.

BINARY_DOUBLE BINARY_DOUBLE datatype use double binary precision (64-bit).


This data type requires 9 bytes including length byte.

Character Datatypes
Character Data type use to store alphabetic/alphanumeric, following are character data types in Oracle SQL.

Datatype Description Storage(Maximum)


CHAR [ (size) ] CHAR data type use to store character data within predefined length. 2000 bytes

NCHAR [ (size) ] NCHAR data type use to store national character data within predefined length. 2000 bytes

VARCHAR2(size) VARCHAR2 data type use to store variable strings data within predefined length. 4000 bytes
You have to must specify the size of VARCHAR2 datatype.
VARCHAR2 Subtypes: This sub type define same length value.

Sub Data type Description

VARCHAR(size) You can also use this data type.

NVARCHAR2(size) NVARCHAR2 data type use to store Unicode string data within predefined length. 4000 bytes
You have to must specify the size of NVARCHAR2 datatype.

LONG and ROW Datatypes


LONG and ROW data type store variable strings data within predefined length, This datatypes use for backward
compatibility. Following are LONG and ROW datatypes in Oracle SQL.
Datatype Description Storage(Maximum)

RAW(size) RAW data type use to store binary data such as image, graphics etc. 2000 bytes
You have to must specify the size of RAW column data type.

LONG LONG data type use to store variable strings data within predefined length, This data type upto 2 gigabytes
use for backward compatibility. Please use CLOB instead of LONG type.

LONG RAW LONG RAW data type same as LONG type use to store variable strings data within upto 2 gigabytes
predefined length, This data type use for backward compatibility. Please use BLOB instead
of LONG RAW type.

ROWID Datatypes
ROWID data type represent actual storage address of a row. Following are ROWID datatypes in Oracle SQL.

Datatype Description Storage(Maximum)

ROWID ROWID data type represent actual storage address of a row. and table index identities as a logical rowid. This
data type use for backward compatibility. Recommended to use UROWID data type.
UROWID[(size)] UROWID data type identify as universal rowid. Same as ROWID type and use newer 4000 bytes
developing applications use UROWID data type.
You can specify optional size of UROWID column type.

Date/Time Datatypes
Variable that has date/time data type hold value call datetimes. Oracle SQL automatically converts character value in to
default date format ('DD-MON-YY') TO_DATE values. Following are Date/Time data types in Oracle SQL.

Datatype Description Range

DATE DATE data type to store valid date-time format with fixed length. Starting date from Jan 1, 4712 Jan 1, 4712 BC
BC to Dec 31, 9999 AD. to
Dec 31, 9999
AD

TIMESTAMP TIMESTAMP data type to store valid date (year, month, day) with time (hour, minute, second).

Type TIMESTAMP Type

1 Syntax: TIMESTAMP [(fractional_seconds_precision)]
Example: TIMESTAMP '2014-04-13 18:10:52.124'
fractional_seconds_precision optionally specifies the number of digits in the fractional part of
the SECOND datetime field. Range 0 to 9. The default is 6.

2 Syntax: TIMESTAMP [(fractional_seconds_precision) ] WITH TIME ZONE


Example: TIMESTAMP '2014-04-13 18:10:52.124 +05:30'
WITH TIME ZONE specify the UTC time zone. Following two values represent same instant in
UTC.
TIMESTAMP '1999-04-15 8:00:00 -8:00' (8.00 AM Pacific Standard Time) or
TIMESTAMP '1999-04-15 11:00:00 -5:00' (11:00 AM Eastern Standard Time) both are same.

3 Syntax: TIMESTAMP [(fractional_seconds_precision) ] WITH LOCAL TIME ZONE


Example: COL_NAME TIMESTAMP(3) WITH LOCAL TIME ZONE;
WITH LOCAL TIME ZONE specify when you insert value into database column, value is store to
the database time zone, and the time-zone displacement is not stored in the column. When you
retrieve value oracle database returns it in your UTC local time zone.

Example:

CREATE TABLE time_table(


start_time TIMESTAMP,
time1 INTERVAL DAY (6) TO SECOND (5),
time2 INTERVAL YEAR TO MONTH
);

Variable that has interval data type hold value call intervals. Following are Interval data types in SQL.

Datatype Description
INTERVAL YEAR TO INTERVAL YEAR TO MONTH data type to store and manipulate intervals of year and month.
MONTH Syntax: INTERVAL YEAR [ (year_precision) ] TO MONTH
Note: precision specifies number of digits in years field range from 0 to 9 and default is 2.
Size of datatype 5 bytes fixed.

INTERVAL DAY TO INTERVAL DAY TO SECOND data type to store and manipulate intervals of days, hours, minutes,
SECOND and seconds.
Syntax: INTERVAL DAY[(day_precision)] TO SECOND[(fractional_seconds_precision)]
Note: day_precision specifies number of digits in days field range from 0 to 9. The default is 2.
fractional_seconds_precision specifies number of digits in days field range from 0 to 9. The default
is 6.
Size of datatype 11 bytes fixed.

Large Object Datatypes (LOB types)


LOB data types use to store large object such as image, video, graphics, text, audio. Maximum size up to 4 Gigabytes.
Following are LOB data types in SQL.

Datatype Description Storage(Maximum)


BFILE BFILE data type to store large binary object into Operating System file. This data type Size: up to 4GB (232 - 1
variable store full file locator's path which point to a stored binary object with in server. bytes)
BFILE data type read only, you can't modify them. Directory name: 30
character
File name: 255
characters

BLOB BLOB data type same as BFILE data type to store unstructured binary object into Operating Size: 8 TB to 128 TB
System file. BLOB type fully supported transactions are recoverable and replicated. (4GB - 1) *
DB_BLOCK_SIZE

CLOB CLOB data type to store large blocks of character data into Database. Store single byte and Size: 8 TB to 128 TB
multi byte character data. CLOB type fully supported transactions are recoverable and (4GB - 1) *
replicated. DB_BLOCK_SIZE

NCLOB NCLOB data type to store large blocks of NCHAR data into Database. Store single byte and Size: 8 TB to 128 TB
multi byte character data. NCLOB type fully supported transactions are recoverable and (4GB - 1) *
replicated. DB_BLOCK_SIZE

SQL Syntax
SQL Syntax is easy and most of the database action you can done using SQL statement. Following are fewer most
frequently useful SQL Query syntax,

 CREATE DATABASE Syntax


 DROP DATABASE Syntax
 CREATE TABLE Syntax
 DESCRIBE TABLE Column Syntax
 INSERT Syntax
 UPDATE Syntax
 DELETE Syntax
 SELECT Syntax
 ALTER TABLE Syntax
 DROP TABLE Syntax
 COMMIT Syntax
 ROLLBACK Syntax
 SET LINESIZE Syntax

SQL CREATE DATABASE Syntax


CREATE DATABASE database_name;

Example:
SQL> CREATE DATABASE user_data;

SQL DROP DATABASE Syntax


DROP DATABASE database_name;

Example:
SQL> DROP DATABASE user_data;
SQL CREATE TABLE Syntax
CREATE TABLE [ IF NOT EXISTS ] table_name(
column_name datatype[(size)] [ NULL | NOT NULL ],
column_name datatype[(size)] [ NULL | NOT NULL ],
[ constraint_name
PRIMARY KEY ( col1, col2, ... ) |
FOREIGN KEY ( col1, col2, ... ) REFERENCES table_2 [ ( col1, col2, ... )
[ ON UPDATE | ON DELETE
[ NO ACTION | SET NULL | SET DEFAULT | CASCADE ]
]
] |
UNIQUE ( col1, col2, ... ) |
CHECK ( expression )
]
...
);

Example:
SQL> CREATE TABLE users_info(
no NUMBER(3) NOT NULL,
name VARCHAR(30),
address VARCHAR(70),
contact_no VARCHAR(12),
PRIMARY KEY (no)
);

SQL DESCRIBE table column Syntax


DESCRIBE table_name;

SQL INSERT Syntax


Insert row values:
INSERT INTO table_name [ ( column_name1, column_name2, ... ) ]
VALUES ( value1_row1, value2_row1, ... );

Example:
SQL> INSERT INTO users_info (no,name,address)
VALUES (1, 'Opal Kole', '63 street Ct.');

Insert multiple row values:


INSERT ALL
INTO table_name [ (column_name1, column_name2, ...) ] VALUES (record1_value1, record1_value2, ...)
INTO table_name [ (column_name1, column_name2, ...) ] VALUES (record2_value1, record2_value2, ...)
INTO table_name [ (column_name1, column_name2, ...) ] VALUES (record3_value1, record3_value2, ...)
....
SELECT * FROM dual;

Example:
SQL> INSERT ALL
INTO users_info (no, name, address, contact_no) VALUES (4, 'Paul Singh', '1343 Prospect St', 000-
444-7141)
INTO users_info (no, name, address, contact_no) VALUES (5, 'Ken Myer', '137 Clay Road', 000-444-
7084)
INTO users_info (no, name, address, contact_no) VALUES (6, 'Jack Evans', '1365 Grove Way', 000-444-
7957)
INTO users_info (no, name, address, contact_no) VALUES (7, 'Reed Koch', '1274 West Street', 000-444-
4784)
SELECT * FROM dual;

4 rows created.

SQL UPDATE Syntax


UPDATE table_name
SET column_name1 = value1, column_name2 = value2 , ...
[ WHERE condition ]
[ LIMIT number ];
Example:
SQL> UPDATE users_info
SET name = "Beccaa Moss" , address ="2500 green city."
WHERE no = 3;

SQL DELETE Syntax


DELETE FROM table_name
[ WHERE condition ]
[ LIMIT number ];

Example:
SQL> DELETE users_info
WHERE no = 3;

SQL SELECT Syntax


SELECT [ DISTINCT | ALL ]
column_name1, column_name2, aggregate_function(column_name) ....
[ FROM table_name ]
[ WHERE condition ]
[ GROUP BY groupby_column_name1, .... ]
[ HAVING having_clause ]
[ ORDER BY order_column_name1 [ ASC | DESC ], .... ];

Example:

We have to fetch all table columns using asterisk (*),


SQL> SELECT * FROM users_info WHERE no = 3;

SQL ALTER TABLE Syntax


ALTER TABLE table_name RENAME TO new_table_name;
ALTER TABLE table_name ADD column_name datatype[(size)];
ALTER TABLE table_name MODIFY column_name column_datatype[(size)];
ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name;
ALTER TABLE table_name DROP COLUMN column_name;

Example:
Add new column to a 'users_info' table
SQL> ALTER TABLE users_info ADD postalcode VARCHAR2(8);

SQL DROP TABLE Syntax


DROP TABLE [ IF EXISTS ]
table_name1, table_name2, ...;

Example:
SQL> DROP TABLE users_info ;

SQL COMMIT Syntax


SQL> COMMIT;

SQL ROLLBACK Syntax


SQL> ROLLBACK;

SQL SET LINESIZE Syntax


SQL Line Size command is use to set a number of character per row.
SET linesize N;

Example:
SQL> SET linesize 300;
SQL CREATE Database
SQL CREATE DATABASE statement to create new database. Oracle SQL you have to first create new database. Before
you learn how to create new database you must have to know about SQL structure.

SQL structure is hierarchically, You first create database. Select database for USE. Now you are in selected database
are. you are access all table as well as make new table and so many things you can done.

Syntax
You have to create new database use following SQL CREATE DATABASE syntax,
CREATE DATABASE database_name;

Example
SQL> CREATE DATABASE user_db;

SQL SHOW DATABASE Statement


SQL SHOW DATABASE statement to display list of all created database.
SQL> SHOW DATABASES;

+---------------------+
| Database |
+---------------------+
| information_schema |
| scott |
| user_db |
| demodb |
+---------------------+
4 rows in set (0.01 sec)

SQL USE Database


SQL USE Statement to use the database. Your have list of database. So whenever you work with database you must
have to select (USE) on database at a time. and you can switch over any time using USE statement.

Syntax
USE database_name;

Example
SQL> USE user_db;

Database changed

SQL DROP Database


SQL DROP DATABASE statement to drop the existing database.
Syntax
DROP DATABASE database_name;

Example
SQL> DROP DATABASE user_db;

Now you can again execute SHOW DATABASE statement. user_db database deleted.


SQL> SHOW DATABASES;

+---------------------+
| Database |
+---------------------+
| information_schema |
| scott |
| demodb |
+---------------------+
3 rows in set (0.00 sec)

SQL CREATE TABLE


SQL CREATE TABLE statement to create a new table with the given name. SQL TABLE columns name must be unique
with specified built-in data type. TABLE COLUMN support various attributes such as column data type, NULL, NOT NULL,
CONSTRAINTS, DEFAULT VALUE et cetera.

Following is SQL CREATE TABLE syntax, but now you just learn basic CREATE TABLE syntax. Once you know SQL
CONSTRAINT concept you can learn this syntax.

Syntax
CREATE TABLE [ IF NOT EXISTS ] table_name(
column_name datatype[(size)] [ NULL | NOT NULL ],
column_name datatype[(size)] [ NULL | NOT NULL ],
[ CONSTRAINT constraint_name
PRIMARY KEY ( col1, col2, ... ) |
FOREIGN KEY ( col1, col2, ... ) REFERENCES table_2 [ ( col1, col2, ... )
[ ON UPDATE | ON DELETE
[ NO ACTION | SET NULL | SET DEFAULT | CASCADE ]
]
] |
UNIQUE ( col1, col2, ... ) |
CHECK ( expression )
]
...
);
Basic Syntax:
CREATE TABLE table_name(
column_name1 datatype(size),
column_name2 datatype(size)
...
);

Keep in Mind... : You can not create new table if table already exist in selected database. and another things table name
must have unique within database.

Example
Here we create new table in SQL. 4 columns specify with data type and size.
SQL> CREATE TABLE users_info(
no NUMBER(3,0),
name VARCHAR(30),
address VARCHAR(70),
contact_no VARCHAR(12)
);

Table created.

SQL DROP TABLE


SQL DROP TABLE statement to delete/drop the table structure, table data, defined constraints or index. But, before
deleting any parent table, make sure child table also should have been already deleted. Otherwise, parent table can not
be deleted due to its integrity.

Syntax
DROP TABLE table_name;

Example
SQL> DROP TABLE users_info;

Table dropped.

SQL TRUNCATE TABLE


SQL TRUNCATE TABLE statement only delete all records in a table and your table keep exist. In this case your table is
empty.

Syntax
TRUNCATE TABLE table_name;

Example
SQL> TRUNCATE TABLE users_info;
Table truncated.

SQL DESC Statement (Describe Table)


SQL DESC statement use for describe the list of column definitions for specified table. You can use either DESC or
DESCRIBE statement. both are return same result.

DESCRIBE statement to get following information:

 Column Name
 Column allow NULL or NOT NULL
 Datatype of the Column
 With database size precision and If NUMERIC datatype scale.

SQL DESC Syntax


SQL DESCRIBE Table Column use following syntax,
DESC table_name

SQL DESC Example


SQL> DESC users_info;

Name Null? Type


------------------------------ -------- ----------------------------
NO NOT NULL NUMBER(3)
NAME VARCHAR2(30)
ADDRESS VARCHAR2(70)
CONTACT_NO VARCHAR2(12)

SQL DESCRIBE Syntax


DESCRIBE table_name

SQL DESCRIBE Syntax


SQL> DESCRIBE users_info;

Name Null? Type


------------------------------ -------- ----------------------------

NO NOT NULL NUMBER(3)


NAME VARCHAR2(30)
ADDRESS VARCHAR2(70)
CONTACT_NO VARCHAR2(12)
SQL INSERT (INSERT INTO / INSERT ALL)
SQL INSERT statement is used to inserting new records into database table. You can insert new record following different
way,

 INSERT INTO Statement


 INSERT ALL Statement
 Multiple Row INSERT into TABLE Statement
 INSERT Data only in specified COLUMNS
 INSERT INTO SELECT Statement

INSERT INTO statement


Using INSERT INTO statement to insert record into database.

When inserting data into table no need to specify the column names if values is table structure wise (column wise).

Syntax
INSERT INTO table_name VALUES (value1, value2, value3, ...);

Example
SQL> INSERT INTO users_info VALUES (1, 'Opal Kole', '63 street Ct.', '000-444-7847');

1 row created.

When you inserting data into table and you haven't know table structure you must specify the column name.
Syntax
INSERT INTO table_name [ (column_name1, column_name2, ...) ]
VALUES (value1, value2, ...);

Example
SQL> INSERT INTO users_info (name, address, no, contact_no)
VALUES ('Beccaa Moss', '2500 green city.', 3, '000-444-7142');

1 row created.

INSERT ALL statement


Using INSERT ALL statement to insert more then one records into table.

We can insert more then one record in single SQL INSERT statement.

Syntax
INSERT ALL
INTO table_name [ (column_name1, column_name2, ...) ] VALUES (record1_value1, record1_value2, ...)
INTO table_name [ (column_name1, column_name2, ...) ] VALUES (record2_value1, record2_value2, ...)
INTO table_name [ (column_name1, column_name2, ...) ] VALUES (record3_value1, record3_value2, ...)
....
SELECT * FROM dual;
Example
SQL> INSERT ALL
INTO users_info (no, name, address, contact_no) VALUES (4, 'Paul Singh', '1343 Prospect St', '000-
444-7141')
INTO users_info (no, name, address, contact_no) VALUES (5, 'Ken Myer', '137 Clay Road', '000-444-
7084')
INTO users_info (no, name, address, contact_no) VALUES (6, 'Jack Evans', '1365 Grove Way', '000-444-
7957')
INTO users_info (no, name, address, contact_no) VALUES (7, 'Reed Koch', '1274 West Street', '000-
444-4784')
SELECT * FROM dual;

4 rows created.

SQL Multiple Row Insert into Table Statements


You can insert multiple record by this way first you execute INSERT INTO statement with & sign with column name. If you
want to add another record you just execute forward slash (/) to again execute last statement automatically and you can
insert new data again.

What is Forward Slash (/)?


Forward Slash (/) - Forward slash tell to a Oracle engine to execute last statement again.
SQL> INSERT INTO users_info VALUES (&no, &name, &address, &contact_no);
Enter value for no: 8
Enter value for name: 'Gabe Hee'
Enter value for address: '1220 Dallas Drive'
Enter value for contact_no: '000-444-4584'
old 1: INSERT INTO users_info VALUES (&no, &name, &address, &contact_no)
new 1: INSERT INTO users_info VALUES (8, 'Gabe Hee', '1220 Dallas Drive', '000-444-4584')

1 row created.

SQL> /
Enter value for no: 9
Enter value for name: 'Ben Mares'
Enter value for address: '101 Candy Road'
Enter value for contact_no: '000-444-5484'
old 1: INSERT INTO users_info VALUES (&no, &name, &address, &contact_no)
new 1: INSERT INTO users_info VALUES (9, 'Ben Mares', '101 Candy Road', '000-444-5484')

1 row created.

SQL>
SQL Insert Data only in specified COLUMNS
You can insert data only specific column. When you write INSERT statement you have to specify column name to
inserting only that column data into table.

Syntax
INSERT INTO Table_Name (specific_column_name1, ...)
VALUES (value1,...);

Example
SQL> INSERT INTO users_info(no, name) VALUES (10, 'Sariya Vargas');

1 row created.

INSERT INTO SELECT Statement


INSERT INTO SELECT Statement to insert data that data you are getting from another table.

Syntax
INSERT INTO new_table_name [(column_name1,column_name2,...)]
SELECT column_name1, column_name1 ... FROM
another_table_name
[WHERE condition];
Example
SQL> CREATE TABLE demo_tbl(
no NUMBER(3),
name VARCHAR2(50)
);

Table created.

SQL> INSERT INTO demo_tbl (no, name)


SELECT no, name FROM
users_info;

10 rows created.

SQL UPDATE
SQL UPDATE statement to update table records with in database. You can update all table row or update data only
matching conditionally WHERE clause.

SQL UPDATE All Rows


Syntax
UPDATE table_name
SET column_name1 = value1, column_name2 = value2, ...;
Example Statement
SQL> UPDATE demo1 SET contact_no = 444;

10 rows updated.

SQL> select * from demo1;

NO NAME ADDRESS CONTACT_NO


--- ----------------- ------------------------------ -------------------
1 Opal Kole 63 street Ct. 000-444-8291
2 Max Miller 41 NEW ROAD. 000-444-8736
3 Beccaa Moss 2500 green city. 000-444-8030
4 Paul Singh 1343 Prospect St 000-444-8029
5 Ken Myer 137 Clay Road 000-444-7972
6 Jack Evans 1365 Grove Way 000-444-8845
7 Reed Koch 1274 West Street 000-444-5672
8 Gabe Hee 1220 Dallas Drive 000-444-5472
9 Ben Mares 101 Candy Road 000-444-6372
10 Sariya Vargas 000-444-6371

10 rows selected.
UPDATE Conditionally use WHERE Clause
UPDATE table data only where WHERE Clause condition match. Use can optionally use LIMIT number to update only
limited record starting from first.

Syntax
UPDATE table_name
SET column_name1 = value1, column_name2 = value2, ...
[ WHERE condition ]
[ LIMIT number];

Example
SQL> UPDATE demo1 SET ADDRESS = '10 Texo Court.' WHERE NO = 10;

1 row updated.

SQL> select * from demo1;

NO NAME ADDRESS CONTACT_NO


--- ----------------- ------------------------------ -------------------
1 Opal Kole 63 street Ct. 000-444-8291
2 Max Miller 41 NEW ROAD. 000-444-8736
3 Beccaa Moss 2500 green city. 000-444-8030
4 Paul Singh 1343 Prospect St 000-444-8029
5 Ken Myer 137 Clay Road 000-444-7972
6 Jack Evans 1365 Grove Way 000-444-8845
7 Reed Koch 1274 West Street 000-444-5672
8 Gabe Hee 1220 Dallas Drive 000-444-5472
9 Ben Mares 101 Candy Road 000-444-6372
10 Sariya Vargas 10 Texo Court. 000-444-6371

10 rows selected.

SQL DELETE
SQL DELETE Statement is used to delete one or more then one row removed from table.

SQL DELETE Query use following two way,

 Remove all TABLE rows


 Remove only specific TABLE row/rows

Remove only specific TABLE row


Syntax
DELETE FROM table_name
[ WHERE condition ]
[ LIMIT number ];
Example
SQL> DELETE FROM demo1 WHERE NO = 10;

1 row deleted.

SQL> SELECT * FROM demo1;

NO NAME ADDRESS CONTACT_NO


--- ----------------- ------------------------------ -------------------
1 Opal Kole 63 street Ct. 000-444-8291
2 Max Miller 41 NEW ROAD. 000-444-8736
3 Beccaa Moss 2500 green city. 000-444-8030
4 Paul Singh 1343 Prospect St 000-444-8029
5 Ken Myer 137 Clay Road 000-444-7972
6 Jack Evans 1365 Grove Way 000-444-8845
7 Reed Koch 1274 West Street 000-444-5672
8 Gabe Hee 1220 Dallas Drive 000-444-5472
9 Ben Mares 101 Candy Road 000-444-6372

9 rows selected.
Remove all TABLE rows
Remove the all table row use this simple delete statement to delete all table data. after execute delete statement SELECT
statement to check table is empty or not.

Syntax
DELETE FROM table_name;

Example
SQL> DELETE FROM demo1;

9 rows deleted.

SQL> SELECT * FROM demo1;

no rows selected.

SQL SELECT
SQL SELECT statement is used to viewing data from the table. This statement return list of table format table data.

We use asterisk (*) sign to fetch all table columns instead of write all column name.

SQL SELECT query is used this four type:

 SELECT All Rows and All Columns


 SELECT All Rows and Selected Columns
 Only Selected Rows and All Columns
 Selected Rows and Selected Columns

SELECT All Rows and All Columns


Syntax
SELECT * FROM table_name;

Example
SQL> SELECT * FROM users_info;

NO NAME ADDRESS CONTACT_NO


--- ----------------- ------------------------------ -------------------
1 Opal Kole 63 street Ct. 000-444-8291
2 Max Miller 41 NEW ROAD. 000-444-8736
3 Beccaa Moss 2500 green city. 000-444-8030
4 Paul Singh 1343 Prospect St 000-444-8029
5 Ken Myer 137 Clay Road 000-444-7972
6 Jack Evans 1365 Grove Way 000-444-8845
7 Reed Koch 1274 West Street 000-444-5672
8 Gabe Hee 1220 Dallas Drive 000-444-5472
9 Ben Mares 101 Candy Road 000-444-6372
10 Sariya Vargas 10 Texo Court. 000-444-6371
10 rows selected.

SELECT All Rows and Selected Columns


Syntax
SELECT column_name1, column_name2, ... FROM table_name;

Example
SQL> SELECT no, name, address FROM users_info;

NO NAME ADDRESS
--- --------------------- --------------------------------
1 Opal Kole 63 street Ct.
2 Max Miller 41 NEW ROAD.
3 Beccaa Moss 2500 green city.
4 Paul Singh 1343 Prospect St
5 Ken Myer 137 Clay Road
6 Jack Evans 1365 Grove Way
7 Reed Koch 1274 West Street
8 Gabe Hee 1220 Dallas Drive
9 Ben Mares 101 Candy Road
10 Sariya Vargas 10 Texo Court.
10 rows selected.

Only Selected Rows and All Columns


Syntax
SELECT * FROM table_name [ WHERE condition ];

Example
SQL> SELECT * FROM users_info WHERE no = 3;

NO NAME ADDRESS CONTACT_NO


--- ----------------- ------------------------------ -------------------
3 Beccaa Moss 2500 green city. 000-444-7586

Selected Rows and Selected Columns


Syntax
SELECT column_name1, column_name2, ... FROM table_name [ WHERE condition ];

Example
SQL> SELECT no, name, address FROM users_info WHERE no = 3;
NO NAME ADDRESS
--- --------------------- -------------------------------
3 Beccaa Moss 2500 green city.

SQL SELECT DISTINCT


SQL SELECT DISTINCT statement is use for eliminate duplicates rows from selected column in table. You can add
DISTINCT statement in multiple column.

 SELECT with DISTINCT on all columns


 SELECT with DISTINCT on two columns
 SELECT with DISTINCT on one columns

SELECT with DISTINCT on all columns


Now we are execute SELECT query with DISTINCT on all columns. DISTINCT all column means same table row value
eliminate duplicates rows.

In this example line number 7 or 13 row are same so two of one row automatically eliminate.

Syntax
SELECT DISTINCT * FROM table_name;

Example
SQL> SELECT * FROM userinfo;

NO NAME ADDRESS
---- ----------------------------- ---------------------------
1 Opal Kole 63 street Ct.
2 Max Miller 41 NEW ROAD.
3 Beccaa Moss 2500 green city.
4 Paul Singh 1343 Prospect St
5 Ken Myer 137 Clay Road
6 Jack Evans 1365 Grove Way
7 Opal Kole 63 street Ct.
8 Max Miller 41 NEW ROAD.
3 Beccaa Moss 2500 green city.

9 rows selected.

SQL> SELECT DISTINCT * FROM userinfo;

NO NAME ADDRESS
---- ----------------------------- ---------------------------
3 Beccaa Moss 2500 green city.
6 Jack Evans 1365 Grove Way
1 Opal Kole 63 street Ct.
2 Max Miller 41 NEW ROAD.
5 Ken Myer 137 Clay Road
8 Max Miller 41 NEW ROAD.
4 Paul Singh 1343 Prospect St
7 Opal Kole 63 street Ct.

8 rows selected.

SELECT with DISTINCT on two columns


Now we are execute SELECT statement with DISTINCT on two columns. DISTINCT two column means eliminate same
value when both column have same value.

In this example line number 5,6 or 11,12 name and address column value are same so this both row automatically
eliminate.

Syntax
SELECT DISTINCT column_name1, column_name2, ... FROM table_name;

Example
SQL> SELECT * FROM userinfo;

NO NAME ADDRESS
---- ----------------------------- ---------------------------
1 Opal Kole 63 street Ct.
2 Max Miller 41 NEW ROAD.
3 Beccaa Moss 2500 green city.
4 Paul Singh 1343 Prospect St
5 Ken Myer 137 Clay Road
6 Jack Evans 1365 Grove Way
7 Opal Kole 63 street Ct.
8 Max Miller 41 NEW ROAD.
3 Beccaa Moss 2500 green city.

9 rows selected.

SQL> SELECT DISTINCT name, address FROM userinfo;

NAME ADDRESS
---------------------------------- ---------------------------
Opal Kole 63 street Ct.
Paul Singh 1343 Prospect St
Ken Myer 137 Clay Road
Beccaa Moss 2500 green city.
Max Miller 41 NEW ROAD.
Jack Evans 1365 Grove Way
6 rows selected.

SELECT with DISTINCT on one columns


Same as above example DISTINCT apply only one columns.

Syntax
SELECT DISTINCT column_name1, column_name2, ... FROM table_name;

Example
SQL> SELECT * FROM userinfo;

NO NAME ADDRESS
---- ----------------------------- ---------------------------
1 Opal Kole 63 street Ct.
2 Max Miller 41 NEW ROAD.
3 Beccaa Moss 2500 green city.
4 Paul Singh 1343 Prospect St
5 Ken Myer 137 Clay Road
6 Jack Evans 1365 Grove Way
7 Opal Kole 63 street Ct.
8 Max Miller 41 NEW ROAD.
3 Beccaa Moss 2500 green city.

9 rows selected.

SQL> SELECT DISTINCT name FROM userinfo;

NAME
----------------------------------
Opal Kole
Max Miller
Beccaa Moss
Paul Singh
Jack Evans
Ken Myer

6 rows selected.
SQL ALTER TABLE
SQL ALTER TABLE Statement to rename table name, add new column, modify existing column (data type, size, etc.),
rename the column, drop the table column.

SQL ALTER TABLE statement is a powerful statement to add, manage or update table structure.

ALTER TABLE Statement to you can do following thing,

 SQL TABLE RENAME


 ADD NEW COLUMN IN TABLE
 MODIFY EXISTING COLUMN IN TABLE
 RENAME COLUMN IN TABLE
 DROP THE EXISTING COLUMN IN TABLE

SQL TABLE RENAME


You can rename the SQL table using this syntax,

Syntax
ALTER TABLE table_name
RENAME TO new_table_name;

Example
SQL> ALTER TABLE userinfo RENAME TO user_info;

Table altered.
SQL ADD NEW COLUMN IN TABLE
You can add new column in table using this syntax,

Syntax
ALTER TABLE table_name
ADD column_name datatype[(size)];

Example
SQL> ALTER TABLE user_info ADD state VARCHAR2(12);

Table altered.

SQL ADD MULTIPLE COLUMN IN TABLE


You can add multiple column in table at a time using this syntax,

Syntax
ALTER TABLE table_name
ADD ( column_name1 datatype[(size)],
column_name2 datatype[(size)],
...
);
Example
SQL> ALTER TABLE user_info
ADD (city VARCHAR2(30),
country VARCHAR2(30)
);

Table altered.

SQL MODIFY EXISTING COLUMN IN TABLE


You can modify the existing column datatype, size, NOT NULL or CONSTRAINS in table using this syntax,

Syntax
ALTER TABLE table_name
MODIFY column_name column_datatype[(size)];

Example

In this example DESC table structure before execute ALTER statement. ALTER Statement to MODIFY the column size.
After alter table again DESC table structure.
SQL> DESC user_info;
Name Null? Type
------------------------ -------- ---------------------
NO NUMBER(3)
NAME VARCHAR2(50)
ADDRESS VARCHAR2(70)
STATE VARCHAR2(12)

SQL> ALTER TABLE user_info MODIFY state VARCHAR2(10);

Table altered.

SQL> DESC user_info;


Name Null? Type
------------------------ -------- ---------------------
NO NUMBER(3)
NAME VARCHAR2(50)
ADDRESS VARCHAR2(70)
STATE VARCHAR2(12)

Same way you can modify multiple existing column in table using following syntax,

Syntax
ALTER TABLE table_name
MODIFY (column_name1 column_datatype[(size)],
column_name2 column_datatype[(size)],
...
);

SQL RENAME COLUMN IN TABLE


You can rename the existing column in table using this syntax,

Syntax
ALTER TABLE table_name
RENAME COLUMN old_column_name TO new_column_name;

Example
SQL> ALTER TABLE user_info
RENAME COLUMN no TO sno;

Table altered.

SQL DROP THE COLUMN IN TABLE


You can drop existing column in table using this syntax,

Syntax
ALTER TABLE table_name
DROP COLUMN column_name;

Example

This example line 8 country column drop using alter table statement.
SQL> DESC user_info;
Name Null? Type
------------------------------ -------- --------------------------
SNO NUMBER(3)
NAME VARCHAR2(50)
ADDRESS VARCHAR2(70)
STATE VARCHAR2(10)
COUNTRY VARCHAR2(30)

SQL> ALTER TABLE user_info DROP COLUMN country;

Table altered.

SQL> DESC user_info;


Name Null? Type
------------------------------ -------- --------------------------
SNO NUMBER(3)
NAME VARCHAR2(50)
ADDRESS VARCHAR2(70)
STATE VARCHAR2(10)

SQL WHERE Clause


SQL WHERE clause is basically use for fetching specific criteria matched data only return. SQL WHERE clause is
optionally clause in DML statement.

SQL WHERE clause we can use with SELECT, UPDATE, DELETE statements. SELECT statement with WHERE clause
execute, fetching all table rows and apply WHERE clause filtering and finally return the result data.

Notes: You can't use INSERT statement with WHERE clause. But you can use INSERT Statement with WHERE cause
only when you get the filter data from another TABLE.

WHERE clause use with SELECT Statement


When you get table data with specific filtered data, you should use where clause, considering following syntax that help
you to understanding WHERE clause.

Syntax
SELECT * FROM table_name WHERE condition;

Example
SQL> SELECT * FROM users_info WHERE name = 'Opal Kole';

NO NAME ADDRESS CONTACT_NO


--- ------------------- --------------------------- --------------
1 Opal Kole 63 street Ct. 000-444-7847
WHERE clause use with UPDATE Statement
When you want to update specific table data you should use UPDATE statement with where clause. You can update/set
more than one columns value.

Syntax

Considering following syntax that help you to understanding WHERE clause,


UPDATE table_name SET column_name=value1, ... WHERE condition;

Example
SQL> UPDATE users_info SET address = '145 Taxo court.' WHERE no = 10;

1 row updated.

WHERE clause use with DELETE Statement


When you want to delete specific table row(s) you should use DELETE statement with WHERE clause, Considering
following syntax that help you to understanding WHERE clause.

Syntax
DELTE FROM table_name WHERE condition;

Example
SQL> DELETE FROM users_info WHERE no = 10;
1 row deleted.

WHERE clause use with INSERT Statement


When you want to insert filter data from another table, you should use INSERT statement with WHERE clause,
Considering following syntax that help you to understanding WHERE clause,

Syntax
INSERT INTO table_name (column_name1, column_name2, ...)
SELECT column_name1, column_name2, ...
FROM another_table_name
WHERE condition;

Example
SQL> INSERT INTO userinfo (no, name, address)
SELECT no, name, address
FROM users_info
WHERE no = 1;

1 row inserted.
WHERE clause Operators
You can use WHERE clause with following operators.

Operator Description Example

= Equal to
SQL> SELECT * FROM users_info WHERE no = 5;

NO NAME ADDRESS CONTACT_NO


-- ----------------- ------------------- --------------
5 Ken Myer 137 Clay Road 000-444-7528

<> Not equal


SQL> SELECT * FROM users_info WHERE no <> 5;

NO NAME ADDRESS CONTACT_NO


-- ----------------- ------------------- --------------
1 Opal Kole 63 street Ct. 000-444-7847
2 Max Miller 41 NEW ROAD. 000-444-8292
3 Beccaa Moss 2500 green city. 000-444-7586
4 Paul Singh 1343 Prospect St 000-444-7585
6 Jack Evans 1365 Grove Way 000-444-8401
7 Reed Koch 1274 West Street 000-444-5228
8 Gabe Hee 1220 Dallas Drive 000-444-5028
9 Ben Mares 101 Candy Road 000-444-5928
10 Sariya Vargas 145 Taxo court. 000-444-5927

9 rows selected.

> Greater than


SQL> SELECT * FROM users_info WHERE no > 5;

NO NAME ADDRESS CONTACT_NO


-- ----------------- ------------------- --------------
6 Jack Evans 1365 Grove Way 000-444-8401
7 Reed Koch 1274 West Street 000-444-5228
8 Gabe Hee 1220 Dallas Drive 000-444-5028
9 Ben Mares 101 Candy Road 000-444-5928
10 Sariya Vargas 145 Taxo court. 000-444-5927

< Less than


SQL> SELECT * FROM users_info WHERE no < 5;

NO NAME ADDRESS CONTACT_NO


-- ----------------- ------------------- --------------
1 Opal Kole 63 street Ct. 000-444-7847
2 Max Miller 41 NEW ROAD. 000-444-8292
3 Beccaa Moss 2500 green city. 000-444-7586
4 Paul Singh 1343 Prospect St 000-444-7585

>= Greater than or equal


SQL> SELECT * FROM users_info WHERE no >= 5;

NO NAME ADDRESS CONTACT_NO


-- ----------------- ------------------- --------------
5 Ken Myer 137 Clay Road 000-444-7528
6 Jack Evans 1365 Grove Way 000-444-8401
7 Reed Koch 1274 West Street 000-444-5228
8 Gabe Hee 1220 Dallas Drive 000-444-5028
9 Ben Mares 101 Candy Road 000-444-5928
10 Sariya Vargas 145 Taxo court. 000-444-5927

6 rows selected.

<= Less than or equal


SQL> SELECT * FROM users_info WHERE no <= 5;

NO NAME ADDRESS CONTACT_NO


-- ----------------- ------------------- --------------
1 Opal Kole 63 street Ct. 000-444-7847
2 Max Miller 41 NEW ROAD. 000-444-8292
3 Beccaa Moss 2500 green city. 000-444-7586
4 Paul Singh 1343 Prospect St 000-444-7585
5 Ken Myer 137 Clay Road 000-444-7528

BETWEE Fetching within range data


SQL> SELECT * FROM users_info WHERE no BETWEEN 1 AND 5;
N

NO NAME ADDRESS CONTACT_NO


-- ----------------- ------------------- --------------
1 Opal Kole 63 street Ct. 000-444-7847
2 Max Miller 41 NEW ROAD. 000-444-8292
3 Beccaa Moss 2500 green city. 000-444-7586
4 Paul Singh 1343 Prospect St 000-444-7585
5 Ken Myer 137 Clay Road 000-444-7528

LIKE Search for a pattern


SQL> SELECT * FROM users_info WHERE name LIKE 'Be%';

NO NAME ADDRESS CONTACT_NO


-- ----------------- ------------------- --------------
3 Beccaa Moss 2500 green city. 000-444-7586
9 Ben Mares 101 Candy Road 000-444-5928

2 rows selected.
IN Allows only specified
SQL> SELECT * FROM users_info WHERE no IN (1,5,10);
values

NO NAME ADDRESS CONTACT_NO


-- ----------------- ------------------- --------------
1 Opal Kole 63 street Ct. 000-444-7847
5 Ken Myer 137 Clay Road 000-444-7528
10 Sariya Vargas 145 Taxo court. 000-444-5927

3 rows selected.

NOT IN Not allows specified values


SQL> SELECT * FROM users_info WHERE no NOT IN (1,3,5,7,9);

NO NAME ADDRESS CONTACT_NO


-- ----------------- ------------------- --------------
2 Max Miller 41 NEW ROAD. 000-444-8292
4 Paul Singh 1343 Prospect St 000-444-7585
6 Jack Evans 1365 Grove Way 000-444-8401
8 Gabe Hee 1220 Dallas Drive 000-444-5028
10 Sariya Vargas 145 Taxo court. 000-444-5927

5 rows selected.
SQL AND and OR Operators
SQL AND and OR operators use for filter the records based on more than one condition.

SQL AND condition use to test more then one conditions in INSERT, UPDATE, DELETE, SELECT statement. AND
operator work is test first condition if true come to a second and so forth, otherwise not check next condition.

SQL AND Operator filter the record based table data. INSERT, UPDATE, DELETE, SELECT statement perform only
when all specified condition TRUE.

Syntax
Considering following general syntax,
WHERE condition_1
AND condition_2
AND condition_3
...
AND condtion_N;

Example
SELECT Statement: Select table data only condition matched data,
SQL> SELECT * FROM users_info WHERE name = 'Max Miller' AND no = 2;

NO NAME ADDRESS CONTACT_NO


--- ------------------- --------------------------- --------------
2 Max Miller 41 NEW ROAD. 000-444-8292

INSERT Statement: AND operator use in INSERT statement, INSERT only condition matched data,
SQL> INSERT INTO userinfo (no, name, address)
SELECT no, name, address FROM users_info WHERE no = 1 AND name = 'Opal Kole';

1 row inserted.

UPDATE Statement: AND operator use in UPDATE statement, Update only condition matched rows,
SQL> UPDATE users_info SET address = '145 Taxo court.' WHERE no = 10 AND name = 'Sariya Vargas';

1 row updated.

DELETE Statement: AND operator use in DELETE statement, DELETE only condition matched rows but in this example
no is find but name 'Sariya' not exist in table so 0 row deleted.
SQL> DELETE FROM users_info WHERE no = 10 AND name = 'Sariya';

0 row deleted.
SQL OR Condition Statement
SQL OR Condition use to test more then one conditions in INSERT, UPDATE, DELETE, SELECT statement. OR operator
test all condition even if condition TRUE or FALSE. And return data when any one of the condition TRUE.

SQL OR Operator same as AND operator, return the record base filtered data. INSERT, UPDATE, DELETE, SELECT
statement perform only one of the specified condition TRUE.

Syntax
Considering following general syntax,
WHERE condition_1
OR condition_2
OR condition_3
...
OR condtion_N;

Example
SELECT Statement: Select table data if any one of the condition true. In this example select all table data, whose name
'Max Miller' and no is 3. Both are different condition and found in table so return both satisfied condition data.
SQL> SELECT * FROM users_info WHERE name = 'Max Miller' OR no = 3;

NO NAME ADDRESS CONTACT_NO


--- ------------------- --------------------------- --------------
2 Max Miller 41 NEW ROAD. 000-444-8292
3 Beccaa Moss 2500 green city. 000-444-7586

INSERT Statement: OR operator use in INSERT statement, INSERT when any one of the condition TRUE, In this example
new record inserted from another table only those rows whose no column value is 1 OR name value 'Max Miller'.
SQL> INSERT INTO userinfo (no, name, address)
SELECT no, name, address FROM users_info WHERE no = 1 OR name = 'Max Miller';

2 row inserted.

UPDATE Statement: OR operator use in UPDATE statement, Update when any one if the condition TRUE, In this example
address update only those rows whose no column value is 10 OR name value 'Sariya'.
SQL> UPDATE users_info SET address = '145 Taxo court.' WHERE no = 10 OR name = 'Sariya';

1 row updated.

DELETE Statement: AND operator use in DELETE statement, DELETE only condition matched rows but in this example
no is find but name 'Sariya' not exist in table so 0 row deleted.
SQL> DELETE FROM users_info WHERE no = 10 AND name = 'Sariya';

0 row deleted.

SQL BETWEEN Operator


SQL BETWEEN operator used for fetching within range data. SQL BETWEEN query simply a shorthand way of
expressing an inclusive range comparison.

SQL Between operator support only range type value like number, dates, character. But not supporting boolean, string
value range.

Syntax

Consider following syntax that help you to understanding BETWEEN clause,


SELECT * FROM table_name
WHERE column_name
BETWEEN lower_bound_value AND upper_bound_value;

When SQL query with BETWEEN clause parse into SQL Buffer, it will automatically expand out into separate comparison
clauses.
SELECT * FROM table_name
WHERE column_name <= lower_bound_value
AND column_name >= upper_bound_value;

Example
In this example select all users_info table rows with range between no column value 1 to 5.
SQL> SELECT * FROM users_info WHERE no BETWEEN 1 AND 5;

NO NAME ADDRESS CONTACT_NO BIRTH_DATE


--- --------------------- ------------------------- --------------- -------------
1 Opal Kole 63 street Ct. 000-444-7847 1984-05-07
2 Max Miller 41 NEW ROAD. 000-444-8292 1987-07-07
3 Beccaa Moss 2500 green city. 000-444-7586 1992-01-15
4 Paul Singh 1343 Prospect St 000-444-7585 1998-07-25
5 Ken Myer 137 Clay Road 000-444-7528 2002-04-07

SQL BETWEEN clause with DATE


In this example select all users_info table rows with range between birth_date column value '1990-01-01' to '2000-12-31'.
SQL> SELECT * FROM users_info WHERE birth_date BETWEEN '1990-01-01' AND '2000-12-31';

NO NAME ADDRESS CONTACT_NO BIRTH_DATE


--- --------------------- ------------------------- --------------- -------------
3 Beccaa Moss 2500 green city. 000-444-7586 1992-01-15
4 Paul Singh 1343 Prospect St 000-444-7585 1998-07-25
6 Jack Evans 1365 Grove Way 000-444-8401 1993-04-24
8 Gabe Hee 1220 Dallas Drive 000-444-5028 1992-08-27
9 Ben Mares 101 Candy Road 000-444-5928 1999-11-24

5 rows selected.

SQL BETWEEN clause with NOT IN


In this example select all users_info table rows with range NOT IN between no column value 1 to 5. Use only NOT
keyword instead of NOT IN.
SQL> SELECT * FROM users_info WHERE no NOT BETWEEN 6 AND 10;

NO NAME ADDRESS CONTACT_NO BIRTH_DATE


--- --------------------- ------------------------- --------------- -------------
1 Opal Kole 63 street Ct. 000-444-7847 1984-05-07
2 Max Miller 41 NEW ROAD. 000-444-8292 1987-07-07
3 Beccaa Moss 2500 green city. 000-444-7586 1992-01-15
4 Paul Singh 1343 Prospect St 000-444-7585 1998-07-25
5 Ken Myer 137 Clay Road 000-444-7528 2002-04-07

5 rows selected.

SQL IN and NOT IN Operators


SQL IN and NOT IN operators used to specify multiple values in a WHERE clause.
SQL IN condition used to allow multiple value in a WHERE clause condition. SQL IN condition you can use when you
need to use multiple OR condition.

SQL IN condition allow only specific value in INSERT, UPDATE, DELETE, SELECT statement.

Syntax
WHERE column_name IN (value1, value2, ...);

Example
SQL> SELECT * FROM users_info WHERE no IN (1,5,10);

NO NAME ADDRESS CONTACT_NO


---- -------------------- ------------------------ --------------------
1 Opal Kole 63 street Ct. 000-444-7847
5 Ken Myer 137 Clay Road 000-444-7528
10 Sariya Vargas 145 Taxo court. 000-444-5927

3 rows selected.

SQL NOT IN Condition Statement


SQL NOT IN condition used to exclude the defined multiple value in a WHERE clause condition. SQL NOT IN condition
also identify by NOT operator.

Syntax
NOT IN condition use with WHERE clause to exclude defined multiple values from record data.
WHERE column_name NOT IN (value1, value2, ...);

SQL NOT condition used with BETWEEN condition or LIKE condition.


NOT BETWEEN condition;
NOT LIKE condition

SQL WHERE clause with NOT IN Example


SQL> SELECT * FROM users_info WHERE no NOT IN (1,3,5,7,9);

NO NAME ADDRESS CONTACT_NO


---- -------------------- ------------------------ --------------
2 Max Miller 41 NEW ROAD. 000-444-8292
4 Paul Singh 1343 Prospect St 000-444-7585
6 Jack Evans 1365 Grove Way 000-444-8401
8 Gabe Hee 1220 Dallas Drive 000-444-5028
10 Sariya Vargas 145 Taxo court. 000-444-5927

5 rows selected.

SQL BETWEEN with NOT Example

Our users_info table we have to select all rows with add condition exclude no column range from 6 to 10.
SQL> SELECT * FROM users_info WHERE no NOT BETWEEN 6 AND 10;

NO NAME ADDRESS CONTACT_NO BIRTH_DATE


--- --------------------- ------------------------- --------------- -------------
1 Opal Kole 63 street Ct. 000-444-7847 1984-05-07
2 Max Miller 41 NEW ROAD. 000-444-8292 1987-07-07
3 Beccaa Moss 2500 green city. 000-444-7586 1992-01-15
4 Paul Singh 1343 Prospect St 000-444-7585 1998-07-25
5 Ken Myer 137 Clay Road 000-444-7528 2002-04-07

5 rows selected.

SQL LIKE with NOT Example

In our users_info table not select whose name column starting with 'Be' character.
SQL> SELECT * FROM users_info WHERE name NOT LIKE 'Be%';

NO NAME ADDRESS CONTACT_NO


--- --------------------- ------------------------------- ---------------
1 Opal Kole 63 street Ct. 000-444-7847
2 Max Miller 41 NEW ROAD. 000-444-8292
4 Paul Singh 1343 Prospect St 000-444-7585
5 Ken Myer 137 Clay Road 000-444-7528
6 Jack Evans 1365 Grove Way 000-444-8401
7 Reed Koch 1274 West Street 000-444-5228
8 Gabe Hee 1220 Dallas Drive 000-444-5028
10 Sariya Vargas 145 Taxo court. 000-444-5927

8 rows selected.

SQL LIKE Operator


SQL LIKE operator is used with WHERE clause to matches specific pattern in a column. SQL LIKE condition apply on
table column data.

Following two wildcards are often used with the LIKE operator

 % - Represents zero, or any number of characters


 _ - Represents a single character

Syntax

Considering following SQL LIKE condition syntax,


SELECT * FROM table_name
WHERE column_name LIKE 'pattern';

Pattern write inside opening or closing single delimit characters (''). Do not use double delimit characters ("") because
double delimit use as delimiter identifier.
...WHERE column_name LIKE 'pattern'; -- Correct way
...WHERE column_name LIKE "pattern"; -- Incorrect way
SQL Wildcards Characters
SQL Wildcards characters used for searching string pattern. SQL Wildcard character use with LIKE condition for
searching pattern string from database table.

Following Wildcard characters pattern use for certain type expression,

Wildcard Description

_ Underscore sign matches any exactly single character with in string.


(underscore sign) Example. 'Op_l Kole', '_pal Kole', 'Opal Kol_'

% Percentage sign matches any number of characters (0 or more characters).


(Percentage sign) Example. 'Op%', '%Kole', 'Opal%'

SQL LIKE Example (with WILDCARD)


Considering following user_info table is our example table.
SQL> SELECT * FROM users_info;

NO NAME ADDRESS CONTACT_NO


--- --------------------- ------------------------------- ---------------
1 Opal Kole 63 street Ct. 000-444-7847
2 Max Miller 41 NEW ROAD. 000-444-8292
3 Beccaa Moss 2500 green city. 000-444-7586
4 Paul Singh 1343 Prospect St 000-444-7585
5 Ken Myer 137 Clay Road 000-444-7528
6 Jack Evans 1365 Grove Way 000-444-8401
7 Reed Koch 1274 West Street 000-444-5228
8 Gabe Hee 1220 Dallas Drive 000-444-5028
9 Ben Mares 101 Candy Road 000-444-5928
10 Sariya Vargas 145 Taxo court. 000-444-7454

10 rows selected.

LIKE with _ WILDCARD Character Example

SQL LIKE condition with _ (underscore) WILDCARD character to matches any exactly single character with in string.
SQL> SELECT * FROM users_info WHERE name LIKE 'Pa_l S__gh';

NO NAME ADDRESS CONTACT_NO


--- --------------------- ------------------------------- ---------------
4 Paul Singh 1343 Prospect St 000-444-7585
1 rows selected.

LIKE with % WILDCARD Character Example

SQL LIKE condition with % (percentage) WILDCARD character to matches any number of characters.
SQL> SELECT * FROM users_info WHERE name LIKE 'Paul%';

NO NAME ADDRESS CONTACT_NO


--- --------------------- ------------------------------- ---------------
4 Paul Singh 1343 Prospect St 000-444-7585

1 rows selected.

LIKE (_ WILDCARD Character) with NOT condition Example

SQL LIKE (_ WILDCARD character) with NOT condition to return all record only exclude pattern result.
SQL> SELECT * FROM users_info WHERE name NOT LIKE 'Pa_l S__gh';

NO NAME ADDRESS CONTACT_NO


--- --------------------- ------------------------------- ---------------
1 Opal Kole 63 street Ct. 000-444-7847
2 Max Miller 41 NEW ROAD. 000-444-8292
3 Beccaa Moss 2500 green city. 000-444-7586
5 Ken Myer 137 Clay Road 000-444-7528
6 Jack Evans 1365 Grove Way 000-444-8401
7 Reed Koch 1274 West Street 000-444-5228
8 Gabe Hee 1220 Dallas Drive 000-444-5028
9 Ben Mares 101 Candy Road 000-444-5928
10 Sariya Vargas 145 Taxo court. 000-444-7454

9 rows selected.

LIKE (% WILDCARD Character) with NOT condition Example

SQL LIKE (% WILDCARD character) with NOT condition to return all record only exclude pattern result.
SQL> SELECT * FROM users_info WHERE name NOT LIKE 'Paul%';

NO NAME ADDRESS CONTACT_NO


--- --------------------- ------------------------------- ---------------
1 Opal Kole 63 street Ct. 000-444-7847
2 Max Miller 41 NEW ROAD. 000-444-8292
3 Beccaa Moss 2500 green city. 000-444-7586
5 Ken Myer 137 Clay Road 000-444-7528
6 Jack Evans 1365 Grove Way 000-444-8401
7 Reed Koch 1274 West Street 000-444-5228
8 Gabe Hee 1220 Dallas Drive 000-444-5028
9 Ben Mares 101 Candy Road 000-444-5928
10 Sariya Vargas 145 Taxo court. 000-444-7454

9 rows selected.

SQL Constraints
What is SQL constraints? SQL Constraints are the rules which are apply to table columns to store valid data and prevents
the user to storing/entering invalid data into table columns.

SQL Constraints are part of a database schema definition.

We can create/define constraints on single or multiple columns of any table. It maintain the data integrity of the table.

SQL Constraints are help us to enter limited fixed size data. For example postal code India country size six digit no below
or no more then six digit.
Type of Data constraints
 Input/Output constraints: This constraints determines the speed of which data are inserted or extracted from database table.
For example Primary key, Foreign key constraints.
 Business Rule constraints: This rules are applied to data prior(first) the data being inserted into the table columns. For
example Unique, Not NULL, Default constraints.

Define Constraints
Oracle SQL allows programmers to define constraints at:

 Column level
 Table level

Column level constraints: Column level constraint define on column level (single column) along with other column
attributes.

Syntax
...
column_name datatype[(size)] [ NULL | NOT NULL ] [constraint_name],
....

Table level constraints: Table level constraint defining after the all table columns define.

Syntax
...
column_name datatype[(size)] [ NULL | NOT NULL ],
column_name datatype[(size)] [ NULL | NOT NULL ],
[ CONSTRAINT constraint_name
PRIMARY KEY ( col1, col2, ... ) |
FOREIGN KEY ( col1, col2, ... ) REFERENCES table_2 [ ( col1, col2, ... )
[ ON UPDATE | ON DELETE
[ NO ACTION | SET NULL | SET DEFAULT | CASCADE ]
]
] |
UNIQUE ( col1, col2, ... ) |
CHECK ( expression )
],
...
ADD CONSTRAINT (ALTER TABLE)
Yes you can add constraint in existing table. But in this way when you adding new constraint, oracle check any existing
data violate the constraint rules or not. If not violate constraint added successfully otherwise you have to manually update
data to prevent constraint violating.

Syntax
ALTER TABLE table_name
ADD CONSTRAINT_NAME (column_name);

DROP CONSTRAINT (ALTER TABLE)


You can drop the existing table constraint using ALTER TABLE statement.

Syntax:
ALTER TABLE table_name
DROP constraint_name column_name;

Type of SQL Constraints


 PRIMARY KEY: value in specified column must be unique for each row in a table and not a NULL. Primary key used to
identify individual records.
 FOREIGN KEY: value in specified column must have reference in another table (That existing record have primary key or
any other constraint).
 NOT NULL: Column value must not be a NULL.
 UNIQUE: Check column value must be unique across the given field in table.
 CHECK: Specific condition is specified, which must evaluate to true for constraint to be satisfied.
 DEFAULT: Default value assign if none of the value specified of given field.

SQL PRIMARY KEY Constraint


SQL PRIMARY KEY Constraint apply on column(s) for a uniquely identifies each record (row) in the table.

SQL Primary Key constraint has been specified for certain column. we can not enter duplicate data in this column.

SQL Primary Key in a table have following three special attributes,

 The NOT NULL attribute is automatic active.


 The data across the column must be unique.
 Defines column as a mandatory column.

Simple Key vs Composite Key


Simple key: In table only one column (single column) apply primary key is known as simple primary key.
Composite key: Where as composite primary key is opposite apply primary key in multicolumn called a composite primary
key.

Defined at Column level


Define primary key at column level with other column attributes.

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ] PRIMARY KEY,
column_name datatype[(size)] [ NULL | NOT NULL ] PRIMARY KEY,
....
);

Example
SQL> CREATE TABLE emp_info(
no NUMBER(3,0) PRIMARY KEY,
name VARCHAR(30),
address VARCHAR(70),
contact_no VARCHAR(12)
);

Table created.
Defined at Table level
Same thing primary key apply in table level. Table level you can define multiple column separated by comma (,).

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ],
column_name datatype[(size)] [ NULL | NOT NULL ],
...,
PRIMARY KEY ( column_name, ... ),
...
);

Example
SQL> CREATE TABLE emp_info(
no NUMBER(3,0),
name VARCHAR(30),
address VARCHAR(70),
contact_no VARCHAR(12),
PRIMARY KEY(no)
);

Table created.
You can also specifies CONSTRAINT keyword to specify the constraint name.

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ],
column_name datatype[(size)] [ NULL | NOT NULL ],
...,
CONSTRAINT pk_constraint_name PRIMARY KEY ( column_name, ... ),
...
);

Example
SQL> CREATE TABLE emp_info(
no NUMBER(3,0),
name VARCHAR(30),
address VARCHAR(70),
contact_no VARCHAR(12),
CONSTRAINT pk_no PRIMARY KEY(no)
);

Table created.
ADD PRIMARY KEY (ALTER TABLE)
ALTER TABLE statement to add primary key in existing table. But in this way when you adding new constraint, oracle
check any existing data violate the primary key constraint or not. If not violate constraint added successfully otherwise you
have to manually update data to prevent primary key constraint violating.

Syntax
ALTER TABLE table_name
ADD PRIMARY KEY (column_name, ...);

Example
SQL> ALTER TABLE emp_info ADD PRIMARY KEY (no);

Table altered.

DROP PRIMARY KEY (ALTER TABLE)


ALTER TABLE statement to drop primary key in existing table.

Syntax
ALTER TABLE table_name
DROP PRIMARY KEY;

Example
SQL> ALTER TABLE emp_info DROP PRIMARY KEY;

Table altered.

In above SQL statement you should notice for dropping PRIMARY KEY constraint in all primary key column(s). You can
not drop individual column. It's this possible in following way, You should specify the constraint name and ALTER TABLE
statement you can drop constraint name.

Example
SQL> CREATE TABLE emp_info(
no NUMBER(3,0),
name VARCHAR(30),
address VARCHAR(70),
contact_no VARCHAR(12),
CONSTRAINT pk_no PRIMARY KEY(no)
);

Table created.

SQL> ALTER TABLE emp_info DROP CONSTRAINT pk_no;

Table altered.
SQL FOREIGN KEY Constraint
SQL FOREIGN KEY Constraint apply on column(s) for whose value must have reference in another table column (that
existing column must be primary key or unique key constraint).

SQL FOREIGN KEY constraints also known as relationship (referential) constraints.

SQL FOREIGN KEY constraints reference in another table within the same database. Using PL/SQL Trigger you can
implement to cross database referencing.

FOREIGN KEY constraint applied column must have same data type as the reference on another table column.

FOREIGN KEY Constraint Rules


SQL Foreign key constraint valid only if following any one condition true,

 Value must have exist in parent column (referenced another table column).
 Value may be store in NULL.
Define on Column level
A FOREIGN KEY constraint specify at column level.

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ] REFERENCES another_table_name(column_name)
[ ON UPDATE | ON DELETE
[ NO ACTION | SET NULL | SET DEFAULT | CASCADE ]
],
....
);

Example
SQL> CREATE TABLE emp_info(
no NUMBER(3,0) PRIMARY KEY,
name VARCHAR(30),
address VARCHAR(70),
contact_no NUMBER(12,0)
);
Table created.

SQL> CREATE TABLE emp_salary(


no NUMBER(3,0) PRIMARY KEY,
users_no NUMBER(3,0) REFERENCES emp_info(no),
salary NUMBER(12)
);

Table created.

Run it...    »

Define on Table level


A FOREIGN KEY constraint specify at table level.

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ],
column_name datatype[(size)] [ NULL | NOT NULL ],
...,
CONSTRAINT fk_constraint_name
FOREIGN KEY (column_name) REFERENCES another_table_name(column_name)
[ ON UPDATE | ON DELETE
[ NO ACTION | SET NULL | SET DEFAULT | CASCADE ]
],
...
);

Example
SQL> CREATE TABLE emp_info(
no NUMBER(3,0) PRIMARY KEY,
name VARCHAR(30),
address VARCHAR(70),
contact_no NUMBER(12,0)
);

Table created.

SQL> CREATE TABLE emp_salary(


no NUMBER(3,0) PRIMARY KEY,
users_no NUMBER(3,0),
salary NUMBER(12),
CONSTRAINT fk_userno FOREIGN KEY (users_no) REFERENCES emp_info(no)
);

Table created.
Composite FOREIGN KEY Constraint
Composite FOREIGN KEY constraint specified one or more columns reference in another table columns separated by
comma (,).

Syntax
REFERENCES another_table_name(column_name1, column_name2)

ADD FOREIGN KEY (ALTER TABLE)


ALTER TABLE statement to add foreign key in existing table. But in this way when you adding foreign key constraint, SQL
check any existing data violate the foreign key constraint or not. If not violate constraint added successfully otherwise you
have to update invalid data to prevent foreign key constraint violating.

Syntax
ALTER TABLE table_name
ADD FOREIGN KEY (column_name, ...) REFERENCES another_table_name(column_name, ...);

Example
SQL> ALTER TABLE emp_salary ADD FOREIGN KEY (users_no) REFERENCES emp_info(no);

Table altered.
DROP FOREIGN KEY (ALTER TABLE)
ALTER TABLE statement to drop foreign key in existing table.

Syntax
ALTER TABLE table_name
DROP CONSTRAINT fk_constraint_name;

Example
SQL> ALTER TABLE emp_salary DROP CONSTRAINT fk_userno;

Table altered.

SQL NOT NULL Constraint


SQL NOT NULL Constraint enforces to a column is always contain a value. This means thats you can not insert NULL
(blank) value in this field.

SQL NOT NULL Constraint applied only at column level. You should manually define NOT NULL constraint because table
column set NULL value.
SQL NOT NULL Constraint Rules
 A NULL values is different from a blank or zero.
 A NULL value can be inserted into the columns of any Datatype.

Datatype Action Values

NUMBER Error Produce blank, space, special character, NULL

NUMBER Allow Only O or 1 to 9

VARCHAR2 Error Produce blank, space, special character, NULL


VARCHAR2 blank NULL, space, special character

NOT NULL Defined only at Column level


Define NOT NULL constraint at column level with other column attributes,

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] NOT NULL,
column_name datatype[(size)] NOT NULL,
....
);

Example
SQL> CREATE TABLE emp_info(
no NUMBER(3,0) PRIMARY KEY,
name VARCHAR(30) NOT NULL,
address VARCHAR(70)
);

Table created.

SQL> DESCRIBE emp_info;


Name Null? Type
---------------------------- --------- ----------------------------
NO NOT NULL NUMBER(3)
NAME NOT NULL VARCHAR2(30)
ADDRESS VARCHAR2(70)

ADD NOT NULL constraint (ALTER TABLE)


ALTER TABLE statement to add NOT NULL constraint in existing table column.

Syntax
ALTER TABLE table_name
MODIFY column_name datatype[(size)] NOT NULL;

Example
SQL> ALTER TABLE emp_info MODIFY address VARCHAR2(70) NOT NULL;

Table altered.

SQL> DESCRIBE emp_info;


Name Null? Type
---------------------------- --------- ----------------------------
NO NOT NULL NUMBER(3)
NAME NOT NULL VARCHAR2(30)
ADDRESS NOT NULL VARCHAR2(70)

DROP NOT NULL constraint (ALTER TABLE)


ALTER TABLE statement to drop NOT NULL constraint in existing table column.

Syntax
ALTER TABLE table_name
MODIFY column_name datatype[(size)] NULL;

Example
SQL> ALTER TABLE emp_info MODIFY address VARCHAR2(70) NULL;

Table altered.

SQL> DESCRIBE emp_info;


Name Null? Type
---------------------------- --------- ----------------------------
NO NOT NULL NUMBER(3)
NAME NOT NULL VARCHAR2(30)
ADDRESS VARCHAR2(70)

SQL UNIQUE Constraint


SQL UNIQUE constraint check column value must be unique across the given field in table.

Primary key characteristics automatic include UNIQUE key constraint that can not store duplicated by any other row.

Note: You can set many UNIQUE constraints per table.

Difference Between Primary Key and Unique Key


Primary Key Unique Key
Primary Key does not allow NULL(blank) values. Whereas Unique key allow NULL(blank) values.
PRIMARY KEY = UNIQUE + NOT NULL UNIQUE KEY = UNIQUE + NULL

Defined at Column level


Define unique key at column level with other column attributes.

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ] UNIQUE,
column_name datatype[(size)] [ NULL | NOT NULL ] UNIQUE,
....
);

Example Statement
SQL> CREATE TABLE stu_info(
no NUMBER(3,0) PRIMARY KEY,
name VARCHAR(30) UNIQUE,
address VARCHAR(70),
contact_no VARCHAR(12)
);

Table created.
Defined at Table level
Unique key apply in table level. Table level you can define multiple column separated by comma (,).

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ],
column_name datatype[(size)] [ NULL | NOT NULL ],
...,
UNIQUE ( column_name, ... ),
...
);

Example
SQL> CREATE TABLE stu_info(
no NUMBER(3,0) PRIMARY KEY,
name VARCHAR(30),
address VARCHAR(70),
contact_no VARCHAR(12),
UNIQUE(name)
);

Table created.
You can also specifies CONSTRAINT keyword to specify the constraint name.

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ],
column_name datatype[(size)] [ NULL | NOT NULL ],
...,
CONSTRAINT uk_constraint_name UNIQUE ( column_name, ... ),
...
);

Example
SQL> CREATE TABLE stu_info1(
no NUMBER(3,0) PRIMARY KEY,
name VARCHAR(30),
address VARCHAR(70),
contact_no VARCHAR(12),
CONSTRAINT uk_name UNIQUE(name)
);

Table created.
Now we are inserting record into this table to check unique key how to work for example try to insert same name as soon
as fire error for unique constraint violated.
SQL> INSERT INTO stu_info(no,name) VALUES(1,'Opal');

1 row created.

SQL> INSERT INTO stu_info(no,name) VALUES(2,'Opal');


INSERT INTO stu_info(no,name) VALUES(2,'Opal')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.SYS_C004081) violated.

ADD UNIQUE constraint (ALTER TABLE)


ALTER TABLE statement to add UNIQUE constraint in existing table column.

Syntax
ALTER TABLE table_name
ADD CONSTRAINT uk_constraint_name UNIQUE (column_name, ...);
Example
SQL> ALTER TABLE stu_info1 ADD CONSTRAINT uk_name UNIQUE(name);

Table altered.

DROP UNIQUE constraint (ALTER TABLE)


ALTER TABLE statement to drop UNIQUE constraint in existing table column.

Syntax
ALTER TABLE table_name
DROP CONSTRAINT uk_constraint_name;

Example
SQL> ALTER TABLE stu_info1 DROP CONSTRAINT uk_name;

Table altered.

SQL CHECK Constraint


SQL CHECK Constraint check specified specific condition, which must evaluate to true for constraint to be satisfied.

SQL CHECK Constraint must be specified as a logical expression that evaluated specific condition either TRUE or
FALSE.

SQL CHECK constraint define at column level that allow only specified values for a column and Whereas define at table
level that allow specified values as well as use other column value for checking specific condition.

CHECK CONSTRAINT defined at Column level


Define CHECK constraint at column level with other column attributes,

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ] CHECK (column_name condition),
column_name datatype[(size)] [ NULL | NOT NULL ] CHECK (column_name condition),
....
);
Example
SQL> CREATE TABLE student_info(
no NUMBER(3) PRIMARY KEY,
stu_code VARCHAR(10) CHECK (stu_code like 'j%'),
name VARCHAR(30) CHECK ( name = upper(name) ),
city VARCHAR(30) CHECK (city IN ('Houston','San Antonio','Boston','Miami'))
scholarship NUMBER(5) CHECK (scholarship BETWEEN 5000 AND 20000)
);

Table created.

SQL> INSERT INTO student_info VALUES (1,'j001', 'JAMES KENON', 'Miami', 8900);

1 row created.

We are create new student_info table name with following check constraints:

 Values inserted into stu_code column must be start with the lower letter 'j'.
 Values inserted into name column must be capitalize.
 Values inserted into city column only allow 'Houston','San Antonio','Boston','Miami' as valid legitimate values.
 Values inserted into scholarship column between 5000 and 20000.
CHECK CONSTRAINT defined at Table level
CHECK constraint apply in table level. Table level you must specified separately.

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ],
column_name datatype[(size)] [ NULL | NOT NULL ],
...,
CHECK ( column_name condition),
CHECK ( column_name condition),
...
);

Example
SQL> CREATE TABLE student_info(
no NUMBER(3) PRIMARY KEY,
stu_code VARCHAR(10),
name VARCHAR(30),
city VARCHAR(30),
scholarship NUMBER(5),
CHECK (stu_code like 'j%'),
CHECK (name = upper(name)),
CHECK (city IN ('Houston','San Antonio','Boston','Miami')),
CHECK (scholarship BETWEEN 5000 AND 20000)
);

Table created.

SQL> INSERT INTO student_info VALUES (1,'j001', 'JAMES KENON', 'Miami','8900');

1 row created.

You can also specifies CONSTRAINT keyword to specify the constraint name. Specifies constraint name is recommended
way for creating table with CHECK constraint.

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ],
column_name datatype[(size)] [ NULL | NOT NULL ],
...,
CONSTRAINT check_constraint_name CHECK ( column_name condition),
CONSTRAINT check_constraint_name CHECK ( column_name condition),
...
);

Example
SQL> CREATE TABLE student_info(
no NUMBER(3) PRIMARY KEY,
stu_code VARCHAR(10),
name VARCHAR(30),
city VARCHAR(30),
scholarship NUMBER(5),
CONSTRAINT check_stucode CHECK (stu_code like 'j%'),
CONSTRAINT check_name CHECK (name = upper(name)),
CONSTRAINT check_city CHECK (city IN ('Houston','San Antonio','Boston','Miami')),
CONSTRAINT check_scholarship CHECK (scholarship BETWEEN 5000 AND 20000)
);

Table created.

ADD CHECK constraint (ALTER TABLE)


ALTER TABLE statement to add CHECK constraint in existing table column.

Syntax
ALTER TABLE table_name
ADD CONSTRAINT check_constraint_name CHECK (column_name condition);
Example
SQL> ALTER TABLE student_info ADD CONSTRAINT check_name CHECK (name = upper(name));

Table altered.

DROP CHECK constraint (ALTER TABLE)


ALTER TABLE statement to drop CHECK constraint in existing table column.

Syntax
ALTER TABLE table_name
DROP CONSTRAINT check_constraint_name;

Example
SQL> ALTER TABLE student_info DROP CONSTRAINT check_name;

Table altered.

SQL DEFAULT Constraint


SQL DEFAULT Constraint assign default value if none of the value specified of given field.

SQL DEFAULT constraint specified only at column level.


When you inserting data does not provide any specific value automatically assign default value only if you specified
DEFAULT constraint.

DEFAULT CONSTRAINT defined only at Column level


Specify DEFAULT constraint at column level with other column attributes.

Syntax
CREATE TABLE table_name(
column_name datatype[(size)] [ NULL | NOT NULL ] DEFAULT default_value,
column_name datatype[(size)] [ NULL | NOT NULL ] DEFAULT default_value,
....
);

Example
SQL> CREATE TABLE stu_info(
no NUMBER(3,0) PRIMARY KEY,
name VARCHAR(30) NOT NULL,
std VARCHAR(18) DEFAULT 'M.Sc.(CS)',
fees_pay NUMBER(5) DEFAULT 2000
);

Table created.
ADD DEFAULT constraint (ALTER TABLE)
ALTER TABLE statement to add DEFAULT constraint in existing table column.

Syntax
ALTER TABLE table_name
MODIFY column_name datatype[(size)] [ NOT NULL ] DEFAULT default_value;

Example
SQL> ALTER TABLE stu_info MODIFY std VARCHAR(18) DEFAULT 'M.Sc.(CS)';

Table altered.

DROP DEFAULT constraint (ALTER TABLE)


ALTER TABLE statement to drop DEFAULT constraint in existing table column. you need to just redefine (or modify)
column attribute.

Syntax
ALTER TABLE table_name
MODIFY column_name datatype[(size)];
Example
SQL> ALTER TABLE emp_info MODIFY std VARCHAR2(18);

Table altered.

Above statement execute on oracle SQL that return to table altered but still default value assigned if not specify any
specific value.

SQL GROUP BY
SQL GROUP BY clause use with SELECT statement for fetching data (result groups) according to a matching values for
one or more columns.

SELECT statement fetching all of the rows together that have specific data in specific columns for applying GROUP BY
clause on one or more columns.

Consider following syntax that help you to understanding GROUP BY clause.

Syntax
SELECT
column_name1, column_name2, aggregate_function(column_name), ....
FROM table_name
[ WHERE condition ]
GROUP BY column_name1, ...;
Example

We have following employee_hour table that store weekday hours for each employee.


SQL> SELECT * FROM employee_hour;

NAME DAY HOURS


-------------------- ---------- ----------
Opal Kole Monday 8
Max Miller Monday 8
Beccaa Moss Monday 8
Paul Singh Monday 9
Opal Kole Tuesday 9
Max Miller Tuesday 6
Beccaa Moss Tuesday 10
Paul Singh Tuesday 8
Opal Kole Wednesday 7
Max Miller Wednesday 9
Beccaa Moss Wednesday 11
Paul Singh Wednesday 12

12 rows selected.

Run it...    »
Example
SQL> SELECT name
FROM employee_hour
GROUP BY name;

NAME
--------------------
Opal Kole
Beccaa Moss
Paul Singh
Max Miller

Run it...    »

GROUP By clause apply only single column that return group of employee name.

Example
SQL> SELECT name,SUM(hours)
FROM employee_hour
GROUP BY name;

NAME SUM(HOURS)
-------------------- ----------
Opal Kole 24
Beccaa Moss 29
Paul Singh 29
Max Miller 23

Run it...    »

GROUP By clause apply on name column with use SUM aggregate function to summation the total working hours.

Example
SQL> SELECT name,AVG(hours)
FROM employee_hour
GROUP BY name;

NAME AVG(HOURS)
-------------------- ----------
Opal Kole 8
Beccaa Moss 9.66666667
Paul Singh 9.66666667
Max Miller 7.66666667

Run it...    »

GROUP By clause apply on name column with use AVG aggregate function to average the total working hours divide by
number of day.

SQL HAVING Clause


SQL HAVING Clause statement used with GROUP BY clause for filtering the GROUP BY clause result set data allow only
group of result whose HAVING clause condition TRUE.

SQL HAVING Clause use with GROUP BY clause. without GROUP BY clause you can't use HAVING clause.

Syntax

Considering following syntax that help you to understanding HAVING clause,


SELECT
column_name1, column_name2, aggregate_function(column_name), ....
FROM table_name
[ WHERE condition ]
GROUP BY column_name1, ...
HAVING condition;

Example

We have following employee_hour table that store weekday hours for each employee:


SQL> SELECT * FROM employee_hour;

NAME DAY HOURS


-------------------- ---------- ----------
Opal Kole Monday 8
Max Miller Monday 8
Beccaa Moss Monday 8
Paul Singh Monday 9
Opal Kole Tuesday 9
Max Miller Tuesday 6
Beccaa Moss Tuesday 10
Paul Singh Tuesday 8
Opal Kole Wednesday 7
Max Miller Wednesday 9
Beccaa Moss Wednesday 11
Paul Singh Wednesday 12

12 rows selected.

Run it...    »

Example
SQL> SELECT name,SUM(hours)
FROM employee_hour
GROUP BY name
HAVING SUM(hours) > 25;

NAME SUM(HOURS)
-------------------- ----------
Beccaa Moss 29
Paul Singh 29
Run it...    »
GROUP By clause apply on name column with use SUM aggregate function to summation the total working hours. Also
with HAVING clause condition allow only sum of hours grater than 25.

Example
SQL> SELECT name,AVG(hours)
FROM employee_hour
GROUP BY name
HAVING AVG(hours) > 8;

NAME AVG(HOURS)
-------------------- ----------
Beccaa Moss 9.66666667
Paul Singh 9.66666667

Run it...    »

GROUP By clause apply on name column with use AVG aggregate function to average the total working hours divide by
number of day. Also with HAVING clause condition allow only avg of hours grater than 8.

SQL ORDER BY
SQL ORDER BY Clause used to sorting SQL result set either ascending or descending order.

SQL ORDER BY Clause default record set sorting ascending order. Using DESC keyword to result set sorting in
descending order.
Syntax

Considering following syntax that help you to understanding ORDER BY clause,


SELECT
column_name1, column_name2, ...
FROM table_name
[ WHERE condition ]
ORDER BY column_name1, column_name2 [ ASC | DESC ];

Example

We have following employee_hour table that store weekday hours for each employee:


SQL> SELECT * FROM employee_hour;

NAME DAY HOURS


-------------------- ---------- ----------
Opal Kole Monday 8
Max Miller Monday 8
Beccaa Moss Monday 8
Paul Singh Monday 9
Opal Kole Tuesday 9
Max Miller Tuesday 6
Beccaa Moss Tuesday 10
Paul Singh Tuesday 8
Opal Kole Wednesday 7
Max Miller Wednesday 9
Beccaa Moss Wednesday 11
Paul Singh Wednesday 12

12 rows selected.

Run it...    »

Example
SQL> SELECT *
FROM employee_hour
ORDER BY hours;

NAME DAY HOURS


-------------------- ---------- ----------
Max Miller Tuesday 6
Opal Kole Wednesday 7
Max Miller Monday 8
Beccaa Moss Monday 8
Opal Kole Monday 8
Paul Singh Tuesday 8
Paul Singh Monday 9
Opal Kole Tuesday 9
Max Miller Wednesday 9
Beccaa Moss Tuesday 10
Beccaa Moss Wednesday 11
Paul Singh Wednesday 12

12 rows selected.

Run it...    »

ORDER BY clause apply on hours column, result set return into ascending order.

Example
SQL> SELECT *
FROM employee_hour
ORDER BY hours DESC;

NAME DAY HOURS


-------------------- ---------- ----------
Paul Singh Wednesday 12
Beccaa Moss Wednesday 11
Beccaa Moss Tuesday 10
Paul Singh Monday 9
Opal Kole Tuesday 9
Max Miller Wednesday 9
Opal Kole Monday 8
Max Miller Monday 8
Paul Singh Tuesday 8
Beccaa Moss Monday 8
Opal Kole Wednesday 7
Max Miller Tuesday 6

12 rows selected.

Run it...    »

Same thing as above but ORDER BY clause apply on hours column, result set return into descending order.

SQL TOP, ROWNUM Clause


SQL TOP Clause fetching only top Nth number of record or Nth percentage record result set return. But 10g onward TOP
Clause no longer supported replace with ROWNUM clause.

Nth number/percentage of record user define, user to specified number/percentage of records return.

SQL TOP clause use always with SELECT statement. When you SELECT statement to select records, but you need only
first 3 records you just use TOP clause to return only first 3 records.

Syntax

Considering following syntax that help you to understanding TOP clause,


SELECT [ TOP number | percentage ]
column_name1, column_name2, ...
FROM table_name;

Example Table

We have following employee_hour table that store weekday hours for each employee:


SQL> SELECT * FROM employee_hour;

NAME DAY HOURS


-------------------- ---------- ----------
Opal Kole Monday 8
Max Miller Monday 8
Beccaa Moss Monday 8
Paul Singh Monday 9
Opal Kole Tuesday 9
Max Miller Tuesday 6
Beccaa Moss Tuesday 10
Paul Singh Tuesday 8
Opal Kole Wednesday 7
Max Miller Wednesday 9
Beccaa Moss Wednesday 11
Paul Singh Wednesday 12

12 rows selected.
Run it...    »

Example
SQL> SELECT TOP 3 *
FROM employee_hour
ORDER BY hours DESC;

NAME DAY HOURS


-------------------- ---------- ----------
Paul Singh Wednesday 12
Beccaa Moss Wednesday 11
Beccaa Moss Tuesday 10

3 rows selected.

I'm not sure this is working or not but I was finding TOP clause work on Oracle 8i, 9i. And now TOP clause replace with
ROWNUM clause to fetch number of rows.

SQL ROWNUM Clause (10g onward)


Syntax

Considering following syntax that help you to understanding ROWNUM clause,


SELECT
column_name1, column_name2, ...
FROM table_name
WHERE ROWNUM operator condition;

Example Statement :
SQL> SELECT *
FROM employee_hour
WHERE ROWNUM <= 3;

NAME DAY HOURS


-------------------- ---------- ----------
Opal Kole Monday 8
Max Miller Monday 8
Beccaa Moss Monday 8

Run it...    »

ROWNUM clause to fetching only top 3 records.

Example
SQL> SELECT *
FROM ( SELECT * FROM employee_hour ORDER BY hours DESC)
WHERE ROWNUM <= 3;

NAME DAY HOURS


-------------------- ---------- ----------
Paul Singh Wednesday 12
Beccaa Moss Wednesday 11
Beccaa Moss Tuesday 10

Run it...    »

ROWNUM clause use with sub query to fetching record hours descending order and filter top 3 records.

SQL Alias
SQL Alias are the temporary names given to table or column for the purpose of a specific SQL query.

SQL Alias name specifies to make table or column name are more readable. Alias assigning is optional not a compulsory.

You can temporary assign another name to a table or a column name for a duration of SELECT query using as alias
name.

When you joining one or more table that time assign alias name of a table or a column name to make easier to read.

Alias Useful When...


 column name is big and complex assign alias name.
 use aggregate function assign alias name.
 JOIN one or more table query structure make more readable assign alias name.
 More than one columns are combined together to assign alias name.

Syntax
Look this syntax column_name AS alias_column_name and table_name alias_table_name no need AS keyword.
Considering following syntax that help you to understanding Alias,
SELECT
column_name [AS alias_column_name], aggregate_function(column_name) [AS alias_name], ...
FROM table_name [ alias_table_name ];

Example Table

We have following employee_hour table that store weekday hours for each employee:


SQL> SELECT * FROM employee_hour;

NAME DAY HOURS


-------------------- ---------- ----------
Opal Kole Monday 8
Max Miller Monday 8
Beccaa Moss Monday 8
Paul Singh Monday 9
Opal Kole Tuesday 9
Max Miller Tuesday 6
Beccaa Moss Tuesday 10
Paul Singh Tuesday 8
Opal Kole Wednesday 7
Max Miller Wednesday 9
Beccaa Moss Wednesday 11
Paul Singh Wednesday 12

12 rows selected.

Example
SQL> SELECT
name AS Employee_Name, SUM(hours) AS Total_Hours
FROM employee_hour emp_hours
GROUP BY name;

EMPLOYEE_NAME TOTAL_HOURS
-------------------- -----------
Opal Kole 24
Beccaa Moss 29
Paul Singh 29
Max Miller 23

SQL Transactions - Commit, Rollback, and Savepoint


SQL Transaction Control Language (TCL) commands are used to manage database transaction. SQL transaction
command use with DML statement for INSERT, UPDATE and DELETE.
DML statement are store into SQL buffer until you execute Transaction commands. Once you execute transaction
commands its store permanent to a database.

SQL COMMIT
SQL COMMIT command save new changes store into database.

Syntax
COMMIT;

Example
SQL> COMMIT;

Commit complete.

SQL SAVEPOINT
SQL SAVEPOINT command create new save point. SAVEPOINT command save the current point with the unique name
in the processing of a transaction.

Syntax
SAVEPOINT savepoint_name;

Example
SQL> CREATE TABLE emp_data (
no NUMBER(3),
name VARCHAR(50),
code VARCHAR(12)
);

Table created.

SQL> SAVEPOINT table_create;

Savepoint created.

SQL> insert into emp_data VALUES(1,'Opal', 'e1401');

1 row created.

SQL> SAVEPOINT insert_1;

Savepoint created.

SQL> insert into emp_data VALUES(2,'Becca', 'e1402');

1 row created.
SQL> SAVEPOINT insert_2;

Savepoint created.

SQL> SELECT * FROM emp_data;

NO NAME CODE
---------- ------------------------------------------ ------------
1 Opal e1401
2 Becca e1402

SQL ROLLBACK
SQL ROLLBACK command execute at the end of current transaction and undo/undone any changes made since the
begin transaction.

Syntax
ROLLBACK [To SAVEPOINT_NAME];

Example

Above example we are create 3 SAVEPOINT table_create, insert_1 and insert_2. Now we are rollback to insert_1
SAVEPOINT.
SQL> ROLLBACK TO insert_1;

Rollback complete.

SQL> SELECT * FROM emp_data;

NO NAME CODE
---------- ------------------------------------------ ------------
1 Opal e1401

SQL TRUNCATE TABLE


SQL TRUNCATE TABLE command used to completely remove all table records. Not supporting to a WHERE clause.

SQL TRUNCATE command is faster and use some transaction log resources.

SQL TRUNCATE command logically equivalent to a DELETE command that deletes all rows, but they are practically
different under some rules.
SQL TRUNCATE command Rules
 TRUNCATE operation use for dropping or re-create table, which is much faster than deleting rows one by one.
 TRUNCATE operation not rollback, It means truncated can not be returned.
 TRUNCATE operation is not a safe.

Syntax

Considering following syntax that help you to understanding TRUNCATE,


TRUNCATE TABLE table_name;

Example
SQL> TRUNCATE TABLE emp_data;

Table Truncated.

Different between Delete and Truncate Commands


  DELETE TRUNCATE

1 If we want to delete all the records from the table, then our Whereas Truncate command is used to delete all records from
SQL query is: the table. Our SQL query is:
SQL> TRUNCATE TABLE table_name;
SQL> DELETE FROM table_name;
2 DELETE statement with WHERE clause you can remove Whereas TRUNCATE statement use for remove all record in a
specific record in a table. table.
SQL> DELETE FROM table_name
WHERE condition;

3 DELETE statement is a DML (Data Manipulation Language) Whereas TRUNCATE statement is a DDL (Data Definition
command. Language) command.

4 Executed DELETE statement you can UNDO the changes Whereas executed TRUNCATE statement you can't return
and return back to deleted data. back.

SQL INDEX
What is an index in SQL? SQL INDEX are used to quickly find data without searching every rows in a database table.

SQL INDEX is improve the speed of search operation on a database table. But additional you need more storage space to
maintain duplicate copy of the database.

INDEX is a copy of the selected column of the database table to store additionally duplicate copy of the data.

End users does not know for indexes is created on table, only they are searching data more quickly and efficiently.

Type of SQL INDEX


CREATE INDEX statement to create indexes on a table. following 3 type indexes you are create on a table.

 Simple INDEX : Create INDEX on one column.


 Composite INDEX : Create INDEX on multiple columns.
 Unique INDEX : Create INDEX on column for restrict duplicate values on INDEX column.
Simple INDEX
Simple INDEX create only one selected column of the database table.

Syntax
CREATE INDEX index_name
ON table_name (column_name)
[ storage_setting ];

Storage setting specifies the table space explicitly. This are the optional storage setting if you are not specifies
automatically default storage setting used.

Example
SQL> CREATE INDEX index_user_name
ON userinfo (name);

We are creating simple index on name column of the userinfo table. In this column allow duplicate values of the column.

Composite INDEX
Composite INDEX create on multiple selected column of the database table.

Syntax
CREATE INDEX index_name
ON table_name (column_name, column_name)
[ storage_setting ];

Example
SQL> CREATE INDEX index_userinfo
ON userinfo (no, name);

We are creating composite index on no, name column of the userinfo table. Duplicate values are allowing for creating
indexes.

Unique INDEX
Unique INDEX create on selected column of the database table and does not allow duplicate values of that indexes
column.

Syntax
CREATE UNIQUE INDEX index_name
ON table_name (column_name)
[ storage_setting ];

Example
SQL> CREATE UNIQUE INDEX index_user_name
ON userinfo (name);

We are create unique index on name column of the userinfo table. Duplicate name value are does not allow again for
creating indexes.

RENAME INDEX
Syntax
ALTER INDEX index_name
RENAME TO new_index_name;

Example
SQL> ALTER INDEX user_name
RENAME TO index_username;

We are renaming the above created index name index_user_name to a new index name index_username.

DROP INDEX
Syntax
DROP INDEX index_name;

Example
SQL> DROP INDEX index_username;

In this statement we are dropping index_username INDEX.

SQL JOIN
SQL JOIN four different types: INNER JOIN, OUTER JOIN, SELF JOIN, CROSS JOIN.

SQL JOIN clause use when select records rows from two or more tables from the database. It's depend on certain
columns from two table. Matching columns are evaluate and if predicated TRUE return a records set data in specified
format.

Considering following different types of SQL JOIN visually.


Inner Join: Inner join check join condition (allow other comparison operator such as <, > etc) and create record set result
that are combining columns value from the tables(two or more table).

 Equi Join: Equiv join is a same as inner join but different is check condition only specific type comparison (not
allowing comparison operator such as <, > etc).
 Natural Join: Natural join is a same as Equi join but different is resulting contains allow only one column for each pair of same
columns named. Record set contains haven't same name columns are found.
Outer Join: Outer Join is a join two table involving common attributes from two tables. But tables (Table A) does not
require to have a matching value to other table (Table B).

 Left Join/Left Outer Join: Left Outer Join always contains all records of left table (Table A) even of join condition
does not find any matching record in right table (Table B).

 Right Join/Right Outer Join: Right Outer Join always contains all records of right table (Table B) even of join
condition does not find any matching record in left table (Table A).
 Full Join/Full Outer Join: Full Outer Join always contains all records of left table (Table A) and right table (Table B) even of
join condition does not find any matching record in both left or right table. Returned result contains set NULL value for all
column that are lack of value in matching rows.

SELF Join: Self Join joining table to itself.


Cross Join: Cross Join joining tables rows and return Cartesian product(each row from Table A with each row of Table B)
record set result.

Example Table
Considering following category, product is our example table. In this following tables category_id column
of category table is primary key and product table category_id foreign key.
CREATE TABLE category(
category_id number(3) PRIMARY KEY,
category_name VARCHAR(25)
);
INSERT INTO category VALUES (1,'Mobiles');
INSERT INTO category VALUES (2,'Laptops');
INSERT INTO category VALUES (3,'Tablet');
INSERT INTO category VALUES (4,'Cameras');
INSERT INTO category VALUES (5,'Gaming');
CREATE TABLE product(
category_id number(3) REFERENCES category(category_id),
product_name VARCHAR(25)
);

INSERT INTO product VALUES (1,'Nokia');


INSERT INTO product VALUES (1,'Samsung');
INSERT INTO product VALUES (2,'HP');
INSERT INTO product VALUES (2,'Dell');
INSERT INTO product VALUES (3,'Apple');
INSERT INTO product VALUES (4,'Nikon');
INSERT INTO product VALUES (NULL,'Playstation');

SQL INNER JOIN


SQL INNER JOIN write two different way: explicit inner join and implicit inner join.

SQL INNER JOIN check join condition (including other comparison operator such as <, > etc) and create record set result
that are combining columns value from the tables (two or more table).

SQL INNER JOIN compare each row of Table A with each row of Table B which are satisfied the join predicate and return
record set rows.
SQL INNER JOIN write two different way:

 Explicit inner join and


 Implicit inner join

Example Table
Considering following category, product is our example table.

SQL> SELECT * FROM category; SQL> SELECT * FROM product;

CATEGORY_ID CATEGORY_NAME CATEGORY_ID PRODUCT_NAME

1 Mobiles 1 Nokia
2 Laptops 1 Samsung

3 Laptops 2 HP

4 Cameras 2 Dell

5 Gaming 3 Apple

4 Nikon

Null Playstation

Run it...    »

Explicit Inner Join


Explicit inner join use INNER JOIN keyword to specify the table to join. And ON keyword to specify join predicates
condition. Consider following SQL inner join example that help you understanding,

Example
SQL> SELECT *
FROM product INNER JOIN category
ON product.category_id = category.category_id;

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- --------------------- ----------- ---------------------
1 Nokia 1 Mobiles
1 Samsung 1 Mobiles
2 HP 2 Laptops
2 Dell 2 Laptops
3 Apple 3 Tablet
4 Nikon 4 Cameras

6 rows selected.

Run it...    »

Implicit Inner Join


Implicit inner join list of table join using FROM and WHERE clause keyword that are specify the tables and specify join
predicates condition. Consider following SQL inner join example that help you understanding,

Example
SQL> SELECT *
FROM product, category
WHERE product.category_id = category.category_id;

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- --------------------- ----------- ---------------------
1 Nokia 1 Mobiles
1 Samsung 1 Mobiles
2 HP 2 Laptops
2 Dell 2 Laptops
3 Apple 3 Tablet
4 Nikon 4 Cameras

6 rows selected.

SQL EQUI JOIN


SQL EQUI JOIN is a specific type comparison base join (equally comparison) not allowing other comparison operator
such as <, > <= etc. And create record set result that are combining columns value from the tables (two or more table).
Example Table
Considering following category, product is our example table.

SQL> SELECT * FROM category; SQL> SELECT * FROM product;

CATEGORY_ID CATEGORY_NAME CATEGORY_ID PRODUCT_NAME

1 Mobiles 1 Nokia

2 Laptops 1 Samsung

3 Laptops 2 HP
4 Cameras 2 Dell

5 Gaming 3 Apple

4 Nikon

Null Playstation

Run it...    »

SQL Equi join use JOIN keyword specify table name and ON keyword specify the join predicate condition.

Example
SQL> SELECT *
FROM product JOIN category
ON product.category_id = category.category_id;

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- --------------------- ----------- ---------------------
1 Nokia 1 Mobiles
1 Samsung 1 Mobiles
2 HP 2 Laptops
2 Dell 2 Laptops
3 Apple 3 Tablet
4 Nikon 4 Cameras

6 rows selected.

Run it...    »

You can also write Equi join as following way.

Example
SQL> SELECT *
FROM product, category
WHERE product.category_id = category.category_id;

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- --------------------- ----------- ---------------------
1 Nokia 1 Mobiles
1 Samsung 1 Mobiles
2 HP 2 Laptops
2 Dell 2 Laptops
3 Apple 3 Tablet
4 Nikon 4 Cameras

6 rows selected.

Run it...    »

If join predicate condition both table column name are same, then you can write this query shorthand way by using USING
Keyword.

Example
SQL> SELECT *
FROM product
INNER JOIN category USING (category_id);

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- --------------------- ----------- ---------------------
1 Nokia 1 Mobiles
1 Samsung 1 Mobiles
2 HP 2 Laptops
2 Dell 2 Laptops
3 Apple 3 Tablet
4 Nikon 4 Cameras

6 rows selected.
SQL NATURAL JOIN
SQL NATURAL JOIN is a same as EQUI JOIN but different is resulting contains allow only one column for each pair of
same columns named. Record set contains haven't same name columns are found.

Example Table
Considering following SQL NATURAL JOIN example, category, product is our example table.

SQL> SELECT * FROM category; SQL> SELECT * FROM product;

CATEGORY_ID CATEGORY_NAME CATEGORY_ID PRODUCT_NAME

1 Mobiles 1 Nokia
2 Laptops 1 Samsung

3 Laptops 2 HP

4 Cameras 2 Dell

5 Gaming 3 Apple

4 Nikon

Null Playstation

Run it...    »

SQL Natural join query use NATURAL JOIN keyword to specify table name.

Example
SQL> SELECT *
FROM product NATURAL JOIN category;
CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME
----------- ------------------ ----------- -------------------
1 Nokia 1 Mobiles
1 Samsung 1 Mobiles
2 HP 2 Laptops
2 Dell 2 Laptops
3 Apple 3 Tablet
4 Nikon 4 Cameras

6 rows selected.

SQL LEFT JOIN


SQL LEFT JOIN (SQL LEFT OUTER JOIN) always contains all records of left table (Table A) even of join condition does
not find any matching record in right table (Table B).

SQL OUTER JOIN - OUTER JOIN is a join two table involving common attributes from two tables. But tables (Table A) does
not require to have a matching value to other table (Table B).
Example Table
Considering following SQL left join example, category, product is our example table.

SQL> SELECT * FROM category; SQL> SELECT * FROM product;

CATEGORY_ID CATEGORY_NAME CATEGORY_ID PRODUCT_NAME

1 Mobiles 1 Nokia

2 Laptops 1 Samsung

3 Laptops 2 HP
4 Cameras 2 Dell

5 Gaming 3 Apple

4 Nikon

Null Playstation

Run it...    »

Example
SQL> SELECT *
FROM product LEFT OUTER JOIN category
ON product.category_id = category.category_id;

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- ---------------------- ----------- ----------------------
1 Samsung 1 Mobiles
1 Nokia 1 Mobiles
2 Dell 2 Laptops
2 HP 2 Laptops
3 Apple 3 Tablet
4 Nikon 4 Cameras
NULL Playstation NULL NULL

7 rows selected.

Run it...    »

Following is alternative syntax result produce same as above,

Example
SQL> SELECT *
FROM product, category
WHERE product.category_id = category.category_id(+);

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- ---------------------- ----------- ----------------------
1 Samsung 1 Mobiles
1 Nokia 1 Mobiles
2 Dell 2 Laptops
2 HP 2 Laptops
3 Apple 3 Tablet
4 Nikon 4 Cameras
NULL Playstation NULL NULL

7 rows selected.

SQL RIGHT JOIN


SQL RIGHT JOIN (RIGHT OUTER JOIN) always contains all records of right table (Table B) even of join condition does
not find any matching record in left table (Table A).

SQL OUTER JOIN - OUTER JOIN is a join two table involving common attributes from two tables. But tables (Table A) does
not require to have a matching value to other table (Table B).
Example Table
Considering following SQL left join example, category, product is our example table.

SQL> SELECT * FROM category; SQL> SELECT * FROM product;

CATEGORY_ID CATEGORY_NAME CATEGORY_ID PRODUCT_NAME

1 Mobiles 1 Nokia

2 Laptops 1 Samsung

3 Laptops 2 HP

4 Cameras 2 Dell

5 Gaming 3 Apple

4 Nikon
Null Playstation

Run it...    »

Example
SQL> SELECT *
FROM product RIGHT OUTER JOIN category
ON product.category_id = category.category_id;

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- -------------------- ----------- --------------------
1 Nokia 1 Mobiles
1 Samsung 1 Mobiles
2 HP 2 Laptops
2 Dell 2 Laptops
3 Apple 3 Tablet
4 Nikon 4 Cameras
NULL NULL 5 Gaming

7 rows selected

SQL FULL JOIN


SQL FULL JOIN (FULL OUTER JOIN) always contains all records of left table (Table A) and right table (Table B) even of
join condition does not find any matching record in both left or right table. Returned result contains set NULL value for all
column that are lack of value in matching rows.

Example Table
Considering following SQL FULL JOIN example, category, product is our example table.

SQL> SELECT * FROM category; SQL> SELECT * FROM product;

CATEGORY_ID CATEGORY_NAME CATEGORY_ID PRODUCT_NAME

1 Mobiles 1 Nokia
2 Laptops 1 Samsung

3 Laptops 2 HP

4 Cameras 2 Dell

5 Gaming 3 Apple

4 Nikon

Null Playstation

Run it...    »

Example
SQL> SELECT *
FROM product FULL OUTER JOIN category
ON product.category_id = category.category_id;

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- ------------------------- ----------- -------------------------
1 Samsung 1 Mobiles
1 Nokia 1 Mobiles
2 Dell 2 Laptops
2 HP 2 Laptops
3 Apple 3 Tablet
4 Nikon 4 Cameras
NULL Playstation NULL NULL
NULL NULL 5 Gaming

8 rows selected.

SQL SELF JOIN


SQL Self JOIN joining same table to itself. SQL Self JOIN possible only when table stored records are in hierarchical
relationship between them.

You must use table alias name otherwise you can't distinguish columns name (referenced of which table's).

Logically assume that same table have two different copies but not actually different copies.
Example Table
Considering following orders table is our example table.
SQL> CREATE TABLE orders(
order_id number(3),
product_name VARCHAR(25),
customer_name VARCHAR(25),
order_amount number(5)
);

INSERT INTO orders VALUES (1,'Nokia Mobile', 'Opal Kole', 290);


INSERT INTO orders VALUES (2,'Flip Cover', 'Max Miller', 15);
INSERT INTO orders VALUES (3,'Handsfree', 'Beccaa Moss', 7);
INSERT INTO orders VALUES (4,'Canon Camera', 'Paul Singh', 2200);
INSERT INTO orders VALUES (5,'Playstation', 'Max Miller', 70);
INSERT INTO orders VALUES (6,'Sandwich Maker', 'Beccaa Moss', 28);
INSERT INTO orders VALUES (7,'Pen Drive 16GB', 'Opal Kole', 12);
INSERT INTO orders VALUES (8,'Headphone', 'Reed Koch', 38);
INSERT INTO orders VALUES (9,'HP Laptop i5', 'Reed Koch', 1300);
INSERT INTO orders VALUES (10,'Titanic Bluray Disk', 'Beccaa Moss', 78);
SELECT * FROM orders;

ORDER_ID PRODUCT_NAME CUSTOMER_NAME ORDER_AMOUNT


-------- ------------------ ---------------------- --------------
1 Nokia Mobile Opal Kole 290
2 Flip Cover Max Miller 15
3 Handsfree Beccaa Moss 7
4 Canon Camera Paul Singh 2200
5 Playstation Max Miller 70
6 Sandwich Maker Beccaa Moss 28
7 Pen Drive 16GB Opal Kole 12
8 Headphone Reed Koch 38
9 HP Laptop i5 Reed Koch 1300
10 Titanic Bluray DiskBeccaa Moss 78

10 rows selected.

Run it...    »

SQL SELF Join Example


SQL> SELECT A.order_id, A.product_name,
B.order_id, B.product_name, B.customer_name
FROM orders A
INNER JOIN orders B
ON A.customer_name = B.customer_name
WHERE A.order_id < B.order_id;

ORDER_ID PRODUCT_NAME CUSTOMER_NAME


------------ ------------------ ----------------------
2 Flip Cover Max Miller
3 Handsfree Beccaa Moss
1 Nokia Mobile Opal Kole
8 Headphone Reed Koch
6 Sandwich Maker Beccaa Moss
3 Handsfree Beccaa Moss

Run it...    »

SQL SELF Join Example Explanation

Above example we are use A and B table alias name.

Inner Join to join predicate condition (A.customer_name = B.customer_name) customer_name only those whose are in


same customer_name in Table A or Table B. It's mean cross customer_name are eliminate.

And last WHERE clause A.order_id < B.order_id means eliminate pairings where order_id Table A is not less than
to order_id of Table B.
SQL CROSS JOIN
SQL CROSS JOIN joining tables rows and return Cartesian product(each row from Table A with each row of Table B)
record set result.

SQL CROSS JOIN write two different way:

 Explicit Cross Join and


 Implicit Cross Join

Example Table
Considering following category, product is our example table.
SQL> SELECT * FROM category; SQL> SELECT * FROM product;

CATEGORY_ID CATEGORY_NAME CATEGORY_ID PRODUCT_NAME

1 Mobiles 1 Nokia

2 Laptops 1 Samsung

3 Laptops 2 HP

4 Cameras 2 Dell

5 Gaming 3 Apple

4 Nikon

Null Playstation

Run it...    »
Explicit Example
SQL> SELECT *
FROM product
CROSS JOIN category;

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- ------------------------- ----------- -------------------------
1 Nokia 1 Mobiles
1 Samsung 1 Mobiles
2 HP 1 Mobiles
2 Dell 1 Mobiles
3 Apple 1 Mobiles
4 Nikon 1 Mobiles
NULL Playstation 1 Mobiles
1 Nokia 2 Laptops
1 Samsung 2 Laptops
2 HP 2 Laptops
2 Dell 2 Laptops
3 Apple 2 Laptops
4 Nikon 2 Laptops
NULL Playstation 2 Laptops
1 Nokia 3 Tablet
1 Samsung 3 Tablet
2 HP 3 Tablet
2 Dell 3 Tablet
3 Apple 3 Tablet
4 Nikon 3 Tablet
NULL Playstation 3 Tablet
1 Nokia 4 Cameras
1 Samsung 4 Cameras
2 HP 4 Cameras
2 Dell 4 Cameras
3 Apple 4 Cameras
4 Nikon 4 Cameras
NULL Playstation 4 Cameras
1 Nokia 5 Gaming
1 Samsung 5 Gaming
2 HP 5 Gaming
2 Dell 5 Gaming
3 Apple 5 Gaming
4 Nikon 5 Gaming
NULL Playstation 5 Gaming

35 rows selected.
Run it...    »

Implicit Example
SQL> SELECT *
FROM product, category;

CATEGORY_ID PRODUCT_NAME CATEGORY_ID CATEGORY_NAME


----------- ------------------------- ----------- -------------------------
1 Nokia 1 Mobiles
1 Samsung 1 Mobiles
2 HP 1 Mobiles
2 Dell 1 Mobiles
3 Apple 1 Mobiles
4 Nikon 1 Mobiles
NULL Playstation 1 Mobiles
1 Nokia 2 Laptops
1 Samsung 2 Laptops
2 HP 2 Laptops
2 Dell 2 Laptops
3 Apple 2 Laptops
4 Nikon 2 Laptops
NULL Playstation 2 Laptops
1 Nokia 3 Tablet
1 Samsung 3 Tablet
2 HP 3 Tablet
2 Dell 3 Tablet
3 Apple 3 Tablet
4 Nikon 3 Tablet
NULL Playstation 3 Tablet
1 Nokia 4 Cameras
1 Samsung 4 Cameras
2 HP 4 Cameras
2 Dell 4 Cameras
3 Apple 4 Cameras
4 Nikon 4 Cameras
NULL Playstation 4 Cameras
1 Nokia 5 Gaming
1 Samsung 5 Gaming
2 HP 5 Gaming
2 Dell 5 Gaming
3 Apple 5 Gaming
4 Nikon 5 Gaming
NULL Playstation 5 Gaming
35 rows selected.

SQL UNION, UNION ALL


SQL UNION clause combines same column(s) name values from two or more table and eliminates duplicate value.

SQL UNION clause is useful when you want to select distinct values (in selected columns) from the tables.

SQL UNION vs UNION ALL


SQL UNION clause used to select distinct values from the tables..

SQL UNION ALL clause used to select all values including duplicates from the tables.

Performance difference between UNION vs UNION ALL - SQL engine do some additional work for removing do additional
work to eliminates the duplicated rows.

UNION clause does not support BLOB or CLOB data type columns where as the UNION ALL support this data types.

Examples
Considering following product, out_of_stock_product is our example table. Both table have same column
name product_name. Execute this for creating table.

Example Table
CREATE TABLE product(
category_id number(3),
product_name VARCHAR(25)
);

INSERT INTO product VALUES (1,'Nokia');


INSERT INTO product VALUES (2,'Samsung');
INSERT INTO product VALUES (3,'HP');
INSERT INTO product VALUES (4,'Dell');
INSERT INTO product VALUES (5,'Apple');
INSERT INTO product VALUES (6,'Nikon');
INSERT INTO product VALUES (7,'Playstation');

CREATE TABLE out_of_stock_product(


category_id number(3),
product_name VARCHAR(25)
);

INSERT INTO out_of_stock_product VALUES (1,'Samsung');


INSERT INTO out_of_stock_product VALUES (2,'LG');
INSERT INTO out_of_stock_product VALUES (3,'HTC');
INSERT INTO out_of_stock_product VALUES (4,'HP');
INSERT INTO out_of_stock_product VALUES (5,'Dell');
INSERT INTO out_of_stock_product VALUES (6,'Apple');
INSERT INTO out_of_stock_product VALUES (7,'Canon');
INSERT INTO out_of_stock_product VALUES (8,'Nikon');
INSERT INTO out_of_stock_product VALUES (9,'Xbox');
INSERT INTO out_of_stock_product VALUES (10,'Playstation');

Run it...    »

SQL> SELECT * FROM category; SQL> SELECT * FROM product;

CATEGORY_ID PRODUCT_NAME CATEGORY_ID PRODUCT_NAME

1 Nokia 1 Samsung

2 Samsung 2 LG

3 HP 3 HTC

4 Dell 4 HP

5 Apple 5 Dell
6 Nikon 6 Apple

7 Playstation 7 Canon

8 Nikon

9 Xbox

10 Playstation

SQL UNION Example


SQL> SELECT product_name FROM product
UNION
SELECT product_name FROM out_of_stock_product;

PRODUCT_NAME
-------------------------
Apple
Cameras
Dell
Gaming
HP
Laptops
Mobiles
Nikon
Nokia
Playstation
Samsung
Tablet

12 rows selected.

Run it...    »

SQL UNION ALL Example


SQL> SELECT product_name FROM product
UNION ALL
SELECT product_name FROM out_of_stock_product;

PRODUCT_NAME
-------------------------
Apple
Cameras
Dell
Gaming
HP
Laptops
Mobiles
Nikon
Nokia
Playstation
Samsung
Tablet

12 rows selected.

SQL Functions
Built-in Oracle SQL functions are following,

 SQL Numeric Functions


 SQL String Functions

Oracle SQL provide buit-in SQL functions. SQL Functions take some values as a arguments, perform some function logic
and returning some values. SQL inbuilt function are so many that all are help us for no need to implementing your own
logic.
SQL Numeric Functions
SQL numeric function take numeric value (maximum 38 digit) and return numeric value (maximum 38 digit). SQL numeric
function return single row single column values. Following are some SQL numeric function.

Function Function Parameter Description

ABS ABS(n) Function return absolute value of n values.

AVG AVG(expression) Function return average value of expression.

BITAND BITAND Function take bitwise vector value and return the bitwise AND vector
value.

BIN_TO_NUM BIN_TO_NUM(n, ...) Function take any number of bit values. every value is must be either 0 or
1. This all bit values convert to a hexadecimal numeric value.

CEIL CEIL(n) Function return smallest integer round value that is greater then or equal
to a parameter value (n).

COUNT COUNT(*) Function return the number of rows in a SELECT statement.


COUNT(expression)
FLOOR FLOOR(n) Function return largest integer round value that is equal to or less then to
a parameter value (n).

LN LN(n) Function return exact log value (natural logarithm value).

LOG LOG(n1,n2) Function return log value (base on n1 value of n2 value).

MAX MAX(expression) Function return maximum value of expression.

MIN MIN(expression) Function return minimum value of expression.

MOD MOD(n1,n2) Function return the reminder value of n1 divide by n2. Where n1 and n2 is
natural value.

NANVL NANVL(value,alternative_value) Function return the alternative value if the specified value is NaN (Not a
number). If value not NaN then return original value.

POWER POWER(n1,n2) Function return n1 raised to a n2 power. n1 is base value and n2 is any
numbers.
RAND RAND() Function generate random number between 0 to 1, but no longer support.
Optionally use Rownum to fetch random record from database table.

REMAINDER REMAINDER(n1,n2) Function return the remainder value of n1 divide by n2. Where n1 and n2
is natural value.

ROUND ROUND(n, decimal_number) Function return the round number of specified nth number of decimal
place.

SIGN SIGN(n) Function return the sign of the n number.

SQRT SQRT(n) Function return the square root of the n number. SQRT function return the
real number.

SUM SUM(expression) Function return the sum of expression.

TRUNC TRUNC(n, decimal_number) Function return the truncated number of specified nth number of decimal
place.
SQL String Functions
SQL String function take some value and return string value. Function return data type VARCHAR2 if parameter argument
CHAR or VARCHAR2 data type. Following are some SQL String function.

Function Function Parameter Description

ASCII ASCII( character ) Function return the ASCII values of given argument.

CHR CHR( number ) Function return the ASCII value of the given argument value.

CONCAT CONCAT( string1, string2 ) Function concatenated string1 with string2 and return to a
concatenated string.

INITCAP INITCAP( char/string ) Function return capitalize string/char (capitalize first letter of
each word).

INSTR INSTR( original_string, sub_string ) Function return sub string position from the original string.

LENGTH LENGTH( string ) Function return the length of string.


LOWER LOWER( char/string ) Function return lowercase character in every word.

LTRIM LTRIM( string, trim_char ) Function remove all specified trim char from left side of the
string.

NCHR NCHR( ascii_number ) Function return the ASCII value of the given argument value.

REPLACE REPLACE( string, match_string, Function return the string/char of every matched string replace
replace_string ) with new string.

REGEXP_REPLACE REGEXP_REPLACE( original_string, Function original string represent to a regular expression pattern.
pattern )

REGEXP_SUBSTR REGEXP_SUBSTR( original_string, Function return substring from the original string using regular
pattern ) expression pattern.

REGEXP_LIKE REGEXP_LIKE( original_string, Function same as like condition but matching regular expression
pattern ) pattern to perform like condition.
RTRIM RTRIM( string, trim_char ) Function remove all specified trim char from right side of the
string.

SUBSTR SUBSTR( string, start_position, Function return a selected string character from a full string.
substr_length )

TRANSLATE TRANSLATE( string, match_char, Function return the string/char of every matched character
replace_char ) replace with new character.

TRIM TRIM( string ) Function remove all specified trim char from beginning and
ending of the string.

UPPER UPPER( char/string ) Function return uppercase character in every word.

You might also like