SQL
SQL
SQL
Syntax:
Create table <table name>
(<column name1> <datatype>(<size>),
<column name2> <datatype>(<size>)
<column nameN> <datatype>(<size>));
Output:
Table Created
(2) Alter Command
For Example:
Alter table Student ADD ( Marks number(5));
Syntax:
Alter Table <Table Name> DROP Column <Column Name>;
For Example: Drop the column name Marks from the student
table
Syntax
Drop Table <Tablename>;
Insert Command
Syntax:
Output:
2 rows created
Insert Null Values
Syntax:
insert all
into Student(name, marks) values(John', 1)
into student (name, marks) values(Roy', 3)
into student(name, marks) values(ram', 4);
For Example:
Student
John Mumbai 2
Name
Rahul
Filtering Table Data
Selected columns and all rows
Syntax::
select <column1>, <column2> from <table name>;
Name City
John Mumbai
Rahul Delhi
Selected rows and all columns:- using where
clause
Retrieval of specific rows from a table
Syntax::
select * from <table name> where<condition>;
John Mumbai 2
Selected rows and selected columns:-
Retrieval of specific rows and specific column from a table
Syntax::
select <column1>, <column2> from <table name>
where<condition>;
Name City
John Mumbai
Updating a Table
Syntax:
1) DELETE from <table name>;
For Example: Delete all the records of those students who live
in Mumbai city.
Name
Rahul
Herry
John
Distinct Command
Eliminating duplicate rows from the table.
Syntax:
Select DISTINCT * from <table name>;
For Example:
CREATE TABLE Employee ( E_Id number(10) Primary Key,
LastName varchar2(10) NOT NULL, FirstName varchar2(10)
UNIQUE, Address varchar2(10) UNIQUE, City varchar2(10) );
For Example:
Create table Customer_master (Cust_id number(5) primary key,
address char(10) default India, loan_no number(6));
Create table emp_database (emp_no number(5) primary key,
name varchar2(10), designation varchar2(10) , C_no number
(10) References customer_master (Cust_id), salary number(5));
OR
Create table emp_database (emp_no Number(5) primary key,
name varchar2(10), designation varchar2(10) , C_no number
(10), salary number(5), foreign key (C_No) References
customer_master (Cust_id));
Add a Foreign Key:
Syntax:
<column name> <data type> CHECK (Condition)
For Example:
a) CREATE TABLE Persons ( P_Id number(10) NOT NULL CHECK
(P_Id>0),
LastName varchar2(20) NOT NULL, FirstName varchar(255),
Address varchar2(20) );
b) CREATE TABLE Customer ( C_Id number(5) Primary Key ,
Name varchar2(20), address varchar2(20) ,
check (Name Like C%), Check(Name =UPPER(Name) );
c) Alter table cust_master ADD check (cust_no>1);
Default CONSTRAINT
The DEFAULT constraint is used to provide a default value for a
column. The default value will be added to all new records if no
other value is specified.
For Example:
CREATE TABLE Employee ( E_Id number(10) Primary Key,
LastName varchar2(10) NOT NULL, FirstName varchar2(10), City
varchar2(10) default London);
Insert into Employee (E_Id, Lastname, FirstName) values (11,
John, Pareira);
Output
Note: By default value will be London but we can also update the
value at the time of insert records.
Logical Operators
1. AND : It is used for creating SQL statement with two or more
conditions and all conditions must be satisfied.
Subtraction (-)
Division (/)
Multiplication (*)
Enclosed Operation ()
Range Searching (Between Operator)
Retrieve data within range of values.
The range coded after the word BETWEEN in inclusive and two
values in between the range must be linked with a keyword AND.
Description
Books
Pens
Bags
Pencils
Description
Folders
IN Operator
Order Operator
2 Comparison Operators
3 IN
4 BETWEEN
Syntax:
Insert into <Tablename> Select <ColumnName1>, < ColumnName
N> from <Table Name>;
For Example: insert into Course select name, city, marks from
student where marks=10;
Aggregate Functions
3. MIN
Operates on numbers and non numbers as well.
4. MAX
5. COUNT
SUM()
The sum of the values in a specified column.
Syntax:
Select SUM (column name) from <table name> where
<condition>; OR
Sum(salary) OR Total_Salary
29000 29000
AVG()
The average of the values in a specified column.
Syntax:
Select AVG (column name) from <table name> where
<condition>;
For Example: Select AVG (salary) from client ;
AVG(salary)
9.666666
Min()
It returns the smallest values in a specified column.
Syntax:
Select MIN (column name) from <table name> where
<condition>;
For Example: Select MIN (sell_price) from product ;
Min(marks)
9
Max()
It returns the maximum value in a specified column.
Syntax:
Select MAX (column name) from <table name> where
<condition>;
For Example: Select MAX (salary) from client ;
Max(marks)
10
COUNT()
Returns the Number of records in a specified column where
expression is not null.
Syntax:
Select COUNT (Column Name) from <Table Name>;
For Example:
Select COUNT (name) from client;
COUNT(name)
3
COUNT(*)
Returns the total Number of records in a specified column.
Syntax:
Select COUNT (*) from <Table Name>;
For Example:
Select COUNT (*) from client;
COUNT(*)
5
Set Operations
SQL supports few SET Operations on data table.
These functions are used to get meaningful results from
different conditions.
Syntax:
select <column name> from table name1 UNION select
<column name> from table name2
OR
select <column name> from table name 1 where condition
UNION select <column name> from table name 2 (or 1) where
condition
Client_ID Name Cust_ID Name
c001 Ivan c002 John
c002 John c003 William
CLIENT C004 Lara
Employee
select clinet_id, name from CLIENT UNION select cust_id, name from
employee
Client_ID Name
c001 Ivan
c002 John
C003 Willaim
c004 Lara
select client_id, name From CLIENT UNION ALL select clust_id, name from
CUSTOMER
Client_ID Name
c001 Ivan
c002 John
C002 John
c003 William
c004 Lara
Set Operation: INTERSECT
It is used to combine two select statement or tables and return
the common result.
Syntax:
select <column name> from table name1 INTERSECT select
<column name> from table name2
OR
select <column name> from table name 1 where condition
INTERSECT select <column name> from table name 2 where
condition
Client_ID Name Cust_ID Name
c001 Ivan c002 John
c002 John c003 William
C004 Lara
CLIENT
Employee
select clinet_id, name from CLIENT INTERSECT select cust_id, name from
Employee
Client_ID Name
c002 John
Set Operation: EXCEPT OR MINUS
Combine results of two select statement and return only those
results which is in first set.
Syntax:
select <column name> from table name1 minus select <column
name> from table name2
OR
select <column name> from table name 1 where condition minus
select <column name> from table name 2 where condition
Client_ID Name Cust_ID Name
c001 Ivan c002 John
c002 John c003 William
C004 Lara
CLIENT
CUSTOMER
select clinet_id, name from CLIENT Minus select cust_id, name from
CUSTOMER
Client_ID Name
c001 Ivan
GROUP BY CLAUSE
Having clause
The HAVING clause specifies conditions that filter which group
results appear in the final results.
The WHERE clause places conditions on the selected columns,
whereas the HAVING clause places conditions on groups created by
the GROUP BY clause.
The HAVING clause must follow the GROUP BY clause in a query and
must also precede the ORDER BY clause if used.
Syntax:
SELECT column1, column2 FROM table WHERE [ conditions ]
GROUP BY column HAVING [ conditions ] ORDER BY column
Find out the product description and total quantity according to
their product description which is having average sell price greater
than 100.
The SQL LIKE condition can be used in any valid SQL statement :
Select, Insert, Update and Delete
Return the employee records whose salary is of 4 digits and starting with 57
Select * from employee where salary LIKE 57__;
EMP-ID NAME SALARY
1002 Jane Anderson 5750
d) SYSDATE
20
9
3. ROUND: Returns rounded value
Syntax: ROUND(n[m])
Select ROUND(23.66, 1) ROUND from DUAL;
Round
---------------------------
23.7
LEAST
----------------------
16
125.3
9. FLOOR: Returns the largest integer value that is equal to or less than
number
Syntax: FLOOR(n)
Select FLOOR(24.8) FLOOR from DUAL;
FLOOR
-------------------------
24
10. CEIL: Returns the smallest integer value that is equal to or greater than
number
Syntax: CEIL(n)
Select CEIL(24.8) ceil from DUAL;
ceil
-----------------
25
Syntax:
Example:
Output: 21-09-13
To_Date ()
It converts character values into date value
Example:
Output: 21-SEP-13
VIEWS in SQL
A view is a virtual table contains rows and columns, just like a real
table. The fields in a view are fields from one or more real tables in
the database.
Syntax:
CREATE VIEW view_name AS SELECT column_name(s) FROM
table_name WHERE condition
For Example:
Create View product_vw AS select description from product
where city = Mumbai Order by city
Syntax:
Select column(s) from VIEW;
For Example:
Select description from product_vw;
To Delete VIEW:
Drop View <View Name>
1) Natural Join
Inner Join
Equi Join
2) Outer Join
Left Outer Join
Right Outer Join
Full Outer Join
3) Cross Join
Tables are joined on columns that have same datatype and size
Inner Join
It is a natural join used for retrieving records from more than two
tables having common data
Inner join can be Equi join if it is having equal condition only
Syntax:
Table 1 Table 2
Resultant
Table
Emp_No Name Branch_No
11 Smith B001 List out the employee details
23 William B007 along with their branch name
34 Simi C006 using Left Outer Join
Employee
Table 1 Table 2
Resultant
Table
Emp_No Name Branch_No
11 Smith B001 List out the employee details
23 William B007 along with their branch name
34 Simi C006 using Right Outer Join
Employee
Select e.emp_no, e.name,
b.branch_name from Employee e
Branch_Name Branch_
No RIGHT OUTER JOIN Branch b ON b.
Mumbai B001
branch_no = e. branch no
Mumbai B007
Pune C009 Select e.emp_no, e.name,
Banglore F007 b.branch_name from Employee e,
Branch b where b. branch_no (+) = e.
Branch
branch no;
Full Outer Join
It does both of those operations, padding tuples from the left
relation that did not match any from the right relation
As well as tuples from the right relation that did not match any
from the left relation and adding them to the result of the join.
Table 2
Table 1
Resultant
Table
Emp_No Name Branch_No
11 Smith B001 List out the employee details
23 William B007 along with their branch name
34 Simi C006 using Full Join
Employee
Branch
SUBQUERIES
A subquery is a form of SQL statement that appears inside
another SQL statement
Subquery also termed as Nested Queries
First table is called Parent statement which uses rows returned by
subqueries