Unit 1 ADBMS
Unit 1 ADBMS
Unit-1
Structured Query Language:
SQL stands for Structured Query Language which is a standard language for
accessing and manipulating data bases. SQL was initially developed at IBM
(International Business Machine). It was design to manipulate & retrieve data in
relational database management system.
Elements of SQL:
1. Queries- It retrieves data based on particular or specific task. A query is a
database command that retrieve records.
2. Statements- It may control program flow transaction.
3. Expressions- It can produce tables which consisting of rows and columns
of data.
4. Clauses- Clauses are some optional component of queries & statement .
Working of SQL
SQL Request
Database
DBMS
Data
Features of SQL:
1. SQL is very flexible.
2. SQL based application are portable i.e. they can be run on any system.
3. SQL is a high level language.
4. SQL is a free format syntax which gives users the ability to structure SQL
statement on any of the stream
5. SQL can be embedded also in front end application code like visual
basics 6.0, java, and c++.
6. SQL itself is not a database where as it is just a language i.e. used to
create database and tables.
7. SQL is not case sensitive.
SQL Commands:
-SQL commands are instruction which is used to communicate with database.
-It is also used to perform specific task, function and queries of data.
Types of SQL commands:
SQL commands
DDL DML DCL TCL DQL
LIKE Condition :
It allows you to use wild cards in the WHERE clause of an SQL statements. The
LIKE condition can be used in any valid SQL statement SELECT, INSERT,
UPDATE OR DELETE.
The patterns that you can choose are as follows:
1. Percentile (‘ % ’) –
Percentile allows you to match any string of any length.
2. Underscore (‘ _ ‘) –
Underscore allows you to match on a single character.
Example :
Suppose we want to display the name of employee starts with letter R then,
SELECT emp_id , Name from employee WHERE Name LIKE ‘R%’;
Name Salary
Mohan 4000
Pankaj 3500
Pinky 4500
Ritesh 3100
2. OR Operator:
If you want to select rows that satisfy at least one condition from the given
condition you can use the logical operator OR.
Example:
SEELECT emp_id, Name from employee WHERE Name = ‘Ritesh’OR Salary
= 4000;
empid Name
102 Mohan
107 Ritesh
3. NOT Operator :
If you want to find rows, that don’t satisfy condition you can use the logical
operator NOT.
Example :
SELECT emp_id , Name from employee WHERE NOT Job = ‘clerk’;
empid Name
101 Rohan
103 Pankaj
106 Manish
107 Ritesh
4. IN Operator :
This can be used to select the rows that match one of the values in the list .
Syntax :
SELECT column_name from<table_name>WHERE column_name IN
(value1,value2,…);
Example: SELECT Name ,Salary from employee WHERE Salary IN
(3000,4000,5000);
Name Salary
Rohan 3000
Mohan 4000
Manish 5000
5. BETWEEN Operator:
The BETWEEN operator is used in a WHERE clause to select a range of data
between two values the values can be numbers, text, dates.
Syntax: SELECT column_name from <table_name>WHERE column_name
BETWEEN value1 AND value2;
Example :
SELECT Name, Salary from employee WHERE Salary BETWEEN 3500 AND
6500;
Name Salary
Rohan 5000
Mohan 4000
Pankaj 3500
Roma 6500
Pinky 4500
Manish 5000
Amar 4700
CUSTOMER
Saledate Avg(price)
08-Dec-06 36.66
10-Dec-06 25
2.COUNT function: Count function count the number of rows including the
no. of values present in the column without including NULLS.
SELECT custname, count (custname) from customer group by custname;
Custname Count(custname)
Mangesh 2
Bharat 3
Pavan 2
4. MIN() : This function will give the least of all values of the column present
in the argument.
SELECT custname , MIN(price) from customer group by custname;
Custname MIN(price)
Mangesh 30
Bharat 20
Pavan 15
5.SUM: The sum function can be used to obtain the sum of a range of values of
a record set.
SELECT custname, sum(Qty+price) as a total from customer group by
customer;
Custname Total
Mangesh 93
Bharat 86
Pavan 42
Cust table
Cname Qty Icode
Rakesh 4 101
Mohan 10 103
Sumit 8 105
Customers
Id Name Age Address Salary
Orders
Oid Date Customer id Amount
1 2016-10-08 3 3000
2 2016-10-08 3 1500
3 2016-11-20 2 1560
4 2015-05-20 4 2060
1.SELECT id, name , Name, Age, Amount from customers orders WHERE
customerid=orders.customer_id;
B. Inner join :
It is also called as simple / equi join which is use frequently on SQL for joining
tables. The inner join creates a new result table by combining column values of
two tables.
Syntax: SELECT table1.column1,table2.column2,… from table1 inner join
table 2 on table1.column field=table2.column field;
SELECT id, Name, amount, date from customers inner join orders on
customers.id =orders.customer_id;
Id Name Amount Date
3 Kaushal 3000 2016-10-08
3 Kaushal 1500 2016-10-08
2 Ketan 1560 2016-11-20
4 Chaitanya 2060 2015-05-20
2.LEFT join :
Left join returns all values from the left table plus matched values from the
right tab keyword return all rows from left table (table1) with the matching rows
in the right table (table2). The result is NULL in the right side when there is no
match.
Syntax : SELECT table1.column1,table2.column2 from table1 left join table 2
on table1.common field=table2.common field;
Example:
SELECT id, name, amount, date from customers left join orders on
customers.id=orders.customerid;
Id Name Amount Date
1 Rajesh NULL NULL
2 Ketan 1560 2016-11-20
3 Kaushal 3000 2016-10-08
3 Kaushal 1500 2016-10-08
4 Chaitanya 2060 2015-05-20
5 Harish NULL NULL
6 Ketki NULL NULL
3. RIGHT join :
A right join returns all values from right table plus matched values from the left
table.
In some databases right join is called as right join .
The right join keyword returns all rows from right table (table 2) with the
matching rows in the left table (table 1)
The result is NULL in the left side when there is no match.
Syntax: SELECT table1.column1, table2.column2 from table1 right join table 2
on table1.common field=table2.common field;
Example :
SELECT id, name, amount, date from customers right join order on
customer.id=orders.customersid;
Subqueries :
A subquery is a query within a query .As we know a query language is a
language in which a user requests information from the databases. The result of
the subquery is used by the DBMS to determine the result of higher level query
that contains a subquery. A subquery or inner query or nested query is query
within another query and embedded with WHERE clause. A subquery is use to
return data that will be used in main query. As a condition to further restrict the
data to be retrieve. Subquery can be used with the SELECT, INSERT,
UPDATE and DELETE Statements along with operators like =, <,>, <=, >=, IN
and BETWEEN etc.
Rules for the sub queries must follow:
1. Sub queries must be enclosed with parenthesis.
2. A sub query can have only one column in the SELECT clause, unless
multiple columns are in main query for the sub query to compare its
selected column.
3. An order by cannot be used in sub query. Although, the main query can
use an order by, the group by can be used to perform same function as the
order by in subquery.
4. A sub query that returns more than 1 row can only be used with multiple
value operator such that IN operator.
5. The SELECT list cannot include any references to values to a BLOB,
Array, CLOB or NCLOB .
6. A subquery cannot be immediately closed with a set function.
7. The BETWEEN operator cannot be used with a subquery. However the
BETWEEN operator used within subquery.
CUSTOMER
ID Name Age Address Salary
Following example deletes record from customer table from all the
customer whose age>= 27.
SQL> DELETE FROM CUSTOMER WHERE Age IN[SELECT Age
From customer detail WHERE Age>=27]
The SQL Set operation is used to combine the two or more SQL SELECT
statements.
1. Union
2. UnionAll
3. Intersect
4. Minus
1. Union
o The SQL Union operation is used to combine the result of two or more
SQL SELECT queries.
o In the union operation, all the number of datatype and columns must be
same in both the tables on which UNION operation is being applied.
o The union operation eliminates the duplicate rows from its resultset.
Syntax
Example:
ID NAME
1 Jack
2 Harry
3 Jackson
ID NAME
3 Jackson
4 Stephan
5 David
ID NAME
1 Jack
2 Harry
3 Jackson
4 Stephan
5 David
2. Union All
Union All operation is equal to the Union operation. It returns the set without
removing duplication and sorting the data.
Syntax:
ID NAME
1 Jack
2 Harry
3 Jackson
3 Jackson
4 Stephan
5 David
3. Intersect
Syntax
Example:
ID NAME
3 Jackson
4. Minus
Syntax:
Example
1 Jack
2 Harry
CASE
IF
These statements are similar; however the CASE statements extends the IF
statement.