Xpresdata SQL Material-1
Xpresdata SQL Material-1
2. SQL SERVER
3. DB2
4. MYSQL
5. SYBASE
6. TERA DATA
3. DRL/DQL (Retrieval/query)
CREATE
ALTER
DROP
TRUNCATE
RENAME
INSERT
UPDATE
DELETE
MERGE
SELECT
COMMIT
ROLLBACK
SAVEPOINT
GRANT
REVOKE
Varchar2
Number(P,S)
Date
SQL Operator is a special symbol in database which can be used for Mathematical and Logical
Operations.
o Comparison Operators
o Logical Operators
o Special Operators
- Subtract
* Multiply
/ Divide
Operator Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
OR
NOT
NVL(expr1, expr2)
It defines the logical relationship of the tables and is within the database
The tables contain columns that you eventually map to objects that end users use
to create reports.
The joins link the tables so that the correct data is returned for queries that are run
on more than one table.
A schema is the set of metadata (data dictionary) used by the database, typically
generated using DDL.
1. Star Schema
2. Snow Flake Schema
3. Galaxy Schema
The design involves more than one fact table, which are
connected further with multiple dimension tables that are
completely normalized.
o SYSDATE
o ADD_MONTHS
o LAST_DAY
o NEXT_DAY
o MONTHS_BETWEEN
o MIN
o AVG
o SUM
o COUNT(*)
ROLLUP
CUBE
TO_DATE
TO_CHAR
Joins are used to get the data from more than one table.
If you we have ‘n’ tables then we need to use ‘n-1’ join conditions.
8i Joins
Equi Join
Non Equi Join
Self Join
Outer Join
9i Joins
Inner Join
Left Outer Join
Right Outer Join
Natural Join
We use equality operator (=) in where condition to retrieve the data from
multiple tables using join condition.
Select column1,column2,…….
From Table1,Table2
Where Table1.Commoncolumn=Table2.Commoncolumn ;
Write a query to get maximum, minimum, and sum salary from the location
wise?
Hierarchical Relationship
We can fetch the all rows from one table and matching rows from another table
using Outer Join.
This Join operator is used only one side in the joining condition.
4. Write a query to get sum salary in department name wise along with total
5. Display the employees from each department wise and also display the
department names which has more than 5 employees?
9i Joins
1. Inner Join
Select column1,column2,….
From TableName1 join TableName2
On TableName1.Commoncolumn=TableName2.Commoncolumn ;
on e.deptno=d.deptno
Where loc=‘CHICAGO’;
Returns all rows from left side table and matching rows from right side table.
Returns all rows from right side table and matching rows from left side table.
And also returns ‘NULL’ values in the place of non-matching rows in another
table.
on e.deptno= d.deptno;
We are using this Natural Join when we do not required joining condition
between the tables.
Example:
Syntax:
Example:
No.of Rows in the Result of Cross Join = (No. of Rows in LEFT Table) * (No. of Rows in RIGHT Table)
56 ROWS
1. Not Null
2. Unique
3. Primary Key
4. Foreign Key
5. Check
Not Null doesn’t support null values but it will accept duplicate values.
Note: This Constraint is not supported for Table Level.
Example :
create table xpresdata (sno number(10) not null,name varchar2(100));
insert into xpresdata values (10,’BCD’);
insert into xpresdata values (10,’DFG’);
insert into xpresdata values (null,'ABC’); - Error
Note:
Primary Key Column & Foreign Key Column must be same datatype.
Example :
Note:
Only Primary key data will be inserted into Foreign key columns in any
tables.
Check Constraint is used to stop invalid entry to the columns in the tables.
Example 01:
Example 02 :
create table xpresdata7(sno number(10),name varchar2(100)
check(name=upper(name)),sal number(5));
insert into xpresdata7 values (1,'ARJUN',8000);
insert into xpresdata7 values (2,'Uma',9000); -> Error
Important Note:
Select search_condition,table_name,constraint_type from user_constraints where
table_name=‘XPRESDATA7’;
Step-01:
desc user_constraints;
Step-02 :
They are :
Union
Union All
Intersect
Minus
Syntax :
UNION
Syntax :
Union All
Syntax :
intersect
Syntax :
intersect
Minus - ?
1. Row_Number()
2. Rank()
3. Dense_Rank()
Rank() Function will assign same ranks when values are same but rank() will
skip next consecutive rank numbers.
Dense_Rank() Function will assign same ranks when values are same but
Dense_Rank() doesn’t skip any consecutive rank numbers.