3 Structure Query Language SQL
3 Structure Query Language SQL
The execution of this DDL statement creates the `student’ table. It also updates a
special set of tables called data dictionary or data directory. A data dictionary
contains metadata, that is data about data. The schema of table is an example of
metadata. A database system consults data dictionary or data directory.
Through the set of special type of DDL, called data storage definition language, we
may specify the storage structure (like size of database, size of table etc) and access
methods.
© T. Paneru 1
3.0 Structure Query Language (SQL)
The DDL allow to enforce constrains in the database. For example: student_id
should begin with `S’, address could not be null etc.
CREATE TABLE STUDENT
(
student_id VARCHAR2 (3),
address VARCHAR2 NOT NULL,
CONSTRAINT ch_student_id CHECK (student_id LIKE `S%’)
);
The database systems check these constraints every time the database is updated.
© T. Paneru 2
3.0 Structure Query Language (SQL)
Note: We don’t need to specify the table name while referencing column_name if we
are taking column from only one table.
SELECT customer.customer_name,account.balance
FROM customer,account.balance
WHERE customer.customer_id= depositor.customer_id
AND depositor.acount_no = account.account_no;
Note: Column name need not to specify if we are going to insert values for all
columns of table.
• You cannot delete all records, only when “account” and “deposit” tables are
empty or only when these table contains records that are not related to the
customer.
Query: Increase the balance by 5% in account table whose account no is
`A101’ or current balance is only 200.
UPDATE account
SET balance = balance + (balance + 0.05)
WHERE account_no = `A1001’ OR balance = 200;
© T. Paneru 3
3.0 Structure Query Language (SQL)
Note:
Sometimes database languages are also categorized with Data Control
Language.
That is
1. Data Definition Language (DDL)
2. Data Control Language (DCL)
3. Data Manipulation (DML)
SQL DDL provides commands for defining relation schemas, deleting schema,
deleting relations and modifying relational schemas.
Example:
CREATE, ALTER, DROP
© T. Paneru 4
3.0 Structure Query Language (SQL)
);
The SQL DML includes query language based on relational algebra and relational
calculus. It includes commands for insert tuples, delete tuples and modify tuples in
database.
3. View Definition:
The SQL DDL includes for defining views e.g.
Syntax:
CREATE VIEW <view name> AS
(<query expression>);
6. Integrity:
SQL DDL includes commands for specifying integrity constraints that the data stored
in database must satisfy.
7. Authorization:
SQL DDL commands used for specifying access right to relation relations and views.
© T. Paneru 5
3.0 Structure Query Language (SQL)
Note: Commands in SQL are not case-sensitive. But generally conversation is write
a keywords as a capital and other should be in small letter.
SQL provides the following basic data manipulation statements: SELECT, UPDATE,
DELETE and INSERT.
The select statements:
- The SELECT statement is most commonly used SQL statement. It is only a data
retrieval statement in SQL.
- The basic syntax for select statement is
SELECT [DISTINCT / ALL ] <attributes> 1
FROM <relations> 2
[WHERE <predicate>] 3
g. List all customer whose name should be “smith” and address should be
“Kathmandu”.
SELECT customer_name FROM customer
WHERE customer_name = ‘smith’
OR customer_ address = ‘Kathmandu’
k. Write SQL statement for (i) using only AND logical connectives and
comparison operatives.
SELECT account_no, balance FROM account
WHERE balance <= 700 AND balance >= 200;
Note: We can retrieve the information from multiple tables; there should be a
common attribute between two tables. i.e., table should be related and we require to
join condition.
l. List the customer_id, account_id and balance whose balance is more than
300.
© T. Paneru 7
3.0 Structure Query Language (SQL)
• We can also rename attributes, it is required when we are taking attribute from
multiple column or we need arithmetic operations in the statement or when we
need to give appropriate name for (column name) attribute name.
• To rename attribute SQL provides as clause or we can simply rename attribute
or relation without as clause.
Examples:
a. SELECT account_no as account number FROM account;
OR
SELECT account_no “Account number” FROM account;
String operations:
String pattern matching operation on SQL can be performed by `like’ operator and
we can describe the patterns by two spherical character.
Example:
`I%’ matches any string beginning with I.
IVAN -> valid / match.
INDIA -> valid / match
NEPALI -> invalid / does not match.
Moreover,
Problems:
© T. Paneru 8
3.0 Structure Query Language (SQL)
Example: 1
Example: 2
Example: 3
Suppose want list account information in descending order by balance but if say
some balance are same and in such case if we want to order account information by
order_no in ascending order then we have order record by performing ordering on
multiple attributes. The SQL statement likes,
SELECT *FROM account
ORDER BY balance DESC, account ASC;
Set operation
• basic set operation are union(u), intersection(n) and difference(_). These
operation also can be performed by using union, intersection and minus
(except) clause respectively.
© T. Paneru 9
3.0 Structure Query Language (SQL)
- Proceed as follow:
Output from 1st SQL statement
ID Name
S001 Ashok
S002 Manoj
ID Name
C001 Ammit
C002 Ammit
ID Name
C001 Ammit
C002 Ammit
C003 Ashok
C004 Manoj
Note: if we retrieve only one column say name without duplicate name then
corresponding statement like
Select name from supplier where city = ‘Kathmandu’
UNION
Select name from client where city = ‘Kathmandu’;
© T. Paneru 10
3.0 Structure Query Language (SQL)
Example:
select name from supplier where city = ‘Kathmandu’.
SALESMAN
order_no Order_date salesman_id
salesman_id name city 0001 10-JAN-98 S001
S001 Manish Kathmandu 0002 12-FEB-98 S002
S002 Manoj Lalitpur 0003 13-FEB-98 S001
S003 Ammit Bhaktapur 0004 18-MAR-98 S001
S004 Rabin Kathmandu 0005 19-MAR-98 S002
Retrieve salesman name who stay in Kathmandu and who must sales at least order.
SELECT salesman_id, name
from salesman salesman_id name
where city = ‘Kathmandu’ S001 Manish
S004 Rabin
INTERSECT
salesman_id name
SELECT salesman. salesman_id
S001 Manish
from salesman, sales_order
Where salesman_id = S002 Manoj
sales_order. salesman_id; S001 Manish
S002 Manoj
salesman_id name
S001 Manish
The difference operation can be perform in SQL by using except or minus clause.
Example: in previous example, find the salesman_id, name who stay in Kathmandu
but they do not sales any order.
The output is
salesman_id name
S004 Rabin
1. Find all customer who have a loan, account or both at the bank .
SELECT customer nameFrom depositor
UNION
SELECT customer name From borrower;
2. Find all customers who have both loan and account at the bank.
SELECT DISTINCT customer name from depositor
INTERSECT
SELECT DISTINCT customer name from borrower;
3. Find all customers who have account but no loan at the bank.
Aggregate Function
Aggregate functions are those functions that take a set of values as input and return
a single value. SQL consist many built in aggregate function. Some are:
1. AVERGE: AVG
2. MAXIMUM: MAX
3. MINIMUM: MIN
4. TOTAL: SUM
5. COUNT: COUNT
The input to AVG and SUM must be a set of numbers and other aggregate function
can be operate by non numeric data types, it may be strings, not necessary
numbers.
AVG:
There would be a situation that we may have to use aggregate function not only a
single set of tuples we may have to use with group of set of tuples, we can specify
this by using GROUP BY clause in SQL. That is, group by clause specifies group rows
based on distinct values that exist in specified column when we use GROUP BY
clause we can not use WHERE clause to specify condition, we must have to use
HAVING clause to specify the condition. That is, GROUP BY and HAVING clause. But
GROUP BY or HAVING clause act on record sets rather than individual records.
MIN:
Syntax: MIN (<DISTINCT\ALL>; n)
• returns minimum value of n.
MAX:
Syntax: MAX (<DISTINCT ALL>; n)
• returns maximum value of n
GROUP BY branch_name;
COUNT:
Syntax: COUNT (<DISTINCT ALL>; n)
• returns numbers of rows where n is not null.
NOTE: each depositor may have numbers of account so we must count depositor
only once thus we write query as below:
PROBLEM: find only those branches where the average account balance is more
than 1200.
SELECT branch_name, AVG (balance) FROM account
GROUP BY branch_name having AVG (balance) > 1200;
NOTE: If a WHERE clause and HAVING clause both appears in the same query, SQL
executes predicate in the WHERE clause first and if it satisfied the only it executes
predicate of GROUP BY clause.
PROBLEM: Find the average balance for each customer who lives in KATHMANDU
and has at least three accounts.
SUM
Syntax: SUM ([DICTINCT/ ALL] n)
• return sum of value of n
NULL VALUES
• In SQL, NULL values all to indicate absence of information for the value of
attribute.
• SQL provides special key word NULL in a predicate to test for NULL values.
Not NULL in predicate use to test absence of NULL values.
Example: find the balance of each branch whose balance is not empty.
SELECT branch_name, balance
FROM account
WHERE balance is NOT NULL;
Example: List the account number, branch name and balance whose balance is
empty.
• The feature of SQL that handles NULL values has important application but
some time it gives unpredictable result. For example, an arithmetic
expression involving (+. _ , * or /), if any of the input values is NULL value
(except IS NULL Q IS NOT NULL).
• If NULL values exist in the processing of aggregate operation, it makes
process complicated.
Example: SELECT AVG (amount) FROM loan;
• This query calculates the average loan amount that is not empty so if there
exist empty amount then calculated average amount is not valid. Except
COUNT(*) function all aggregate function ignores NULL values in input.
• The COUNT ( ) function return if count value is empty and all other aggregate
function returns NULL if it found empty value.
© T. Paneru 15
3.0 Structure Query Language (SQL)
• here the result is unpredictable. In this case result should be 100. To handle such
unpredictable situation SQL provides NVL ( ) function
Example: SELECT sal+nvl(comm,0)/100
• nvl function returns 0 when comm is found empty. If user do not specify the
value for any column (attribute) SQL place null values in these columns. The null
value is different from zero. That is null value is not equivalent to value zero.
• A NULL value will evaluate to NULL in any expression.
Example: NULL multiply by 10 is NULL.
• If the column has a NULL value, SQL ignores the unique Foreign key, check
constraints that are attached to the column.
• If any field define as NOT NULL, it does not allow to ignore this field, user must
insert value, that is NOT NULL is itself a constraint while it specify in table.
Nested Subqueries
Example: find all branch name where depositor account number is ‘A005’ .
SELECT branch name FROM account
WHERE account account_number = (SELECT account_number FROM
depositor
WHERE account number
= ‘A005’);
• When sub_query return more than one values the we required to test whether
value written by first query is match/exist or not within values return by
sub_query.
• IN and NOT IN connectives are useful to test in such condition. That is IN
connectives use to test for set membership and NOT IN connectives use to test
for absence of set membership.
Example: find those customers who are borrowers from the bank and who are also
account holder.
SELECT DICTINCT customer_name FROM borrower
WHERE customer_name IN (SELECT customer_name FROM
depositor);
Example: find all customer who do have loan at the bank but do not have an amount
at the bank.
SELECT DISTINCT customer_name FROM borrower
WHERE customer_name NOT IN (SELECT customer_name FROM
depositor);
© T. Paneru 16
3.0 Structure Query Language (SQL)
Example: list the name of customers who have a loan at the bank and whose name
neither SMITH nor JONE
SELECT DISTINCT Customer_name FROM borrower
WHERE customer_name NOT IN (‘SMITH’, ‘JONE’);
Example: find all customers who have both an account and loan at Kathmandu
branch.
SELECT DISTINCT Customer_name FROM borrower, loan
WHERE borrower loan number = loan loan_number and
branch_name = ‘KATHMANDU’
AND (branch_name, customer_name) IN (SELECT
branch_name, customer_name
FROM depositor account
WHERE depositor account_number = account
account_name);
SET COMPARISION
Nested sub_query have an ability to compare set.
Example: Find the names of all branches that have assets greater than those of at
least one branch located
in ‘Kathmandu’.
- here, >some comparison in the where clause of the outer value return by
sub_query.
- SQL also allow <some, >=some, =some and < > some comparison
- Not that = some is identical to IN. but < > some is not same as NOT IN.
Example: Find the names of all branches that have an assets value greater than that
of each branch in ‘KATHMANDU’.
NOTE: The construct > all corresponds to the phase ‘greater than all’
NOTE: SQL also allow <all, <=all, >=all, =all and < >all comparison.
Example: find the branch that has the highest average balance.
NOTE that we can not use MAX {AVG (balance)}, since aggregate function can not
be composed in SQL. So we first need to find all average balances and need to nest
© T. Paneru 17
3.0 Structure Query Language (SQL)
it as subquery of another query that finds those branches for which average balance
is greater than or equal to all average balances.
SELECT branch_name FROM account
GROUP BY branch_name
HAVING AVG (balance) >=all (SELECT AVG (balance) FROM account
GROUP BY
branch_name);
Example: find all customers who have both an account and loan at the bank.
We can test non existence of values (tuples) in sub_query by using not exists
construct.
Example: find all customers who have an account at all branches located in
‘KATHMANDU’.
SQL has a feature for testing whether the subquery has any duplicate Tuples in its
results.
The UNIQUE construct true if a subquery contains no duplicate Tuples.
Example: find all customers who have at most one account at the KATHMANDU
branch.
NOTE: using NOT UNIQUE construct, we can test the existence of duplicate tuples.
Example: find all customers who have at least two account at the KATHMANDU
branch.
© T. Paneru 18
3.0 Structure Query Language (SQL)
Derived Relation
- SQL have a feature that it allow sub_query expression to used in the FROM
clause.
- If we use such expression then we must give result relation name and we can
remove the attributes.
Example: find the average account of those branches where the average account
balance is greater than 1200.
SELECT branch_name, avg_balance
FROM {SELECT branch_name, avg (balance) FROM account GROUP BY
branch_name}
AS branch_avg (branch_name, avg_balance)
WHERE avg_balance > 1200;
The with clause
- The with clause introduced in SQL: 1999 and is currently supported by only
some database.
- The with clause makes query logic clear.
Example: find all branches where the total account deposit is less than the average
deposits at all branches.
WITH branch_total (branch_name, value) as
SELECT branch_name, SUM (balance) FROM account
GROUP BY branch_name
WITH branch_total_avg (value) as
SELECT avg (value) FROM branch_total
SELECT branch_name FROM branch_total, brnch_total_avg
WHERE branch_total.value >= branch_total_avg.value;
UPDATE STATEMENT
The syntax is
UPDATE <relation>
SET <attribute with new value>
[WHERE <predicate>];
Example: increase balance of those branches whose current balance is less than or
equal to 1000 by 5%
UPDATE account
SET balance = balance * 1.05
WHERE balance <= 1000;
NOTE: SQL provides case construct, which can be perform multiple updates with a
single UPDATE statements
The syntax is:
Case
When predicate 1 then result 1
When predicate 2 then result 2
When predicate n then result n
Else result
END
DELETE STATEMENT
The DELETE statement use to delete one or more records from relations. The records
to be deleted are specified by the predicate in the WHERE clause.
Note: Delete statement can operate only one relation. It can not delete records of
multiple relation.
Example: Delete all records from account relation whose branch is located in
Kathmandu.
DELETE FROM account
WHERE branch_name = ‘KATHMANDU;
© T. Paneru 20
3.0 Structure Query Language (SQL)
Example: Delete all loan with loan amount between 1000 and11500;
DELETE FROM account
WHERE branch_name IN (SELECT branch_name FROM branch)
WHERE branch_city = ‘KATHMANDU’);
Example: Delete all records of account with balance below the average
DELETE FROM account
WHERE balance <(SELECT avg (balance) FROM account);
INSERT STATEMENT
The INSERT statement used to insert a new records into a specified relation
Syntax:
INSERT INTO <relation>
VALUES (<values list>)
Another form:
INSERT INTO <relation> (<target columns>)
VALUES (<values list>)
The attributes values that are going to insert must be match order by corresponding
attributes and also must be matched data type of value and corresponding attribute
data type.
Insert Tuple (customer_name, loan_number) into the depositor relation for each
customer who has a loan in Kathmandu branch with loan number.
Joined Relations
- one of the most powerful feature of SQL is its capability to gather and
manipulate data from several relations.
© T. Paneru 21
3.0 Structure Query Language (SQL)
- If SQL does not provides this feature we must have to store all the data
elements in a single relations for each application. We have to store same data
in several relations.
- The join statements of SQL enables to design smaller, more specific relations
that are easier to maintain than larger relations.
- There are several methods for joining relations. Some methods are not useful for
application and some are very useful.
1. Cross Join
Joins two or more tables without relation between them or without condition
in the where clause. The results is the Cartesian product of two or more table’s
attributes
Examples: consider two relations
Table 1:
row remarks Table 2: row remarks
1. Table 1 1. Table 2
2. Table 1 2. Table 2
Output is:
row remarks row remarks
1 Table 1 1 Table 2
1 Table 1 2 Table 2
2 Table 1 1 Table 2
2 Table 1 2 Table 2
- cross join is normally not useful but it illustrates the basic combining property of
all join types.
Example: consider
Relation : dept
relation: emp
#dept. no Depo. name Loc
10 Management Kathmandu
20 Technical Kathmandu
30 Marketing Bhaktapur
40 Account Lalitpur
We can further qualify this query by adding more condition on where clause.
SQL also supports non equi_join. That is, this method joins tables (relations) based
on non equality. But equi_join is far more common than non equi_join
Case 1: equi_join
SELECT e.ename, d.dname, e.deptno “emp deptno”, d.deptno “deptno”
FROM emp e, dept d
WHERE e.deptno;
JONE TECHNICAL 20 20
MICLE TECHNCAL 20 20
JACK ACCOUNT 40 40
Output:
ename dname Emp deptno
JONE MANAGEMENT 20 >10
MICLE MANAGEMENT 20 >10
JACK MANAGEMENT 40 >10
JONE TECHNICAL 20 >10
REMARKS: output is selested from the Cartesian product of two relation
Emp (e.ename, e.deptno)
Dept (e.ename, e.deptno)
Such that emp table of deptno is always greater than 10.
Loan Borrower
Remarks: Tuple from the left hand side relation that do not math any Tuple in the
right side relation are padded with null.
SELECT loan. loan_number, loan . brach_name, loan.amount, borrower.
Customer_name loan
right outer join borrower on loan.loan_number = borrower. Loan_number;
Remarks: Tuples from the right hand-side that do not match any Tuple in the left
hand side relation are padded with nulls.
Retrieve the names of employees and the names of their respective manager
from the employee relation.
Output:
Name Manager
Smith Michael
Michael Scott
John Ivan
Process:
EMP MGR
Emp no name manager no emp no name manager no
E001 Smith E002 E001 Michael E002
E002 Michael E005 E002 Scott E005
E003 John E04 E003 Ivan E004
Name Manager
Smith Michael
Michael Scott
John Ivan
Data definition Language in SQL
• In SQL, DDL specifies set of relations (tables) in a database.
• SQL DDL also allows to specify
o Integrity constraints.
o Index on relations
o Security and authorization for each relation
o Physical storage structure of each relation
© T. Paneru 26
3.0 Structure Query Language (SQL)
PRIMARY KEY :
Primary key is an attribute or combination of multiple attributes that uniquely
identifies records.
If a primary key is a combination of multiple attributes called composite
primary key. A primary
key attributes are required NOT NULL AND UNIQUE. That is primary key
attribute cannot be
left null and it cannot contain duplicate values.
NOT NULL / UNIQUE: Attribute can be specified NOT NULL attribute or unique
attribute.
Drop statement
Syntax:
DROP TABLE <relation name>;
© T. Paneru 30