Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.

Similar presentations


Presentation on theme: "Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh."— Presentation transcript:

1

2 Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh

3 Tables create table account (account_number varchar(15)not null, branch_name varchar(15) not null, balance numbernot null, primary key(account_number)); create table branch (branch_name varchar(15) not null, branch_city varchar(15) not null, assets number not null, primary key(branch_name)); account(account_number, branch_name, balance); branch(branch_name, branch_city, assets);

4 Tables create table customer (customer_name varchar(15)not null, customer_street varchar(12)not null, customer_city varchar(15) not null, primary key(customer_name)); create table loan (loan_number varchar(15)not null, branch_name varchar(15)not null, amount number not null, primary key(loan_number)); customer(customer_name, customer_street, customer_city ); Loan(loan_number, branch_name, amount);

5 Tables create table depositor (customer_name varchar(15)not null, account_number varchar(15)not null, primary key(customer_name, account_number), foreign key(account_number) references account(account_number), foreign key(customer_name) references customer(customer_name)); create table borrower (customer_name varchar(15)not null, loan_number varchar(15) not null, primary key(customer_name, loan_number), foreign key(customer_name) references customer(customer_name), foreign key(loan_number) references loan(loan_number)); depositor(customer_name, account_number); borrower(customer_name, loan_number);

6 Basic Structure A typical SQL query has the form: select A 1, A 2, …, A n from R 1, R 2, …, R m where condition - A i represent attributes - R i represent relations This query is equivalent to the relational algebra expression:  A1, A2, …, An (  P (R 1  R 2  …  R m )) The result of an SQL query is a relation.

7 Projection The select corresponds to the projection operation of the relational algebra. It is used to list the attributes desired in the result of a query. Find the names of all branches in the loan relation select branch-name from loan Equivalent to:  branch-name (loan) An asterisk in the select clause denotes “all attributes” select * from loan

8 Duplicate Removal SQL allows duplicates in relations as well as in query results. Use select distinct to force the elimination of duplicates. Find the names of all branches in the loan relation, and remove duplicates select distinct branch-name from loan The keyword all specifies that duplicates not be removed. select all branch-name from loan force the DBMS to remove duplicates force the DBMS not to remove duplicates

9 Arithmetic Operations on Retrieved Results The select clause can contain arithmetic expressions involving the operators, , ,  and , and operating on constants or attributes of tuples. The query: select branch-name, loan-number, amount * 100 from loan would return a relation which is the same as the loan relations, except that the attribute amount is multiplied by 100

10 The where Clause The where clause specifies conditions that tuples in the relations in the from clause must satisfy. Find all loan numbers for loans made at the Nablus branch with loan amounts greater than $1200. select loan-number from loan where branch-name=“Nablus” and amount >1200 SQL allows logical connectives and, or, and not. Arithmetic expressions can be used in the comparison operators.

11 The where Clause (Cont.) SQL includes the between operator Find the loan number of those loans with loan amounts between $90,000 and $100,000 (that is,  $90,000 and  $100,000) select loan-number from loan where amount between 90000and 100000

12 The from Clause The from clause corresponds to the Cartesian product operation of the relational algebra. Find the Cartesian product borrower  loan select * from borrower, loan It is rarely used without a where clause. Find the name and loan number of all customers having a loan at the Nablus branch. select distinct customer-name, borrower.loan-number from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = “Nablus”

13 Table loan branch-nameloan-numberamount Nablus Perryridge L-170 L-260 L-230 3000 1700 4000 borrower cust-nameloan-number Jones Hayes Smith L-170 L-230 L-155 branch-nameloan-numberamount Nablus L-170 L-230 3000 4000 cust-name Jones Smith Loan Borrower

14 The Rename Operation Renaming relations and attributes using the as clause: old-name as new-name Find the name and loan number of all customers having a loan at the Nablus branch; replace the column name loan-number with the name loan-id. select distinct customer-name, borrower.loan-number as loan-id from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = “Nablus”

15 Tuple Variables/Alias Tuple variables are defined in the from clause via the use of the “as” clause. Find the customer names and their loan numbers for all customers having a loan at some branch. select distinct customer-name, T.loan-number from borrower as T, loan as S where T.loan-number = S.loan-number

16 String Operations Character attributes can be compared to a pattern: % matches any substring. Find the name of all customers whose street includes the substring ‘Main’. (Eg Mainroad, Smallmain Road, AMainroad,…) select customer-name from customer where customer-street like “%Main%” How to match the name “Main%”: (Eg abcMain%, MainMain%,…)

17 Ordering the Display of Tuples List in alphabetic order the names of all customers having a loan at Nablus branch select distinct customer-name from borrower, loan where borrower.loan-number = loan.loan-number and branch-name = “Nablus” order by customer-name order by customer-name desc, amount asc desc for descending order; asc for ascending order (default)

18 Set operations Find all customers who have a loan, an account, or both: (select customer-name from depositor) union (select customer-name from borrower) Find all customers who have both a loan and an account. (select customer-name from depositor) intersect (select customer-name from borrower) Find all customers who have an account but no loan. (select customer-name from depositor) except (select customer-name from borrower)

19 SQL Aggregate Functions

20 Aggregate Functions Operates on a column of a relation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values

21 Aggregate Functions(cont.) Find the average account balance at the Nablus branch. select avg(balance) from account where branch-name=“Nablus” account select balance from account where branch-name =“Nablus” Avg() 120,000 balance

22 Aggregate Functions(cont.) Find the numbers of tuples in the customer relation. select count(*) from customer –remember * stands for all attributes –compare to: select count(customer-city) from customer Find the number of depositors in the bank select count (distinct customer-name) from depositor

23 Aggregate functions - Group by Find the number of accounts for each branch. select branch-name, count( distinct account-number) from account group by branch-name For each group of tuples with the same branch-name, apply aggregate function count and distinct to account-number account table

24 Null values It is possible for tuples to have a null value, denoted by null, for some of their attributes; null signifies an unknown value or that a value does not exist. The result of any arithmetic expression involving null is null. More precisely, –Any comparison with null returns unknown (caution: Oracle treats it as false!) –(true or unknown) = true, (false or unknown) = unknown (unknown or unknown) = unknown, (true and unknown) = unknown, (false and unknown) = false (unknown and unknown) = unknown –Result of where clause predicate is treated as false if it evaluates to unknown

25 Null Values (cont.) Find all loan numbers which appear in the loan relation with null values for amount. select loan-number from loan where amount is null Total of all loan amounts select sum(amount) from loan Above statement ignores null amounts;

26 Check for each borrower if he is also a depositor Example Nested Query Find all customers who have both an account and a loan in the bank. select distinct customer-name from borrower where customer-name in (select customer-name from depositor)

27 Example Query Find all customers who have a loan at the bank but do not have an account at the bank. select distinct customer-name from borrower where customer-name not in (select customer-name from depositor)

28 Views Provide a mechanism to hide certain data from the view of certain users. To create a view we use the command: create view view-name as where: – is any legal SQL query –the name of the view is represented by view- name

29 Example Queries A view consisting of branches and their customers create view all-customer as (select branch-name, customer-name from depositor, account where depositor.account-number = account.name-number) union (select branch-name, customer-name from borrower, loan where borrower.loan-number = loan.loan-number) Find all customers of the Nablus branch select customer-name from all-customer where branch-name = “Nablus”


Download ppt "Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh."

Similar presentations


Ads by Google