Introduction of Structured Query Language: SQL Practical File
Introduction of Structured Query Language: SQL Practical File
Introduction of Structured Query Language: SQL Practical File
SQL is Structured Query Language, which is a computer language for storing, manipulating and
retrieving data stored in relational database.
Allows users to define the data in database and manipulate that data.
Allows to embed within other languages using SQL modules, libraries & pre-compilers.
SQL Statements
SQL statements are divided into four different categories: Data definition language (DDL), Data
manipulation language (DML), Data Control Language (DCL), Transaction Control Statement
(TCS).
Data definition statement are use to define the database structure or table.
Data manipulation commands are those commands which used to manipulate the data or the
information that is been stored into the database.
Data control language provides command to grant and take back authority.
SQL commands
The create table statement is used to create a table in a database. A table consists of rows and
columns and must be saved with an appropriate name.
Syntax:
CREATE TABLE<tablename>
(<col_name1><datatype>(<size>),
<col_name1><datatype>(<size>),……)
2. Insert Command:
This command is used to insert or load the values into a table or relation.
Syntax:
INSERT INTO<tablename>
VALUES(‘&<label1>’,’&<label2>,……..);
3. View/Select :
Select statement is used to retrieve rows from the existing table.using select statement we can
retrieve all rows from table or selected rows from a table.
4. Update
First clause: the actual update clause, where the table that will be update is named
Second clause : is the Set clause where all columns that will be changed by the update
statement are named, along with their new values.
Example:
5. Alter
Either to add a column or to change the datatype of already existing column in already existing
table,ALTER table statement is used.
6. Delete
The DELETE command deletes the rows from the table that satisfies the condition provided
by WHERE clause, and returns the number of rows deleted.
Example:
7. Rename
8. Describe
Syntax: DESCRIBE<tablename>;
INTEGRITY CONSTRAINTS
Integrity Constraints are used to apply business rules for the database tables.The constraints
available in SQL are Foreign Key, Not Null, Unique, Check.
1) The constraints can be specified immediately after the column definition. This is called
column-level definition.
2) The constraints can be specified after all the columns are defined. This is called table-
level definition.
1. NOT NULL
This constraint ensures all rows in the table contain a definite value for the column which is
specified as not null. Which means a null value is not allowed.
2. UNIQUE
This constraint defines a column or combination of columns which uniquely identifies each row
in the table.
Syntax:
[constraint constraint_name]Unique;
3. PRIMARY KEY
This constraint defines a column or combination of columns which uniquely identifies each row
in the table.A combination of a NOT NULL and UNIQUE
PRIMARY KEY(col_name);
Example:
4. FOREIGN KEY
This constraint identifies any column referencing the PRIMARY KEY in another table. It
establishes a relationship between two columns in the same table or between different tables.
For a column to be defined as a Foreign Key, it should be a defined as a Primary Key in the table
which it is referring. One or more columns can be defined as Foreign key.
Syntax://TABLE LEVEL
FOREIGN KEY(<col_name>)REFERENCES<tablename>(<col_name>)
5. CHECK
This constraint defines a business rule on a column. All the rows must satisfy this rule. The
constraint can be applied for a single column or a group of columns.
Syntax:
10 | P a g e Charanjeev Singh
SQL Practical File
6. JOINS IN SQL
An SQL JOIN clause is used to combine rows from two or more tables, based on a common field
between them.
The INNER JOIN keyword selects all rows from both tables as long as there is a match between
the columns in both tables.
Syntax:
Select<field1>,<field2>……
from <table1>,<table2>………
where<table1.keyfield=table2.keyfield>;
Example:
11 | P a g e Charanjeev Singh
SQL Practical File
Self Join
The SQL SELF JOIN is used to join a table to itself as if the table were two tables, temporarily
renaming at least one table in the SQL statement.
Syntax:
Select<table1.colname>,<table2.colname>……
where<condition>;
Example:
Outer Join
LEFT OUTER JOIN - This returns all the matching rows and the unmatched rows of the left table
of the SQL code. RIGHT OUTER JOIN - This returns all the matching rows and the unmatched
rows of the right table of the SQL. FULL OUTER JOIN - This returns all the matching and
unmatched rows from both the tables
12 | P a g e Charanjeev Singh
SQL Practical File
Syntax:
Select<table1.colname>,<table2.colname>……
from <table1>,<table2>………
where
<table1.colname>=<table2.colname>(+);
Example:
The CROSS JOIN produced a result set which is the product of rows of two associated tables
when no WHERE clause is used with CROSS JOIN. In this join the result set appeared by
multiplying each row of the first table with all rows in the second table if no condition
introduced with CROSS JOIN.
Syntax:
SELECT(<COL1>,<COL2>……..)
FROM(<TABLE1><TABLE2>…..);
13 | P a g e Charanjeev Singh
SQL Practical File
Example:
7. SET OPERATORS
Set operators are used to join the results of two (or more) SELECT statements.The SET operators
available in Oracle 11g are UNION,UNION ALL,INTERSECT,and MINUS.
All the SET operators share the same degree of precedence among them.
UNION
The UNION set operator returns the combined results of the two SELECT
statements.Essentially,it removes duplicates from the results i.e. only one row will be listed for
each duplicated result
Syntax:
INTERSECTION
INTERSECT lists only records that are common to both the SELECT queries.
Syntax:
14 | P a g e Charanjeev Singh
SQL Practical File
INTERSECT
MINUS
the MINUS set operator removes the second query's results from the output if they are also
found in the first query's results.
Syntax:
Output:-
15 | P a g e Charanjeev Singh
SQL Practical File
VIEWS IN SQL
A view is nothing more than a SQL statement that is stored in the database with an associated
name. A view is actually a composition of a table in the form of a predefined SQL query.
A view can contain all rows of a table or select rows from a table.
Syntax:
as select<col1,col2,…..>
from<table name>
[WHERE <condition>]
[<HAVING CLAUSE>];
Example:
16 | P a g e Charanjeev Singh
SQL Practical File
JOIN VIEW
A join view is a view which is based in more than one base table. Any insert, update or delete
statement on a view can update only one of the base tables involved in the view. A table is
called a key preserved in a join view, if the primary and unique keys are unique on the views
result set. Join view does not use GROUP BY, ROWNUM and DISTINCT clause.
Dropping a view:
The SQL DROP VIEW statement is the SQL command that removes an entire SQL view. You can
change the definition of a view by dropping and re-creating it. When you drop a view all the
associated grants are also dropped. You can also change the definition of the view by using the
CREATE OR REPLACE command.
Inline View:
It is a subquery that appears in the ‘from’ clause of the select statement. This subquery is
enclosed in parenthesis and may be given an alias name. The columns selected in the subquery
can be referenced in the parent query.
17 | P a g e Charanjeev Singh
SQL Practical File
Materialized View:
In a materialized view, a summary table is created from a base table, and all queries that
perform a similar summation against the base table will be transparently re-written to
reference the pre-built summary table.A materialized view is a database object that contains
the results of a query. They are local copies of data located remotely, or are used to create
summary tables based on aggregations of a table’s data.
Materialized views, which store data based on remote tables are also, know as snapshots. A
materialized view can query tables, views, and other materialized views. Collectively these are
called master tables (a replication term) or detail tables (a data warehouse term).
A materialized view provides indirect access to table data by storing the results of a query in a
separate schema object. Unlike an ordinary view, which does not take up any storage space or
contain any data.
Materialized views are used in data warehouses so as to increase the speed of queries on a
larger database. In large databases, the queries often involve joins between tables and
aggregation like sum and etc. so to improve the performance materialized views are used.
Materialized views can be used to replicate the data.
18 | P a g e Charanjeev Singh